Home | History | Annotate | Download | only in docs
      1 <!DOCTYPE html>
      2 <html>
      3 <!--
      4   Copyright 2012 The Android Open Source Project
      5 
      6   Licensed under the Apache License, Version 2.0 (the "License");
      7   you may not use this file except in compliance with the License.
      8   You may obtain a copy of the License at
      9 
     10       http://www.apache.org/licenses/LICENSE-2.0
     11 
     12   Unless required by applicable law or agreed to in writing, software
     13   distributed under the License is distributed on an "AS IS" BASIS,
     14   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     15   See the License for the specific language governing permissions and
     16   limitations under the License.
     17   -->
     18 <head>
     19   <meta charset="utf-8">
     20   <title>Android IDE Template Format</title>
     21   <link rel="stylesheet" href="cssreset-min.css">
     22   <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:regular,medium,thin,italic,mediumitalic,bold;Inconsolata" title="roboto">
     23   <link rel="stylesheet" href="prettify.css">
     24   <link rel="stylesheet" href="default.css">
     25   <script src="jquery-1.8.0.min.js"></script>
     26   <script src="prettify.js"></script>
     27   <script src="default.js"></script>
     28 </head>
     29 <body>
     30   
     31 <nav>
     32 
     33 </nav>
     34 
     35 <div id="page-content">
     36 
     37 <h1>Android IDE Template Format</h1>
     38 <br>
     39 <dl style="margin:0">
     40   <dt>Format Version</dt>
     41   <dd style="margin:0">4</dd>
     42 
     43   <dt>Last Updated</dt>
     44   <dd style="margin:0">1/30/2014</dd>
     45 </dl>
     46 
     47 
     48 
     49 
     50 
     51 <h2>Overview</h2>
     52 
     53 <p>This document describes the format and syntax for Android code templates. These templates provide starting points for entire projects (e.g. <code>NewAndroidApplication</code>) or application components such as activities (e.g. <code>BlankActivity</code>).</p>
     54 
     55 <p>Although these templates were originally introduced in the <a href="http://developer.android.com/tools/sdk/eclipse-adt.html">ADT Plugin</a> for Eclipse, the template format is designed for use by any IDE or command-line tool.</p>
     56 
     57 <p>Templates are customizable. Each template exposes several options (called parameters) that allow developers to customize the generated code. The most common workflow for <em>using</em> a template is as follows:</p>
     58 
     59 <ol>
     60   <li>Choose a template.</li>
     61   <li>Populate template options (parameters).</li>
     62   <li>Preview and then execute the additions/changes to your project.</li>
     63 </ol>
     64 
     65 <h3>FreeMarker</h3>
     66 
     67 <p>Templates make heavy use of <a href="http://freemarker.sourceforge.net/">FreeMarker</a>, 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 <a href="http://en.wikipedia.org/wiki/C_preprocessor">preprocessor</a> language (i.e. <code>#ifdef</code>).</p>
     68 
     69 <p>By convention, any file in the template directory structure that is to be processed by FreeMarker should have the <code>.ftl</code> file extension. So if one of your source files is <code>MyActivity.java</code>, and it contains FreeMarker instructions, it should be named something like <code>MyActivity.java.ftl</code>.</p>
     70 
     71 <p>For more documentation on FreeMarker, see the <a href="http://freemarker.sourceforge.net/docs/index.html">docs</a>. In particular, the <a href="http://freemarker.sourceforge.net/docs/ref_builtins_string.html">reference on string operations</a>.</p>
     72 
     73 <p>An example, templated version of an Android manifest, normally named <code>AndroidManifest.xml.ftl</code> is shown below.</p>
     74 
     75 <pre class="prettyprint lang-xml">
     76 &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"&gt;
     77     &lt;application&gt;
     78         &lt;activity android:name="<strong>${packageName}.${activityClass}</strong>"
     79             android:parentActivityName="<strong>${parentActivityClass}</strong>"
     80             android:label="@string/title_<strong>${activityToLayout(activityClass)}</strong>">
     81             <strong>&lt;#if parentActivityClass != ""&gt;</strong>
     82             &lt;meta-data android:name="android.support.PARENT_ACTIVITY"
     83                 android:value="<strong>${parentActivityClass}</strong>" /&gt;
     84             <strong>&lt;/#if&gt;</strong>
     85             <strong>&lt;#if isLauncher&gt;</strong>
     86             &lt;intent-filter&gt;
     87                 &lt;action android:name="android.intent.action.MAIN" /&gt;
     88                 &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
     89             &lt;/intent-filter&gt;
     90             <strong>&lt;/#if&gt;</strong>
     91         &lt;/activity&gt;
     92     &lt;/application&gt;
     93 &lt;/manifest&gt;
     94 </pre>
     95 
     96 <p>In this example excerpt from the <code>BlankActivity</code> template:</p>
     97 
     98 <ul>
     99   <li>The expression <code>${activityClass}</code> is bound to the value of the 'Activity Class' template parameter.</li>
    100   <li>The expression <code>${activityToLayout(activityClass)}</code> uses the <code>activityToLayout</code> method built into the template engine to convert an activity class such as <code>MyFooActivity</code> to <code>activity_my_foo</code>.</li>
    101   <li>The <code>isLauncher</code> boolean variable and <code>parentActivityClass</code> string variables are bound to the values of the 'Launcher Activity' and 'Hierarchical Parent' template parameter, respectively.</li>
    102 </ul>
    103 
    104 
    105 
    106 
    107 
    108 <h2>Directory Structure</h2>
    109 
    110 <p>A template is a directory containing a number of XML and FreeMarker files. The only two mandatory files are <code>template.xml</code> and <code>recipe.xml.ftl</code>. Template source files (PNG files, templated Java and XML files, etc.) belong in a <code>root/</code> subdirectory. An example directory structure for a template is below:</p>
    111 
    112 <ul>
    113   <li><strong>MyTemplate/</strong> <span class="dim">&mdash; Root directory</span><ul>
    114     <li><a href="#toc_templatexml">template.xml</a> <span class="dim">&mdash; Metadata (description, parameters, etc.)</span></li>
    115     <li><a href="#toc_recipexmlftl">recipe.xml.ftl</a> <span class="dim">&mdash; Instructions/script (files to copy, etc.)</span></li>
    116     <li><a href="#toc_globalsxmlftl">globals.xml.ftl </a><span class="dim">&mdash; Optional global variables</span></li>
    117     <li>template.png <span class="dim">&mdash; Default template thumbnail</span></li>
    118     <li>template_foo.png <span class="dim">&mdash; Thumbnail when option 'foo' is selected</span></li>
    119     <li>template_bar.png</li>
    120     <li><strong><a href="#toc_root">root/</a></strong> <span class="dim">&mdash; Source files (which get processed/copied/merged with the output project)</span><ul>
    121       <li>AndroidManifest.xml.ftl</li>
    122       <li><strong>res/</strong> <ul>
    123         <li>&hellip;</li>
    124       </ul></li>
    125       <li><strong>src/</strong> <ul>
    126         <li><strong>app_package/</strong> <ul>
    127           <li>MyActivity.java.ftl</li>
    128         </ul></li>
    129       </ul></li>
    130     </ul></li>
    131   </ul>
    132 </li>
    133 </ul>
    134 <br>
    135 
    136 <p>More on the role of each of these files is discussed in the sections below.</p>
    137 
    138 <h3>template.xml</h3>
    139 
    140 <p>Each template directory must contain a <code>template.xml</code> file. This XML file contains 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 <a href="#toc_recipexmlftl">recipe XML file</a> (which gets processed by FreeMarker), and the <a href="#toc_globalsxmlftl">global variables XML file</a> if there are global variables besides the template parameter values that should be visible to all FreeMarker-processed files (<code>.ftl</code> files).</p>
    141 
    142 <p>An example <code>template.xml</code> is shown below.</p>
    143 
    144 <pre class="prettyprint lang-xml">
    145 &lt;!-- A template for a blank activity. Use template format
    146      version 4, as described in this document. -->
    147 &lt;template
    148     format="4"
    149     revision="2"
    150     minApi="7"
    151     minBuildApi="16"
    152     name="Blank Activity"
    153     description="Creates a new blank activity, with navigation."&gt;
    154 
    155     &lt;!-- A string parameter; the value is available to FreeMarker
    156          processed files (.ftl files) as ${activityName}. -->
    157     &lt;parameter
    158         id="activityClass"
    159         name="Activity Name"
    160         type="string"
    161         constraints="class|unique|nonempty"
    162         suggest="${layoutToActivity(layoutName)}"
    163         default="MainActivity"
    164         help="The name of the activity class to create." /&gt;
    165 
    166     &lt;parameter
    167         id="layoutName"
    168         name="Layout Name"
    169         type="string"
    170         constraints="layout|unique|nonempty"
    171         suggest="${activityToLayout(activityClass)}"
    172         default="activity_main"
    173         help="The name of the layout to create for the activity" /&gt;
    174 
    175     &lt;parameter
    176         id="navType"
    177         name="Navigation Type"
    178         type="enum"
    179         default="none"
    180         help="The type of navigation to use for the activity"&gt;
    181         &lt;option id="none"&gt;None&lt;/option&gt;
    182         &lt;option id="tabs" minApi="11"&gt;Tabs&lt;/option&gt;
    183         &lt;option id="pager" minApi="11"&gt;Swipe Views&lt;/option&gt;
    184         &lt;option id="dropdown" minApi="11"&gt;Dropdown&lt;/option&gt;
    185     &lt;/parameter&gt;
    186 
    187     &lt;parameter
    188         id="fragmentName"
    189         name="Fragment Name"
    190         type="string"
    191         constraints="class|unique|nonempty"
    192         default="MainFragment"
    193         visibility="navType != 'none'"
    194         help="The name of the fragment class to create" /&gt;
    195 
    196     &lt;!-- 512x512 PNG thumbnails. --&gt;
    197     &lt;thumbs&gt;
    198         &lt;!-- Default thumbnail. --&gt;
    199         &lt;thumb&gt;template_default.png&lt;/thumb&gt;
    200         &lt;!-- Attributes act as selectors based on chosen parameters. --&gt;
    201         &lt;thumb navType="tabs"&gt;template_tabs.png&lt;/thumb&gt;
    202         &lt;thumb navType="dropdown"&gt;template_dropdown.png&lt;/thumb&gt;
    203     &lt;/thumbs&gt;
    204 
    205     &lt;!-- Optional global variables. --&gt;
    206     &lt;globals file="globals.xml.ftl" /&gt;
    207 
    208     &lt;!-- Required recipe (script) to run when instantiating
    209          the template. --&gt;
    210     &lt;execute file="recipe.xml.ftl" /&gt;
    211 &lt;/template&gt;
    212 </pre>
    213 
    214 <p>Below is a listing of supported tags in <code>template.xml</code>.</p>
    215 
    216 <h4 class="includetoc">&lt;template&gt;</h4>
    217 
    218 <p>The template root element.</p>
    219 
    220 <dl>
    221   <dt><code>format</code></dt>
    222   <dd>The template format version that this template adheres to. Should be <code>4</code>.</dd>
    223 
    224   <dt><code>revision</code></dt>
    225   <dd>Optional. The version of this template (which you can increment when updating the template), as an integer.</dd>
    226 
    227   <dt><code>name</code></dt>
    228   <dd>The template's display name.</dd>
    229 
    230   <dt><code>description</code></dt>
    231   <dd>The template's description.</dd>
    232 
    233   <dt><code>minApi</code></dt>
    234   <dd>Optional. The minimum API level required for this template. The IDE will ensure that the target project has a <code>minSdkVersion</code> no lower than this value before instantiating the template.</dd>
    235 
    236   <dt><code>minBuildApi</code></dt>
    237   <dd>Optional. The minimum build target (expressed as an API level) required for this template. The IDE will ensure that the target project is targeting an API level greater than or equal to this value before instantiating the template. This ensures that the template can safely use newer APIs (optionally guarded by runtime API level checks) without introducing compile-time errors into the target project.</dd>
    238 </dl>
    239 
    240 <div class="deprecated">
    241 <h4 class="includetoc">&lt;dependency&gt;</h4>
    242 
    243 <p>This tag is deprecated for use in <code>template.xml</code>. Use <a href="#toc_recipe_dependency"><code>&lt;dependency&gt;</code></a> in <code>recipe.xml.ftl</code> instead.</p>
    244 
    245 <p>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.</p>
    246 
    247 <dl>
    248   <dt><code>name</code></dt>
    249   <dd>The name of the library. Currently accepted values are:<ul>
    250     <li><code>android-support-v4</code></li>
    251     <li><code>android-support-v13</code></li>
    252   </ul></dd>
    253 
    254   <dt><code>revision</code></dt>
    255   <dd>The minimum revision of the library required by this template.</dd>
    256 </dl>
    257 </div>
    258 
    259 <div class="deprecated">
    260 <h4 class="includetoc">&lt;category&gt;</h4>
    261 
    262 <p>The template type. This element is optional.</p>
    263 
    264 <dl>
    265   <dt><code>value</code></dt>
    266   <dd>The template type. Should be one of the following values: <ul>
    267     <li><code>Applications</code></li>
    268     <li><code>Activities</code></li>
    269     <li><code>UI Components</code></li>
    270   </ul></dd>
    271 </dl>
    272 </div>
    273 
    274 <h4 class="includetoc">&lt;parameter&gt;</h4>
    275 
    276 <p>Defines a user-customizable template parameter.</p>
    277 
    278 <dl>
    279   <dt><code>id</code></dt>
    280   <dd>The identifier representing this variable, made available as a global variable in FreeMarker files. If the identifier is <code>foo</code>, the parameter value will be available in FreeMarker files as <code>${foo}</code>.</dd>
    281 
    282   <dt><code>name</code></dt>
    283   <dd>The display name of the template parameter.</dd>
    284 
    285   <dt><code>type</code></dt>
    286   <dd>The data type of the parameter. Either <code>string</code>, <code>boolean</code>, <code>enum</code>, or <code>separator</code>.</dd>
    287 
    288   <dt><code>constraints</code></dt>
    289   <dd>Optional. Constraints to impose on the parameter's value. Constraints can be combined using <code>|</code>. Valid constraint types are: <ul>
    290     <li><code>nonempty</code> &mdash; the value must not be empty</li>
    291     <li><code>apilevel</code> &mdash; the value should represent a numeric API level</li>
    292     <li><code>package</code> &mdash; the value should represent a valid Java package name</li>
    293     <li><code>app_package</code> &mdash; the value should represent a valid Android app package name</li>
    294     <li><code>module</code> &mdash; the value should represent a valid Module name</li>
    295     <li><code>class</code> &mdash; the value should represent a valid Java class name</li>
    296     <li><code>activity</code> &mdash; the value should represent a fully-qualified activity class name</li>
    297     <li><code>layout</code> &mdash; the value should represent a valid layout resource name</li>
    298     <li><code>drawable</code> &mdash; the value should represent a valid drawable resource name</li>
    299     <li><code>string</code> &mdash; the value should represent a valid string resource name</li>
    300     <li><code>id</code> &mdash; the value should represent a valid id resource name</li>
    301     <li><code>unique</code> &mdash; the value must be unique; this constraint only makes sense when other constraints are specified, such as <code>layout</code>, which would mean that the value should not represent an existing layout resource name</li>
    302     <li><code>exists</code> &mdash; the value must already exist; this constraint only makes sense when other constraints are specified, such as <code>layout</code>, which would mean that the value should represent an existing layout resource name</li>
    303   </ul></dd>
    304 
    305   <dt><code>suggest</code></dt>
    306   <dd>Optional. A FreeMarker expression representing the auto-suggested parameter value (a 'dynamic default'). When the user modifies other parameter values, and if this parameter's value has not been changed from its default, then the value changes to the result of this expression. This may seem to be circular since parameters can <code>suggest</code> against each other's values, but these expressions are only updated for non-edited values, so this approach lets the user edit either parameter value, and the other will automatically be updated to a reasonable default.</dd>
    307 
    308   <dt><code>default</code></dt>
    309   <dd>Optional. The default value for this parameter.</dd>
    310 
    311   <dt><code>visibility</code></dt>
    312   <dd>Optional. A FreeMarker expression that determines whether this parameter should be visible. The expression should evaluate to a boolean value (i.e. true or false).</dd>
    313 
    314   <dt><code>help</code></dt>
    315   <dd>Optional. The help string to display to the user for this parameter.</dd>
    316 
    317 </dl>
    318 
    319 <h4 class="includetoc">&lt;option&gt;</h4>
    320 
    321 <p>For parameters of type <code>enum</code>, represents a choice for the value.</p>
    322 
    323 <dl>
    324   <dt><code>id</code></dt>
    325   <dd>The parameter value to set if this option is chosen.</dd>
    326 
    327   <dt><code>minApi</code></dt>
    328   <dd>Optional. The minimum API level required if this option is chosen. The IDE will ensure that the target project has a <code>minSdkVersion</code> no lower than this value before instantiating the template.</dd>
    329 
    330   <dt><code>[text]</code></dt>
    331   <dd>The text content of this element represents the display value of the choice.</dd>
    332 </dl>
    333 
    334 <h4 class="includetoc">&lt;thumb&gt;</h4>
    335 
    336 <p>Represents a thumbnail for the template. <code>&lt;thumb&gt;</code> elements should be contained inside a <code>&lt;thumbs&gt;</code> element. The text contents of this element represent the path to the thumbnail. If this element has any attributes, they will be treated as selectors for parameter values. For example, if there are two thumbnails:</p>
    337 
    338 <pre class="prettyprint lang-xml">
    339 &lt;thumbs&gt;
    340   &lt;thumb&gt;template.png&lt;/thumb&gt;
    341   &lt;thumb navType="tabs"&gt;template_tabs.png&lt;/thumb&gt;
    342 &lt;/thumbs&gt;
    343 </pre>
    344 
    345 <p>The template 'preview' thumbnail will show <code>template_tabs.png</code> if the value of the <code>navType</code> template parameter is <code>tabs</code> and <code>template.png</code> otherwise.</p>
    346 
    347 <h4 class="includetoc">&lt;icons&gt;</h4>
    348 
    349 <p>States that the template would like the Asset Studio icon creation tool of the given type to run, and save the output icons with the given name.</p>
    350 
    351 <dl>
    352   <dt><code>type</code></dt>
    353   <dd>The type of icon wizard to create. Valid values are <code>notification</code>, <code>actionbar</code>, <code>launcher</code>.</dd>
    354 
    355   <dt><code>name</code></dt>
    356   <dd>The base icon name to output, e.g. <code>ic_stat_my_notification</code>.</dd>
    357 </dl>
    358 
    359 <h3>globals.xml.ftl</h3>
    360 
    361 <p>The optional globals XML file contains global variable definitions, for use in all FreeMarker processing jobs for this template.</p>
    362 
    363 <p>An example <code>globals.xml.ftl</code> is shown below.</p>
    364 
    365 <pre class="prettyprint lang-xml">
    366 &lt;globals&gt;
    367     &lt;global id="srcOut"
    368             value="src/${slashedPackageName(packageName)}" /&gt;
    369     &lt;global id="activityNameLower"
    370             value="${activityName?lower_case}" /&gt;
    371     &lt;global id="activityClass"
    372             value="${activityName}Activity" /&gt;
    373 &lt;/globals&gt;
    374 </pre>
    375 
    376 <h3>recipe.xml.ftl</h3>
    377 
    378 <p>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 running 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).</p>
    379 
    380 <p class="note"><strong>Note:</strong> The name of the recipe file is up to you, and is defined in <code>template.xml</code>. By convention, however, it's best to call it <code>recipe.xml.ftl</code>.</p>
    381 
    382 <p class="note"><strong>Note:</strong> The global variables in <code>globals.xml.ftl</code> are available for use in <code>recipe.xml.ftl</code>.</p>
    383 
    384 <p>An example <code>recipe.xml.ftl</code> is shown below.</p>
    385 
    386 <pre class="prettyprint lang-xml">
    387 &lt;recipe&gt;
    388     &lt;#if appCompat?has_content&gt;
    389     &lt;dependency mavenUrl="com.android.support:appcompat-v7:+"/&gt;
    390     &lt;/#if&gt;
    391 
    392     &lt;!-- runs FreeMarker, then copies from
    393          [template-directory]/root/ to [output-directory],
    394          automatically creating directories as needed. --&gt;
    395     &lt;merge from="AndroidManifest.xml.ftl"
    396              to="${escapeXmlAttribute(manifestDir)}/AndroidManifest.xml" /&gt;
    397 
    398     &lt;!-- simply copy file, don't run FreeMarker --&gt;
    399     &lt;copy from="res/drawable-mdpi"
    400             to="${escapeXmlAttribute(resDir)}/res/drawable-mdpi" /&gt;
    401     &lt;copy from="res/drawable-hdpi"
    402             to="${escapeXmlAttribute(resDir)}/res/drawable-hdpi" /&gt;
    403     &lt;copy from="res/drawable-xhdpi"
    404             to="${escapeXmlAttribute(resDir)}/res/drawable-xhdpi" /&gt;
    405     &lt;copy from="res/drawable-xxhdpi"
    406             to="${escapeXmlAttribute(resDir)}/res/drawable-xxhdpi" /&gt;
    407     &lt;copy from="res/menu/main.xml"
    408             to="${escapeXmlAttribute(resDir)}/res/menu/${activityNameLower}.xml" /&gt;
    409 
    410     &lt;!-- run FreeMarker and then merge with existing files --&gt;
    411     &lt;merge from="res/values/dimens.xml"
    412              to="${escapeXmlAttribute(resDir)}/res/values/dimens.xml" /&gt;
    413     &lt;merge from="res/values-large/dimens.xml"
    414              to="${escapeXmlAttribute(resDir)}/res/values-large/dimens.xml" /&gt;
    415     &lt;merge from="res/values/styles.xml"
    416              to="${escapeXmlAttribute(resDir)}/res/values/styles.xml" /&gt;
    417     &lt;merge from="res/values/strings.xml.ftl"
    418              to="${escapeXmlAttribute(resDir)}/res/values/strings.xml" /&gt;
    419 
    420     &lt;!-- Decide which layout to add --&gt;
    421     &lt;#if navType?contains("pager")&gt;
    422         &lt;instantiate
    423             from="${escapeXmlAttribute(resDir)}/res/layout/activity_pager.xml.ftl"
    424               to="${escapeXmlAttribute(resDir)}/res/layout/activity_${activityNameLower}.xml" /&gt;
    425     &lt;#elseif navType == "tabs" || navType == "dropdown"&gt;
    426         &lt;copy from="${escapeXmlAttribute(resDir)}/res/layout/activity_fragment_container.xml"
    427                 to="${escapeXmlAttribute(resDir)}/res/layout/activity_${activityNameLower}.xml" /&gt;
    428     &lt;#else&gt;
    429         &lt;copy from="${escapeXmlAttribute(resDir)}/res/layout/activity_simple.xml"
    430                 to="${escapeXmlAttribute(resDir)}/res/layout/activity_${activityNameLower}.xml" /&gt;
    431     &lt;/#if&gt;
    432 
    433     &lt;!-- Decide which activity code to add --&gt;
    434     &lt;#if navType == "none"&gt;
    435         &lt;instantiate from="src/app_package/SimpleActivity.java.ftl"
    436                        to="${escapeXmlAttribute(srcOut)}/${activityClass}.java" /&gt;
    437     &lt;#elseif navType == "pager"&gt;
    438         &lt;instantiate from="src/app_package/PagerActivity.java.ftl"
    439                        to="${escapeXmlAttribute(srcOut)}/${activityClass}.java" /&gt;
    440     &lt;#elseif navType == "tabs"&gt;
    441         &lt;instantiate from="src/app_package/TabsActivity.java.ftl"
    442                        to="${escapeXmlAttribute(srcOut)}/${activityClass}.java" /&gt;
    443     &lt;#elseif navType == "dropdown"&gt;
    444         &lt;instantiate from="src/app_package/DropdownActivity.java.ftl"
    445                        to="${escapeXmlAttribute(srcOut)}/${activityClass}.java" /&gt;
    446     &lt;/#if&gt;
    447 
    448     &lt;!-- open the layout file and Java class when done --&gt;
    449     &lt;open file="${escapeXmlAttribute(resDir)}/res/layout/${activityNameLower}.xml" /&gt;
    450     &lt;open file="${escapeXmlAttribute(srcOut)}/${activityClass}.java" /&gt;
    451 &lt;/recipe&gt;
    452 </pre>
    453 
    454 <p>The instructions below are supported:</p>
    455 
    456 <h4 class="includetoc" data-tocid="recipe_dependency">&lt;dependency&gt;</h4>
    457 
    458 <p>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.</p>
    459 
    460 <dl>
    461   <dt><code>mavenUrl</code></dt>
    462   <dd>The maven coordinates of the library. For example,
    463     <code>com.android.support:appcompat-v7:+</code></dd>
    464 </dl>
    465 
    466 <h4 class="includetoc">&lt;copy&gt;</h4>
    467 
    468 <p>The only required argument is <code>from</code> which specifies the location of the source files to copy under the <code>root/</code> directory. All necessary ancestor directories are automatically created if needed.</p>
    469 
    470 <p>The default destination location is the same path under the output directory root (i.e. the location of the destination project). If the optional <code>to</code> argument is provided, this specifies the output directory. Note that if the from path ends with <code>.ftl</code>, it will automatically be stripped. For example <code>&lt;instantiate from="res/values/strings.xml.ftl" /&gt;</code> is adequate; this will create a file named <code>strings.xml</code>, not <code>strings.xml.ftl</code>.</p>
    471 
    472 <p>This argument works recursively, so if <code>from</code> is a directory, that directory is recursively copied.</p>
    473 
    474 <h4 class="includetoc">&lt;instantiate&gt;</h4>
    475 
    476 <p>Same as <code>&lt;copy&gt;</code>, but each source file is first run through FreeMarker.</p>
    477 
    478 <h4 class="includetoc">&lt;merge&gt;</h4>
    479 
    480 <p>This instruction will run the source file through FreeMarker and then merge the contents of the output into an existing file in the project, or create a new file. The most common use case for this is to add components to the <code>AndroidManifest.xml</code> file of the destination project, or to merge resources such as strings into an existing <code>strings.xml</code> file.</p>
    481 
    482 <h4 class="includetoc">&lt;open&gt;</h4>
    483 
    484 <p>Instruct the IDE to open the file created by the specified <code>file</code> argument after code generation is complete.</p>
    485 
    486 <h4 class="includetoc">&lt;mkdir&gt;</h4>
    487 
    488 <p>Ensures the directory provided in the <code>at</code> argument exists.</p>
    489 
    490 <h3>root/</h3>
    491 
    492 <p>The actual template files (resources, Java sources, Android Manifest changes) should be placed in the <code>root/</code> directory, in a directory structure that roughly resembles what the output directory structure should look like.</p>
    493 
    494 <p>One difference is that instead of placing source files in <code>src/com/google/...</code> you can just use a naming convention like <code>src/app_package/</code> to indicate that files under this directory will be placed in the destination project's source file package root.</p>
    495 
    496 
    497 
    498 
    499 <h2>Built-in Template Functions</h2>
    500 
    501 <p>Several functions are available to FreeMarker expressions and files beyond the standard set of built-in FreeMarker functions. These are listed below.</p>
    502 
    503 <h3 data-toctitle="activityToLayout">string <em>activityToLayout</em>(string)</h3>
    504 
    505 <p>This function converts an activity class-like identifer string, such as <code>FooActivity</code>, to a corresponding resource-friendly identifier string, such as <code>activity_foo</code>.</p>
    506 
    507 <h4>Arguments</h4>
    508 <dl>
    509   <dt><code>activityClass</code></dt>
    510   <dd>The activity class name, e.g. <code>FooActivity</code> to reformat.</dd>
    511 </dl>
    512 
    513 <h4>See also</h4>
    514 <p><a href="#toc_layouttoactivity"><code>layoutToActivity</code></a></p>
    515 
    516 <h3 data-toctitle="camelCaseToUnderscore">string <em>camelCaseToUnderscore</em>(string)</h3>
    517 
    518 <p>This function converts a camel-case identifer string, such as <code>FooBar</code>, to its corresponding underscore-separated identifier string, such as <code>foo_bar</code>.</p>
    519 
    520 <h4>Arguments</h4>
    521 <dl>
    522   <dt><code>camelStr</code></dt>
    523   <dd>The camel-case string, e.g. <code>FooBar</code> to convert to an underscore-delimited string.</dd>
    524 </dl>
    525 
    526 <h4>See also</h4>
    527 <p><a href="#toc_underscoretocamelcase"><code>underscoreToCamelCase</code></a></p>
    528 
    529 <h3 data-toctitle="escapePropertyValue">string <em>escapePropertyValue</em>(string)</h3>
    530 
    531 <p>This function escapes a string, such as <code>foo=bar</code> such that it is suitable to be inserted in a Java <code>.properties</code> file as a property value, such as <code>foo\=bar</code>.</p>
    532 
    533 <h4>Arguments</h4>
    534 <dl>
    535   <dt><code>str</code></dt>
    536   <dd>The string, e.g. <code>foo=bar</code> to escape to a proper property value.</dd>
    537 </dl>
    538 
    539 <h3 data-toctitle="escapeXmlAttribute">string <em>escapeXmlAttribute</em>(string)</h3>
    540 
    541 <p>This function escapes a string, such as <code>Android's</code> such that it can be used as an XML attribute value: <code>Android&amp;apos;s</code>. In particular, it will escape ', ", &lt; and &amp;.</p>
    542 
    543 <h4>Arguments</h4>
    544 <dl>
    545   <dt><code>str</code></dt>
    546   <dd>The string to be escaped.</dd>
    547 </dl>
    548 
    549 <h4>See also</h4>
    550 <p><a href="#toc_escapexmltext"><code>escapeXmlText</code></a></p>
    551 <p><a href="#toc_escapexmlstring"><code>escapeXmlString</code></a></p>
    552 
    553 <h3 data-toctitle="escapeXmlText">string <em>escapeXmlText</em>(string)</h3>
    554 
    555 <p>This function escapes a string, such as <code>A &amp; B's</code> such that it can be used as XML text. This means it will escape &lt; and &gt;, but unlike <a href="#toc_escapexmlattribute"><code>escapeXmlAttribute</code></a> it will <b>not</b> escape ' and ". In the preceeding example, it will escape the string to <code>A &amp;amp; B\s</code>. Note that if you plan to use the XML text as the value for a &lt;string&gt; resource value, you should consider using <a href="#toc_escapexmlstring"><code>escapeXmlString</code></a> instead, since it performs additional escapes necessary for string resources.</p>
    556 
    557 <h4>Arguments</h4>
    558 <dl>
    559   <dt><code>str</code></dt>
    560   <dd>The string to escape to proper XML text.</dd>
    561 </dl>
    562 
    563 <h4>See also</h4>
    564 <p><a href="#toc_escapexmlattribute"><code>escapeXmlAttribute</code></a></p>
    565 <p><a href="#toc_escapexmlstring"><code>escapeXmlString</code></a></p>
    566 
    567 <h3 data-toctitle="escapeXmlString">string <em>escapeXmlString</em>(string)</h3>
    568 
    569 <p>This function escapes a string, such as <code>A &amp; B's</code> such that it is suitable to be inserted in a string resource file as XML text, such as <code>A &amp;amp; B\s</code>. In addition to escaping XML characters like &lt; and &amp;, it also performs additional Android specific escapes, such as escaping apostrophes with a backslash, and so on.</p>
    570 
    571 <h4>Arguments</h4>
    572 <dl>
    573   <dt><code>str</code></dt>
    574   <dd>The string, e.g. <code>Activity's Title</code> to escape to a proper resource XML value.</dd>
    575 </dl>
    576 
    577 <h4>See also</h4>
    578 <p><a href="#toc_escapexmlattribute"><code>escapeXmlAttribute</code></a></p>
    579 <p><a href="#toc_escapexmltext"><code>escapeXmlText</code></a></p>
    580 
    581 <h3 data-toctitle="extractLetters">string <em>extractLetters</em>(string)</h3>
    582 
    583 <p>This function extracts all the letters from a string, effectively removing any punctuation and whitespace characters.</p>
    584 
    585 <h4>Arguments</h4>
    586 <dl>
    587   <dt><code>str</code></dt>
    588   <dd>The string to extract letters from</dd>
    589 </dl>
    590 
    591 <h3 data-toctitle="classToResource">string <em>classToResource</em>(string)</h3>
    592 
    593 <p>This function converts an Android class name, such as <code>FooActivity</code> or <code>FooFragment</code>, to a corresponding resource-friendly identifier string, such as <code>foo</code>, stripping the 'Activity' or 'Fragment' suffix. Currently stripped suffixes are listed below.</p>
    594 
    595 <ul>
    596   <li>Activity</li>
    597   <li>Fragment</li>
    598   <li>Provider</li>
    599   <li>Service</li>
    600 </ul>
    601 
    602 <h4>Arguments</h4>
    603 <dl>
    604   <dt><code>className</code></dt>
    605   <dd>The class name, e.g. <code>FooActivity</code> to reformat as an underscore-delimited string with suffixes removed.</dd>
    606 </dl>
    607 
    608 <h4>See also</h4>
    609 <p><a href="#toc_activitytolayout"><code>activityToLayout</code></a></p>
    610 
    611 <h3 data-toctitle="layoutToActivity">string <em>layoutToActivity</em>(string)</h3>
    612 
    613 <p>This function converts a resource-friendly identifer string, such as <code>activity_foo</code>, to a corresponding Java class-friendly identifier string, such as <code>FooActivity</code>.</p>
    614 
    615 <h4>Arguments</h4>
    616 <dl>
    617   <dt><code>resourceName</code></dt>
    618   <dd>The resource name, e.g. <code>activity_foo</code> to reformat.</dd>
    619 </dl>
    620 
    621 <h4>See also</h4>
    622 <p><a href="#toc_activitytolayout"><code>activityToLayout</code></a></p>
    623 
    624 <h3 data-toctitle="slashedPackageName">string <em>slashedPackageName</em>(string)</h3>
    625 
    626 <p>This function converts a full Java package name to its corresponding directory path. For example, if the given argument is <code>com.example.foo</code>, the return value will be <code>com/example/foo</code>.</p>
    627 
    628 <h4>Arguments</h4>
    629 <dl>
    630   <dt><code>packageName</code></dt>
    631   <dd>The package name to reformat, e.g. <code>com.example.foo</code>.</dd>
    632 </dl>
    633 
    634 <h3 data-toctitle="underscoreToCamelCase">string <em>underscoreToCamelCase</em>(string)</h3>
    635 
    636 <p>This function converts an underscore-delimited string, such as <code>foo_bar</code>, to its corresponding camel-case string, such as <code>FooBar</code>.</p>
    637 
    638 <h4>Arguments</h4>
    639 <dl>
    640   <dt><code>underStr</code></dt>
    641   <dd>The underscore-delimited string, e.g. <code>foo_bar</code> to convert to a camel-case string.</dd>
    642 </dl>
    643 
    644 <h4>See also</h4>
    645 <p><a href="#toc_camelcasetounderscore"><code>camelCaseToUnderscore</code></a></p>
    646 
    647 
    648 
    649 <h2>Built-in Template Parameters</h2>
    650 
    651 <p>Several parameters are available to FreeMarker expressions and files beyond <a href="#toc_parameter">those defined by the template</a>. These are listed below.</p>
    652 
    653 <h3>packageName</h3>
    654 
    655 <p>The Java-style Android package name for the project, e.g. <code>com.example.foo</code></p>
    656 
    657 <h3>applicationPackage</h3>
    658 
    659 <p>Will be the application package (i.e. the package name declared in the app manifest) if the target package for this template is not the application package. Otherwise, this parameter will be empty.</p>
    660 
    661 <h3>isNewProject</h3>
    662 
    663 <p>A boolean indicating whether or not this template is being instantiated as part of a New Project flow.</p>
    664 
    665 <h3>minApi</h3>
    666 
    667 <p>The minimum API level the project supports. Note that this value could be a string so consider using <a href="#toc_minapilevel"><code>minApiLevel</code></a> instead.</p>
    668 
    669 <h3>minApiLevel</h3>
    670 
    671 <p>The minimum API level the project supports, guaranteed to be a number. This is generally used to guard the generation of code based on the project's API level, for example:</p>
    672 
    673 <pre>
    674 int drawableResourceId = android.R.layout.simple_list_item_<strong>&lt;#if minApiLevel gte 11&gt;</strong>activated_<strong>&lt;/#if&gt;</strong>_1;
    675 </pre>
    676 
    677 <h3>buildApi</h3>
    678 
    679 <p>The API level that the project is building against, guaranteed to be a number. This is generally used to guard the generation of code based on what version of the Android SDK the project is being built against, for example:</p>
    680 
    681 <pre>
    682 &lt;TextView android:layout_width="wrap_content"
    683     android:layout_height="<strong>&lt;#if buildApi gte 8&gt;</strong>match_parent<strong>&lt;#else&gt;</strong>fill_parent<strong>&lt;/#if&gt;</strong>" /&gt;
    684 </pre>
    685 
    686 <h3>manifestDir</h3>
    687 
    688 <p>The target output directory for the <code>AndroidManifest.xml</code> file. This varies depending on the project's directory structure (Gradle-style or Ant-style).</p>
    689 
    690 <h3>srcDir</h3>
    691 
    692 <p>The target Java source root directory for the project. This varies depending on the project's directory structure (Gradle-style or Ant-style). It's common to concatenate the package name:</p>
    693 
    694 <pre>
    695 ${srcDir}/${slashedPackageName(packageName)}
    696 </pre>
    697 
    698 <h4>See also</h4>
    699 <p><a href="#toc_slashedpackagename"><code>slashedPackageName</code></a></p>
    700 
    701 <h3>resDir</h3>
    702 
    703 <p>The target resource directory root (<code>res/</code> folder) for the project. This varies depending on the project's directory structure (Gradle-style or Ant-style).</p>
    704 
    705 
    706 
    707 
    708 
    709 <h2>Notes for Template Authors</h2>
    710 
    711 <h3>Tools metadata</h3>
    712 
    713 <p>When creating activity layouts, make sure to include the activity name in the root view in the layout as part of the <code>tools</code> namespace, as shown in the following example:</p>
    714 
    715 <pre class="prettyprint lang-xml">
    716 &lt;TextView xmlns:android="http://schemas.android.com/apk/res/android"
    717     <strong>xmlns:tools="http://schemas.android.com/tools"</strong>
    718     android:layout_width="match_parent"
    719     android:layout_height="match_parent"
    720     android:gravity="center"
    721     android:text="@string/hello_world"
    722     android:padding="@dimen/padding_medium"
    723     <strong>tools:context=".${activityClass}"</strong> /&gt;
    724 </pre>
    725 
    726 <p>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&mdash;for example to preview the action bar, which also requires us to know the activity context.</p>
    727 
    728 </div>
    729 
    730 </body>
    731 </html>
    732