Mobile App Development: Tutorial How To Create Custom Android Code Templates

1 Star2 Stars3 Stars4 Stars5 Stars (11 votes, average: 5.00 out of 5)


This tutorial will show you how to create custom Android code templates of your own. Most Android developers would have used Android code templates at least once to start projects. But have you tried creating your own Android code templates? In this tutorial, we will show you how to create a template that can be used to generate a Android project from Android Studio IDE that contains Google Analytics configuration. (Google Analytics v4 for Android)

Tung Dao Xuan,, is the author of this article and he contributes to RobustTechHouse Blog


Video & Source Code

Source Code

You can download the source code at GoogleAnalyticApplication-2015-05-31.

Step by Step Video

Video Tutorial How To Create Custom Android Code Template


High Level View of Code Generation Process

Create custom Android code templates

If you are not familiar with the code generation process, please also check out the documentation of the full process here.


Steps To Create Custom Android Code Templates For Google Analytics

To build your own template, please ensure that:

  1. You understand the code generation process described above at a high level.
  2. You have some knowledge about FreeMarker
  3. You have some knowledge about Android IDE Template Format and you can see the documentation here.


Step 1: Create Google Analytic Application Folder

This is for the template directory you need. On Windows, place it in: path-to-your-installation-android-studio-folder\plugins\android\lib\templates\activities. Please change your path appropriately on Linux, Mac OS etc.

Then, create the root folder and other files inside your GoogleAnalyticApplication folder.

GoogleAnalytics Folder


Step 2: Create template.xml file

Each template directory must contain a template.xml file. This XML file contains the metadata about the template, including the name, description, category and user-visible parameters that the IDE will present as options to the user. The XML file also indicates the name of the recipe XML file which gets processed by FreeMarker, and the global variables XML file  if there are global variables besides the template parameter values that should be visible to all FreeMarker-processed files (.ftl files).

<?xml version="1.0"?>
	name="Google Analytics Application"
	description="Creates a new application that has already Google Analytics configuration.">

	<category value="Activity" />
	<formfactor value="Mobile" />

		name="Activity Name"
		help="The name of the activity class to create" />

		name="Layout Name"
		help="The name of the layout to create for the activity" />

		name="Class extends Application"
		help="The name of class that extends from Application" />

		name="Google Analytic ID"
		help="Id of Google Analytic" />

		name="Dispatch Period"
		help="Frequency of automatic dispatch in seconds. Defaults to 30 minutes (1800 seconds)."

		name="Auto Activity Tracking"
		help="If true, views (Activities) will be tracked automatically. false by default."
	<!-- 128x128 thumbnails relative to template.xml -->
		<!-- default thumbnail is required -->

	<globals file="globals.xml.ftl" />
	<execute file="recipe.xml.ftl" />


There are parameters: activityClass, layoutName, classApplication, googleAnalyticID, dispatchPeriod, autoActivityTracking which will be shown in popups when you create a project.

Create Custom Android Code Templates


 Step 3: Create globals.xml.ftl file

The optional globals XML file contains global variable definitions, for use in all FreeMarker processing jobs for this template.

<?xml version="1.0"?>
    <global id="manifestOut" value="${manifestDir}" />    
    <global id="srcOut" value="${srcDir}/${slashedPackageName(packageName)}" />
    <global id="resOut" value="${resDir}" />	    


Step 4: Create folder & files inside root folder

The root folder contains template source code & template resource to generate.

Root Folder

4a. Create AndroidManifest.xml.ftl

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=""

            android:value="@integer/google_play_services_version" />
            android:resource="@xml/analytics_global_config" />

                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />



There are parameters: packageName & classApplication that are declared in template.xml file above.


4b. Create

package ${packageName};



 * Created by TungDX on 5/29/2015.
public class ${classApplication} extends Application {
    private static GoogleAnalytics analytics;
    private static Tracker tracker;

