Android ADT Template Format Document |
Wednesday, 16 October 2013 |
This document describes the format and syntax for Android code templates. ADT Template Format
OverviewThis document describes the format and syntax for Android code templates. These templates provide starting points for entire projects (e.g. Although these templates were originally introduced in the ADT Plugin for Eclipse, the template format is designed for use by any IDE or command-line tool. Templates are customizable. Each template exposes several options (called parameters) that allow developers to customize the generated code. The most common workflow for using a template is as follows:
FreeMarkerTemplates make heavy use of FreeMarker, a Java templating engine used to enable things like control flows and variable substitutions inside files. It's similar to PHP, Django templates, etc. For those more acquainted with C/C++, think of it as a preprocessor language (i.e. By convention, any file in the template directory structure that is to be processed by FreeMarker should have the For more documentation on FreeMarker, see the docs. In particular, the reference on string operations. An example, templated version of an Android manifest, normally named <manifest xmlns:android="http://schemas.android.com/apk/res/android"> <application> <activity android:name=".${activityClass}" android:label="@string/title_${activityToLayout(activityClass)}"> <#if parentActivityClass != ""> <meta-data android:name="android.support.PARENT_ACTIVITY" android:value="${parentActivityClass}" /> </#if> <#if isLauncher> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </#if> </activity> </application> </manifest> In this example excerpt from the
Directory StructureA template is a directory containing a number of XML and FreeMarker files. The only two mandatory files are
More on the role of each of these files is discussed in the sections below. template.xmlEach template directory must contain a An example <!-- A template for a blank activity. Use template format version 3, as described in this document. --> <template format="3" revision="1" minApi="4" minBuildApi="11" name="New Blank Activity" description="Creates a new blank activity, with navigation."> <!-- Indicate that the Android Support Library (r8) should be added to the project, if it isn't already in the project. --> <dependency name="android-support-v4" revision="8" /> <category value="Activities" /> <!-- A string parameter; the value is available to FreeMarker processed files (.ftl files) as ${activityName}. --> <parameter id="activityName" name="Activity Name" type="string" constraints="class|unique|nonempty" suggest="${layoutToActivity(layoutName)}" default="MainActivity" help="The name of the activity class to create." /> <parameter id="layoutName" name="Layout Name" type="string" constraints="layout|unique" suggest="${activityToLayout(activityClass)}" default="main" help="The name of the layout to create for the activity" /> <parameter id="navType" name="Navigation Type" type="enum" default="none"> <option id="none" default="true">None</option> <option id="tabs" minApi="11">Tabs</option> <option id="pager" minApi="11">Swipe Views</option> <option id="dropdown" minApi="11">Dropdown</option> </parameter> <!-- 512x512 PNG thumbnails. --> <thumbs> <!-- Default thumbnail. --> <thumb>template_default.png</thumb> <!-- Attributes act as selectors based on chosen parameters. --> <thumb navType="tabs">template_tabs.png</thumb> <thumb navType="dropdown">template_dropdown.png</thumb> </thumbs> <!-- Optional global variables. --> <globals file="globals.xml.ftl" /> <!-- Required recipe (script) to run when instantiating the template. --> <execute file="recipe.xml.ftl" /> </template> Below is a listing of supported tags in <template>The template root element.
<dependency>Indicates that the template requires that a given library be present in the target project. If not present, the IDE will add the dependency to the project.
<category>The template type. This element is optional.
<parameter>Defines a user-customizable template parameter.
<option>For parameters of type
<thumb>Represents a thumbnail for the template. <thumbs> <thumb>template.png</thumb> <thumb navType="tabs">template_tabs.png</thumb> </thumbs> The template 'preview' thumbnail will show globals.xml.ftlThe optional globals XML file contains global variable definitions, for use in all FreeMarker processing jobs for this template. An example <globals> <global id="srcOut" value="src/${slashedPackageName(packageName)}" /> <global id="activityNameLower" value="${activityName?lower_case}" /> <global id="activityClass" value="${activityName}Activity" /> </globals> recipe.xml.ftlThe 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 running the source files through FreeMarker (the instantiate instruction), and ask ADT to open a file in Eclipse after the code has been generated (the open instruction). Note: The name of the recipe file is up to you, and is defined in Note: The global variables in An example <recipe> <!-- runs FreeMarker, then copies from [template-directory]/root/ to [output-directory]. --> <instantiate from="AndroidManifest.xml.ftl" /> <!-- automatically creates directories as needed --> <copy from="res/drawable-hdpi" /> <copy from="res/drawable-mdpi" /> <copy from="res/drawable-xhdpi" /> <copy from="res/values/dimens.xml" /> <copy from="res/values/styles.xml" /> <copy from="res/values-large/dimens.xml" /> <copy from="res/menu/main.xml" to="res/menu/${activityNameLower}.xml" /> <instantiate from="res/values/strings.xml.ftl" /> <!-- Decide which layout to add --> <#if navType?contains("pager")> <instantiate from="res/layout/activity_pager.xml.ftl" to="res/layout/activity_${activityNameLower}.xml" /> <#elseif navType == "tabs" || navType == "dropdown"> <copy from="res/layout/activity_fragment_container.xml" to="res/layout/activity_${activityNameLower}.xml" /> <#else> <copy from="res/layout/activity_simple.xml" to="res/layout/activity_${activityNameLower}.xml" /> </#if> <!-- Decide which activity code to add --> <#if navType == "none"> <instantiate from="src/app_package/SimpleActivity.java.ftl" to="${srcOut}/${activityClass}.java" /> <#elseif navType == "pager"> <instantiate from="src/app_package/PagerActivity.java.ftl" to="${srcOut}/${activityClass}.java" /> <#elseif navType == "tabs"> <instantiate from="src/app_package/TabsActivity.java.ftl" to="${srcOut}/${activityClass}.java" /> <#elseif navType == "dropdown"> <instantiate from="src/app_package/DropdownActivity.java.ftl" to="${srcOut}/${activityClass}.java" /> </#if> <!-- open the layout file when done --> <open file="res/layout/${activityNameLower}.xml" /> </recipe> The instructions below are supported: <copy>The only required argument is The default destination location is the same path under the output directory root (i.e. the location of the destination project). If the optional This argument works recursively, so if <instantiate>Same as <merge>This instruction will be used to merge the contents of a source file into an existing file in the project. The most common use case for this will be to add components to the <open>Instruct ADT to open the file created by the specified root/The actual template files (resources, Java sources, Android Manifest changes) should be placed in the One difference is that instead of placing source files in Extra Template FunctionsSeveral functions are available to FreeMarker expressions and files beyond the standard set of built-in FreeMarker functions. These are listed below. string activityToLayout(string)This function converts an activity class-like identifer string, such as Arguments
See alsostring camelCaseToUnderscore(string)This function converts a camel-case identifer string, such as Arguments
See alsostring escapeXmlAttribute(string)This function escapes a string, such as Arguments
See alsostring escapeXmlText(string)This function escapes a string, such as Arguments
See alsostring escapeXmlString(string)This function escapes a string, such as Arguments
See alsostring extractLetters(string)This function extracts all the letters from a string, effectively removing any punctuation and whitespace characters. Arguments
string classToResource(string)This function converts an Android class name, such as
Arguments
See alsostring layoutToActivity(string)This function converts a resource-friendly identifer string, such as Arguments
See alsostring slashedPackageName(string)This function converts a full Java package name to its corresponding directory path. For example, if the given argument is Arguments
string underscoreToCamelCase(string)This function converts an underscore-delimited string, such as Arguments
See alsoNotes for Template AuthorsTools metadataWhen creating activity layouts, make sure to include the activity name in the root view in the layout as part of the <TextView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:text="@string/hello_world" android:padding="@dimen/padding_medium" tools:context=".${activityClass}" /> As of ADT 20 we use this attribute in layouts to maintain a mapping to the active activity to use for a layout. (Yes, there can be more than one, but this attribute is showing the activity context you want to edit the layout as. For example, it will be used to look up a theme registration (which is per-activity rather than per-layout) in the manifest file, and we will use this for other features in the future—for example to preview the action bar, which also requires us to know the activity context. Powered by Gitiles |
Last Updated ( Friday, 01 September 2023 ) |