Custom Projects In Android Studio
Written by Ian Elliot   
Thursday, 16 January 2014
Article Index
Custom Projects In Android Studio
Using a Recipe


The Recipe file does all of the work setting up the project, but it does most of this by copying and expanding directories which exist in the template root directory. 

One key command that you are likely to use is copy, which copies a directory and all its subdirectories from the template root to the project. The command instantiate does the same job as copy, but it expands any FreeMarker tags within the files.  The final command is merge, which as you can guess merges the content of a source file in the template root with a project file.

The Recipe file starts out by loading compatibility libraries if they are needed:


<?xml version="1.0"?>
 <#if appCompat?has_content>
    mavenUrl=""/>  </#if>
 <#if !appCompat?has_content &&
 <#if !appCompat?has_content >


You might as well leave these lines in your own Recipe file. 

Next we need to create the manifest:

<merge from="AndroidManifest.xml.ftl"

The template manifest is stored in the root:


<manifest xmlns:android=
  "" >
  <activity android:name=
   <#if isNewProject>
   <#if buildApi gte 16 &&
        parentActivityClass != "">
  <#if parentActivityClass != "">
    <meta-data android:name=
   <#if isLauncher>
     <action android:name=
           "android.intent.action.MAIN" />
     <category android:name=
           "android.intent.category.LAUNCHER" />

 When this file is processed it is merged with any existing manifest file in such a way that only the new lines are added. What this means is that when the same template is used to create a new activity within an existing project only the new definition of the activity is added to the existing manifest.

Next we have to merge some resource files:

<merge from="res/values/strings.xml.ftl"
                         values/strings.xml" />
<merge from="res/values/dimens.xml.ftl"
                         values/dimens.xml" />
<merge from="res/values-w820dp/dimens.xml"
                  values-w820dp/dimens.xml" />

As before, the values are merged with any existing files so there is no duplication of definitions. 

The strings.xml file is where we put the "Hello world" message:

 <#if !isNewProject>
  <string name=
<string name="hello_world">Hello world!</string>

The dimens file just contains some basic layout properties;

<!-- Default screen margins,
     per the Android Design guidelines. -->
<dimen name=
<dimen name=


If you want to define additional string resources or layout parameters simply add them to the appropriate file. 

Our final task is to create the Java source and the xml layout files and this is just a matter of copying and expanding two template files:

<instantiate from=
                   layout/${layoutName}.xml" />
<instantiate from=
                 ${activityClass}.java" />


The layout file template simpleactivity.xml is very simple:

<RelativeLayout xmlns:android=
 android:layout_height="wrap_content" />

You can modify RelativeLayout to be any sort of container you want and you can remove the "hello world" message if you find it gets in the way. 

The Java template is:

package ${packageName};
 <#if appCompat?has_content>
import android.
 <#if appCompat?has_content>support.v7.</#if>
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
public class ${activityClass} extends Activity {
 protected void onCreate(Bundle savedInstanceState){

As before, you can modify this to include features you might want, but this the very minimum. 

Finally if you want to open some files in the editor ready for the programmer to start work you can:

<open file=
            ${activityClass}.java" />

This opens the jave souce code ready to work. 

This ends the recipe file and the entire template. 

Where Next

It would be wise not to spend too much time and effort on hand crafted templates as Android Studio is still changing, but you can see that simple templates are easy enough. If you study the documentation for FreeMarker and use the existing templates as examples, you should be able to cope until there is full documentation. 

The template presented here can be downloaded from the CodeBin. Note you have to register with I Programmer to access this.  

It also used in our ongoing series, Adventures with Android Studio


Related Articles:

Android ADT Template Format Document

Android Adventures With Android Studio 

  1. Getting Started With Android Studio
  2. The Activity And The UI
  3. Building The UI and a Calculator App
  4. Lifecycle and State
  5. Basic Controls And Events
  6. Spinners
  7. Pickers








or email your comment to:


To be informed about new articles on I Programmer, subscribe to the RSS feed, follow us on Google+Twitter, Linkedin or Facebook or sign up for our weekly newsletter.



QuickSort Exposed

QuickSort was published in July 1961 and so is celebrating its 60th birthday.  QuickSort is the most elegant of algorithms and every programmer should study it. It is also subtle and this often m [ ... ]

Setting Up Site-To-Site OpenVPN

Setting up a point-to-point VPN is relatively easy but site-to-site is much more complicated involving certificates and more IP addresses than you can count. Find out how to do it using OpenVPN.

Other Projects


Last Updated ( Tuesday, 21 January 2014 )