Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Custom Android                       Code Templates                           Paul Murphy                           DROIDC...
Agenda                       • What are Android Code Templates?                       • Default ADT Templates             ...
What are Android                       Code Templates?Friday 7 December 12
What are Android Code                Templates?                       • A mechanism for generating boilerplate            ...
Code Generation Process                                               recipe.xml.ftl                       UI Parameters  ...
Code Generation Process                                          recipe.xml.ftl                       UI Parameters       ...
Default ADT                       TemplatesFriday 7 December 12
Default ADT Templates                       Template Types:                       1. Android Application Templates        ...
Android Application TemplateFriday 7 December 12
Android Application TemplateFriday 7 December 12
Android Activity TemplateFriday 7 December 12
Android Activity TemplateFriday 7 December 12
Android Activity TemplateFriday 7 December 12
Android Activity Template                                  Template ParametersFriday 7 December 12
Android Object TemplateFriday 7 December 12
Where are                       ADT Templates                         Located?Friday 7 December 12
<android-sdk>/tools/templatesFriday 7 December 12
Friday 7 December 12
Application TemplatesFriday 7 December 12
Friday 7 December 12
Activity TemplatesFriday 7 December 12
Friday 7 December 12
Android Object                         TemplatesFriday 7 December 12
Friday 7 December 12
Friday 7 December 12
Custom Android                       Code TemplateFriday 7 December 12
Tools and                       ConfigurationFriday 7 December 12
Tools and Config                       • Eclipse                       • FreeMarker IDE                       • Create a lo...
FreeMarker IDE                       Key Features:                • Syntax Highlighting                • Template Structur...
Friday 7 December 12
Install New Software..Friday 7 December 12
Add Site                       http://download.jboss.org/jbosstools/updates/development/junoFriday 7 December 12
Friday 7 December 12
Configuring Eclipse Workspace                       • Create New Eclipse Project                       • Set the Project Lo...
Eclipse | New Project...Friday 7 December 12
New Project                       <android-sdk>/tools/templatesFriday 7 December 12
tools/templatesFriday 7 December 12
Configured EnvironmentFriday 7 December 12
Template Directory                    StructureFriday 7 December 12
Template Directory                                  TemplateFriday 7 December 12
template.xml                             Template MetadataFriday 7 December 12
root                          Source DirectoryFriday 7 December 12
recipe.xml.ftl                                   RecipeFriday 7 December 12
globals.xml.ftl                               Global VariablesFriday 7 December 12
root                          Source DirectoryFriday 7 December 12
app_package                              app_packageFriday 7 December 12
FreeMarker Template Files                                   FreeMarker templateFriday 7 December 12
FreeMarker Template Files                                   FreeMarker templatesFriday 7 December 12
Template FilesFriday 7 December 12
Template FilesFriday 7 December 12
template.xml                             Template MetadataFriday 7 December 12
template.xml                           Metadata                         UI Parameters                         Global Value...
template.xml                           Metadata                         UI Parameters                         Global Value...
template.xml                       <?xml version="1.0"?>                       <template name="My Template"               ...
Category Element                        <?xml version="1.0"?>                        <template name="My Template"         ...
Category Element                       • Applications                                        <template name="MyTemplate"> ...
recipe.xml.ftl                                   RecipeFriday 7 December 12
recipe.xml                          <copy>                        <instantiate>                          <merge>Friday 7 D...
recipe.xml                                 <copy>                               <instantiate>               root          ...
recipe.xml.ftl                       <?xml version="1.0"?>                       <recipe>                       	     <cop...
globals.xml.ftl                               Global VariablesFriday 7 December 12
recipe.xml.ftl                       <?xml version="1.0"?>                       <globals>                       	   <#-- ...
Template Variable Dataflow        UI Parameters           Global Values                                                  g...
FreeMarker                  Template LanguageFriday 7 December 12
FreeMarker Template Language                       • It is a Java Template Language used                         for over ...
Introduction to FreeMarker                            Template                                    Data               <mani...
FreeMarker “Markup”                       • Interpolations                       ${duration}                       • FTL T...
Interpolations                 package ${packageName};                 public class ${activityClass} extends Activity {   ...
Conditional Tags                • If Statement:                       <#if animationType == "zoom">                       ...
http://freemarker.org/docs/index.htmlFriday 7 December 12
Extra Template Functions                       string slashedPackageName(string)                       This function conve...
Demo                       Animation TemplateFriday 7 December 12
ReviewFriday 7 December 12
Ideas for Custom ADT?                       • Custom SDK Templates                       • Rapid Development Templates    ...
Questions?                        Twitter: @murphonicFriday 7 December 12
References                       • Official FreeMarker Docs:                         http://freemarker.org/docs/index.html ...
Upcoming SlideShare
Loading in …5
×

Custom Android Code Templates

15,478 views

Published on

Published in: Technology
  • Tutorial for creating custom Android code templates http://robusttechhouse.com/tutorial-how-to-create-custom-android-code-templates/ was inspired by your useful slides
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • http://es.droidcon.com/wp-content/uploads/2012/12/Custom-Android-Code-Templates.pdf
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here

Custom Android Code Templates

  1. 1. Custom Android Code Templates Paul Murphy DROIDCON SPAIN 2012Friday 7 December 12
  2. 2. Agenda • What are Android Code Templates? • Default ADT Templates • Where are ADT Templates Located? • Tools and Configuration in Eclipse • Template Directory Structure • Template Files • FreeMarker Template Language • Demo - Animation TemplateFriday 7 December 12
  3. 3. What are Android Code Templates?Friday 7 December 12
  4. 4. What are Android Code Templates? • A mechanism for generating boilerplate Android code and assets from the input of simple UI parameters. • Intergrated with the Eclipse ADT Plugin • Templates are written using FreeMarker - a Java templating engine • It is relatively lightweight - not a full metadata driven code generation solution like EMFFriday 7 December 12
  5. 5. Code Generation Process recipe.xml.ftl UI Parameters FreeMarker MyActivity.java MyActivity.java.ftlFriday 7 December 12
  6. 6. Code Generation Process recipe.xml.ftl UI Parameters FreeMarker output directory root (source directory)Friday 7 December 12
  7. 7. Default ADT TemplatesFriday 7 December 12
  8. 8. Default ADT Templates Template Types: 1. Android Application Templates 2. Android Activity Templates 3. Android Object TemplatesFriday 7 December 12
  9. 9. Android Application TemplateFriday 7 December 12
  10. 10. Android Application TemplateFriday 7 December 12
  11. 11. Android Activity TemplateFriday 7 December 12
  12. 12. Android Activity TemplateFriday 7 December 12
  13. 13. Android Activity TemplateFriday 7 December 12
  14. 14. Android Activity Template Template ParametersFriday 7 December 12
  15. 15. Android Object TemplateFriday 7 December 12
  16. 16. Where are ADT Templates Located?Friday 7 December 12
  17. 17. <android-sdk>/tools/templatesFriday 7 December 12
  18. 18. Friday 7 December 12
  19. 19. Application TemplatesFriday 7 December 12
  20. 20. Friday 7 December 12
  21. 21. Activity TemplatesFriday 7 December 12
  22. 22. Friday 7 December 12
  23. 23. Android Object TemplatesFriday 7 December 12
  24. 24. Friday 7 December 12
  25. 25. Friday 7 December 12
  26. 26. Custom Android Code TemplateFriday 7 December 12
  27. 27. Tools and ConfigurationFriday 7 December 12
  28. 28. Tools and Config • Eclipse • FreeMarker IDE • Create a location mapping in Eclipse to <android-sdk>/tools/templates • ADT Plugin Preview ReleasesFriday 7 December 12
  29. 29. FreeMarker IDE Key Features: • Syntax Highlighting • Template Structure Outline • Developer: JBoss Tools Sub ProjectFriday 7 December 12
  30. 30. Friday 7 December 12
  31. 31. Install New Software..Friday 7 December 12
  32. 32. Add Site http://download.jboss.org/jbosstools/updates/development/junoFriday 7 December 12
  33. 33. Friday 7 December 12
  34. 34. Configuring Eclipse Workspace • Create New Eclipse Project • Set the Project Location to: <android-sdk>/tools/templatesFriday 7 December 12
  35. 35. Eclipse | New Project...Friday 7 December 12
  36. 36. New Project <android-sdk>/tools/templatesFriday 7 December 12
  37. 37. tools/templatesFriday 7 December 12
  38. 38. Configured EnvironmentFriday 7 December 12
  39. 39. Template Directory StructureFriday 7 December 12
  40. 40. Template Directory TemplateFriday 7 December 12
  41. 41. template.xml Template MetadataFriday 7 December 12
  42. 42. root Source DirectoryFriday 7 December 12
  43. 43. recipe.xml.ftl RecipeFriday 7 December 12
  44. 44. globals.xml.ftl Global VariablesFriday 7 December 12
  45. 45. root Source DirectoryFriday 7 December 12
  46. 46. app_package app_packageFriday 7 December 12
  47. 47. FreeMarker Template Files FreeMarker templateFriday 7 December 12
  48. 48. FreeMarker Template Files FreeMarker templatesFriday 7 December 12
  49. 49. Template FilesFriday 7 December 12
  50. 50. Template FilesFriday 7 December 12
  51. 51. template.xml Template MetadataFriday 7 December 12
  52. 52. template.xml Metadata UI Parameters Global Values Recipe to ExecuteFriday 7 December 12
  53. 53. template.xml Metadata UI Parameters Global Values globals.xml.ftl Recipe to Execute recipe.xml.ftlFriday 7 December 12
  54. 54. template.xml <?xml version="1.0"?> <template name="My Template" description="description..."> <category value="UI Components"/> <parameter id="duration" type="float" name="Animation Duration"/> <globals file="globals.xml.ftl" /> <execute file="recipe.xml.ftl" /> </template>Friday 7 December 12
  55. 55. Category Element <?xml version="1.0"?> <template name="My Template" description="description..."> <category value="UI Components"/> <parameter id="duration" type="float" name="Animation Duration"/> <globals file="globals.xml.ftl" /> <execute file="recipe.xml.ftl" /> </template>Friday 7 December 12
  56. 56. Category Element • Applications <template name="MyTemplate"> <category value="Applications"/> .... </template> <template name="MyTemplate"> • Activities <category value="Activities"/> ... </template> <template name="MyTemplate"> • UI Component <category value="UI Component"/> ... </template>Friday 7 December 12
  57. 57. recipe.xml.ftl RecipeFriday 7 December 12
  58. 58. recipe.xml <copy> <instantiate> <merge>Friday 7 December 12
  59. 59. recipe.xml <copy> <instantiate> root output directory (source directory) <merge>Friday 7 December 12
  60. 60. recipe.xml.ftl <?xml version="1.0"?> <recipe> <copy from="res/anim/zoom.xml" to="res/anim/zoom.xml" /> <instantiate from="res/anim/zoom.xml.ftl" to="res/anim/${filename}.xml" /> <merge from="AndroidManifest.xml.ftl" /> <open file="res/anim/${filename}.xml"/> </recipe>Friday 7 December 12
  61. 61. globals.xml.ftl Global VariablesFriday 7 December 12
  62. 62. recipe.xml.ftl <?xml version="1.0"?> <globals> <#-- Animation Defaults --> <global id="slow" value="500" /> <global id="fast" value="100" /> </globals>Friday 7 December 12
  63. 63. Template Variable Dataflow UI Parameters Global Values globals.xml.ftl Recipe ExecutionFriday 7 December 12
  64. 64. FreeMarker Template LanguageFriday 7 December 12
  65. 65. FreeMarker Template Language • It is a Java Template Language used for over 10 years • It is an example of a DSL for generating text output of any kind • It has a small subset of programming language constructs • Similar to: Velocity Template Language, C Preprocessor, PHP, Django templates • http://freemarker.orgFriday 7 December 12
  66. 66. Introduction to FreeMarker Template Data <manifest> <activity name="{activityName}"> <global id="HomeActivity" value= <manifest> "HomeActivity"/> FreeMarker Merge <manifest> <activity name="HomeActivity"> <manifest> OutputFriday 7 December 12
  67. 67. FreeMarker “Markup” • Interpolations ${duration} • FTL Tags <#if animationType == "zoom"> </#if> • Comments <#-- FreeMarker Comment -->Friday 7 December 12
  68. 68. Interpolations package ${packageName}; public class ${activityClass} extends Activity { protected void onCreate(Bundle savedInstanceState) { setContentView(R.layout.${layoutName}); } }Friday 7 December 12
  69. 69. Conditional Tags • If Statement: <#if animationType == "zoom"> </#if> • Else Statement: <#if animationType == "zoom"> <#else> </#if>Friday 7 December 12
  70. 70. http://freemarker.org/docs/index.htmlFriday 7 December 12
  71. 71. Extra Template Functions string slashedPackageName(string) This function converts a full Java package name to its corresponding directory path. For example, if the given argument is com.example.foo, the return value will be com/ example/foo. <global id="srcOut" value="src/${slashedPackageName(packageName)}" /> string underscoreToCamelCase(string) This function converts an underscore-delimited string, such as foo_bar, to its corresponding camel-case string, such as FooBar. string activityToLayout(string) This function converts an activity class-like identifer string, such as FooActivity, to a corresponding resource-friendly identifier string, such as activity_foo.Friday 7 December 12
  72. 72. Demo Animation TemplateFriday 7 December 12
  73. 73. ReviewFriday 7 December 12
  74. 74. Ideas for Custom ADT? • Custom SDK Templates • Rapid Development Templates • Templates enabling easy access to an organisations visual assets colour schemes etc... • Generating the boiler plate code required for Content Providers etc...Friday 7 December 12
  75. 75. Questions? Twitter: @murphonicFriday 7 December 12
  76. 76. References • Official FreeMarker Docs: http://freemarker.org/docs/index.html • Roman Nurik Android Template Article: https://dl.dropbox.com/u/231329/ android/templatedocs/index.htmlFriday 7 December 12

×