    public void onCreate() {
        analytics = GoogleAnalytics.getInstance(this);
        tracker = analytics.newTracker("${googleAnalyticID}");

    public static GoogleAnalytics getGoogleAnalytics() {
        return analytics;

    public static Tracker getTracker() {
        return tracker;

There are parameters: packageName & googleAnalyticID which are parameters declared in template.xml file above.


4c. Create

package ${packageName}.activities;

import android.os.Bundle;

import ${packageName}.${classApplication};
import ${packageName}.R;

public class MainActivity extends ActionBarActivity {

    protected void onCreate(Bundle savedInstanceState) {

    protected void onStart() {

    protected void onStop() {

There are parameters: packageName & classApplication which are parameters declared in template.xml file above.


4d. Create activity_main.xml.ftl

<RelativeLayout xmlns:android=""

        android:layout_height="wrap_content" />



4e. Create strings.xml.ftl

    <#if !isNewProject>
    <string name="title_${activityToLayout(activityClass)}">${escapeXmlString(activityTitle)}</string>
	<string name="ready">Google Analytic is ready!</string>


4f. Create dimens.xml

    <!-- Default screen margins, per the Android Design guidelines. -->
    <dimen name="activity_horizontal_margin">16dp</dimen>
    <dimen name="activity_vertical_margin">16dp</dimen>


4g. Create recipe.xml.ftl file

The recipe XML file contains the individual instructions that should be executed when generating code from this template. For example, you can copy certain files or directories (the copy instruction), optionally run the source files through FreeMarker (the instantiate instruction), and ask the IDE to open a file after the code has been generated (the open instruction).


<?xml version="1.0"?>
	<dependency mavenUrl="${targetApi}.+" />
	<dependency mavenUrl="${targetApi}.+"/>
	<dependency mavenUrl="" />
    <instantiate from="AndroidManifest.xml.ftl"
             to="${escapeXmlAttribute(manifestOut)}/AndroidManifest.xml" />
	<instantiate from="src/app_package/" to="${escapeXmlAttribute(srcOut)}/${classApplication}.java"/>
	<instantiate from="src/app_package/activities/" to="${escapeXmlAttribute(srcOut)}/activities/${activityClass}.java"/>

	<instantiate from="res/xml/analytics_global_config.xml.ftl" to="${escapeXmlAttribute(resOut)}/xml/analytics_global_config.xml"/>
	<instantiate from="res/layout/activity_main.xml.ftl" to="${escapeXmlAttribute(resOut)}/layout/${layoutName}.xml"/>
	<copy from="res/values/dimens.xml" to="${escapeXmlAttribute(resOut)}/values/dimens.xml"/>
    <merge from="res/values/strings.xml.ftl"
             to="${escapeXmlAttribute(resOut)}/values/strings.xml" />   

    <open file="${escapeXmlAttribute(srcOut)}/activities/${activityClass}.java" />
    <open file="${escapeXmlAttribute(resOut)}/layout/${layoutName}.xml" />



4h. Check Dependencies

Check Dependencies


 4i. Check AndroidManifest.xml.ftl

<instantiate from="AndroidManifest.xml.ftl"          to="${escapeXmlAttribute(manifestOut)}/AndroidManifest.xml" />

Check AndroidManifest.xml.ftl


4j. Check

<instantiate from="src/app_package/" to="${escapeXmlAttribute(srcOut)}/${classApplication}.java"/>





4k. Check

<instantiate from="src/app_package/activities/" to="${escapeXmlAttribute(srcOut)}/activities/${activityClass}.java"/>

Check 1

Check 2


4l. Check analytics_global_config.xml.ftl

<instantiate from="res/xml/analytics_global_config.xml.ftl" to="${escapeXmlAttribute(resOut)}/xml/analytics_global_config.xml"/>

Check analytics_global.config.xml.ftl

4m. Generate files: activity_main.xml, dimens.xml, strings.xml from template to project.

<instantiate from="res/layout/activity_main.xml.ftl" to="${escapeXmlAttribute(resOut)}/layout/${layoutName}.xml"/>
<copy from="res/values/dimens.xml" to="${escapeXmlAttribute(resOut)}/values/dimens.xml"/>
<merge from="res/values/strings.xml.ftl" to="${escapeXmlAttribute(resOut)}/values/strings.xml" />


4n. Open files after the project is generated.

<open file="${escapeXmlAttribute(srcOut)}/activities/${activityClass}.java" />
<open file="${escapeXmlAttribute(resOut)}/layout/${layoutName}.xml" />


Try Generating A Project For Your Own Template

If your Android Studio IDE is running, please restart it to see your own template as shown in following image.

Create Custom Android Code Templates

If you encounter this error when you are generating a project from the template:

AssertionError: Wrong line separators: ‘…plication;\r\n\r\nimport…’ at offset 29:

Please check your line separators in all your files in the template. Please ensure that you use the correct line separators for your computer’s OS. Eg Window uses LF, Linux or MAC OS use CRLF.



And that’s it. It isn’t that difficult to create custom Android code templates and you will benefit from it in the long-term because you would adhere to the DRY (Don’t Repeat Yourself) principle and save yourself some time. Have fun coding!


RobustTechHouse is a leading tech company focusing on mobile app development, ECommerce, Mobile-Commerce and Financial Technology (FinTech) in Singapore. If you are interested to engage RobustTechHouse on your projects in Singapore, you can contact us here.




Video showing the steps

Source code can be downloaded at GoogleAnalyticApplication-2015-05-31

Custom Android Code Templates Slides

GoogleSource Templates

ADT Template Format Documentation by Roman Nurik




Also published on Medium.

Recommended Posts
Showing 5 comments
  • Afsal Ovingal

    How can i create a template project with 2 or more modules together at the time of new project creation its self.

  • Antonio Goncalves

    Hi, i have some throuble creating the template based on your doc,
    exception in plugin android support. moments ago. occurred once since last clear. unread. disable plugin
    Can you please help me with this issue. Appreciated. Thanks in advance.

  • Reply

    Nice post. I was checking continuously this blog and I am impressed! Extremely helpful info particularly the last part 🙂 I care for such info a lot. I was seeking this certain information for a very long time. Thank you and best of luck.

  • EllCorb

    Amoxicillin Natural Remedy [url=]cheap cialis online[/url] Cialis Sources In Canada

  • Reply

    I have figured out some essential things through your blog post post. One other thing I would like to state is that there are several games available on the market designed in particular for preschool age youngsters. They consist of pattern acknowledgement, colors, family pets, and designs. These typically focus on familiarization as an alternative to memorization. This will keep little children occupied without having a sensation like they are studying. Thanks

Leave a Comment

Contact Us

We look forward to your messages. Please drop us a note for any enquiries and we'll get back to you, asap.

Not readable? Change text. captcha txt
RobotGood Store