SlideShare a Scribd company logo
1 of 76
Custom Android
                       Code Templates
                           Paul Murphy
                           DROIDCON SPAIN 2012




Friday 7 December 12
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 Template
Friday 7 December 12
What are Android
                       Code Templates?


Friday 7 December 12
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 EMF
Friday 7 December 12
Code Generation Process



                                               recipe.xml.ftl




                       UI Parameters
                                             FreeMarker

                                                                MyActivity.java




                       MyActivity.java.ftl




Friday 7 December 12
Code Generation Process



                                          recipe.xml.ftl




                       UI Parameters
                                        FreeMarker
                                                           output directory




                         root
                   (source directory)




Friday 7 December 12
Default ADT
                       Templates


Friday 7 December 12
Default ADT Templates

                       Template Types:
                       1. Android Application Templates
                       2. Android Activity Templates
                       3. Android Object Templates




Friday 7 December 12
Android Application Template




Friday 7 December 12
Android Application Template




Friday 7 December 12
Android Activity Template




Friday 7 December 12
Android Activity Template




Friday 7 December 12
Android Activity Template




Friday 7 December 12
Android Activity Template




                                  Template Parameters



Friday 7 December 12
Android Object Template




Friday 7 December 12
Where are
                       ADT Templates
                         Located?

Friday 7 December 12
<android-sdk>/tools/templates




Friday 7 December 12
Friday 7 December 12
Application Templates



Friday 7 December 12
Friday 7 December 12
Activity Templates




Friday 7 December 12
Friday 7 December 12
Android Object
                         Templates




Friday 7 December 12
Friday 7 December 12
Friday 7 December 12
Custom Android
                       Code Template




Friday 7 December 12
Tools and
                       Configuration


Friday 7 December 12
Tools and Config

                       • Eclipse
                       • FreeMarker IDE
                       • Create a location mapping in Eclipse to
                         <android-sdk>/tools/templates
                       • ADT Plugin Preview Releases


Friday 7 December 12
FreeMarker IDE

                       Key Features:
                • Syntax Highlighting
                • Template Structure Outline

                • Developer:
                       JBoss Tools Sub Project



Friday 7 December 12
Friday 7 December 12
Install New Software..




Friday 7 December 12
Add Site




                       http://download.jboss.org/jbosstools/updates/development/juno




Friday 7 December 12
Friday 7 December 12
Configuring Eclipse Workspace

                       • Create New Eclipse Project
                       • Set the Project Location to:
                         <android-sdk>/tools/templates




Friday 7 December 12
Eclipse | New Project...




Friday 7 December 12
New Project




                       <android-sdk>/tools/templates



Friday 7 December 12
tools/templates




Friday 7 December 12
Configured Environment




Friday 7 December 12
Template Directory
                    Structure


Friday 7 December 12
Template Directory

                                  Template




Friday 7 December 12
template.xml


                             Template Metadata




Friday 7 December 12
root


                          Source Directory




Friday 7 December 12
recipe.xml.ftl



                                   Recipe




Friday 7 December 12
globals.xml.ftl



                               Global Variables




Friday 7 December 12
root


                          Source Directory




Friday 7 December 12
app_package




                              app_package




Friday 7 December 12
FreeMarker Template Files




                                   FreeMarker template




Friday 7 December 12
FreeMarker Template Files




                                   FreeMarker templates



Friday 7 December 12
Template Files



Friday 7 December 12
Template Files




Friday 7 December 12
template.xml


                             Template Metadata




Friday 7 December 12
template.xml



                           Metadata

                         UI Parameters

                         Global Values


                           Recipe to
                            Execute


Friday 7 December 12
template.xml



                           Metadata

                         UI Parameters

                         Global Values
                                         globals.xml.ftl




                           Recipe to
                            Execute

                                         recipe.xml.ftl


Friday 7 December 12
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
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
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
recipe.xml.ftl



                                   Recipe




Friday 7 December 12
recipe.xml



                          <copy>

                        <instantiate>

                          <merge>




Friday 7 December 12
recipe.xml



                                 <copy>

                               <instantiate>
               root
                                               output directory
         (source directory)
                                 <merge>




Friday 7 December 12
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
globals.xml.ftl



                               Global Variables




Friday 7 December 12
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
Template Variable Dataflow




        UI Parameters           Global Values
                                                  globals.xml.ftl




                               Recipe Execution



Friday 7 December 12
FreeMarker
                  Template Language



Friday 7 December 12
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.org
Friday 7 December 12
Introduction to FreeMarker
                            Template                                    Data

               <manifest>
                 <activity name="{activityName}">        <global id="HomeActivity" value=
               <manifest>                                "HomeActivity"/>




                                            FreeMarker

                                                        Merge



                                   <manifest>
                                     <activity name="HomeActivity">
                                   <manifest>



                                                    Output



Friday 7 December 12
FreeMarker “Markup”

                       • Interpolations
                       ${duration}


                       • FTL Tags
                        <#if animationType == "zoom">

                        </#if>


                       • Comments
                       <#-- FreeMarker Comment -->




Friday 7 December 12
Interpolations



                 package ${packageName};

                 public class ${activityClass} extends Activity {
                 	 protected void onCreate(Bundle savedInstanceState) {
                 	 	 setContentView(R.layout.${layoutName});
                 	 }
                 }




Friday 7 December 12
Conditional Tags

                • If Statement:
                       <#if animationType == "zoom">

                       </#if>


                • Else Statement:
                       <#if animationType == "zoom">

                       <#else>

                       </#if>




Friday 7 December 12
http://freemarker.org/docs/index.html




Friday 7 December 12
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
Demo
                       Animation Template




Friday 7 December 12
Review



Friday 7 December 12
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
Questions?
                        Twitter: @murphonic




Friday 7 December 12
References

                       • Official FreeMarker Docs:
                         http://freemarker.org/docs/index.html
                       • Roman Nurik Android Template Article:
                         https://dl.dropbox.com/u/231329/
                         android/templatedocs/index.html




Friday 7 December 12

More Related Content

Similar to Android Code Templates: Custom Templates with FreeMarker

Understanding Webkit Rendering
Understanding Webkit RenderingUnderstanding Webkit Rendering
Understanding Webkit RenderingAriya Hidayat
 
Ruby meetup 7_years_in_testing
Ruby meetup 7_years_in_testingRuby meetup 7_years_in_testing
Ruby meetup 7_years_in_testingDigital Natives
 
Tools/Processes for serious android app development
Tools/Processes for serious android app developmentTools/Processes for serious android app development
Tools/Processes for serious android app developmentGaurav Lochan
 
Modularizing your Grails Application with Private Plugins - SpringOne 2GX 2012
Modularizing your Grails Application with Private Plugins - SpringOne 2GX 2012Modularizing your Grails Application with Private Plugins - SpringOne 2GX 2012
Modularizing your Grails Application with Private Plugins - SpringOne 2GX 2012kennethaliu
 
RailsAdmin - Overview and Best practices
RailsAdmin - Overview and Best practicesRailsAdmin - Overview and Best practices
RailsAdmin - Overview and Best practicesBenoit Bénézech
 
JavaScript DOM Manipulations
JavaScript DOM ManipulationsJavaScript DOM Manipulations
JavaScript DOM ManipulationsYnon Perek
 
Cloud4all Architecture Overview
Cloud4all Architecture OverviewCloud4all Architecture Overview
Cloud4all Architecture Overviewicchp2012
 
How to be an agile programmer.
How to be an agile programmer.How to be an agile programmer.
How to be an agile programmer.Tsuyoshi Ushio
 
Build and Deploy Sites Using Features
Build and Deploy Sites Using Features Build and Deploy Sites Using Features
Build and Deploy Sites Using Features Phase2
 
Client Server 3.0 - 6 Ways JavaScript is Revolutionizing the Client/Server Re...
Client Server 3.0 - 6 Ways JavaScript is Revolutionizing the Client/Server Re...Client Server 3.0 - 6 Ways JavaScript is Revolutionizing the Client/Server Re...
Client Server 3.0 - 6 Ways JavaScript is Revolutionizing the Client/Server Re...Jesse Cravens
 
Reverse engineering and instrumentation of android apps
Reverse engineering and instrumentation of android appsReverse engineering and instrumentation of android apps
Reverse engineering and instrumentation of android appsGaurav Lochan
 
Разработка приложений в Android studio
Разработка приложений в Android studioРазработка приложений в Android studio
Разработка приложений в Android studioDev2Dev
 
Introduction To MongoDB
Introduction To MongoDBIntroduction To MongoDB
Introduction To MongoDBYnon Perek
 
Working With Social APIs - SoMeT12
Working With Social APIs - SoMeT12Working With Social APIs - SoMeT12
Working With Social APIs - SoMeT12Mario Vasquez
 
The state of drupal 8 - Drupalcamp Gent
The state of drupal 8  - Drupalcamp GentThe state of drupal 8  - Drupalcamp Gent
The state of drupal 8 - Drupalcamp Gentswentel
 

Similar to Android Code Templates: Custom Templates with FreeMarker (20)

Understanding Webkit Rendering
Understanding Webkit RenderingUnderstanding Webkit Rendering
Understanding Webkit Rendering
 
Grails 2.0 Update
Grails 2.0 UpdateGrails 2.0 Update
Grails 2.0 Update
 
HTML5 and Sencha Touch
HTML5 and Sencha TouchHTML5 and Sencha Touch
HTML5 and Sencha Touch
 
Ruby meetup 7_years_in_testing
Ruby meetup 7_years_in_testingRuby meetup 7_years_in_testing
Ruby meetup 7_years_in_testing
 
Tools/Processes for serious android app development
Tools/Processes for serious android app developmentTools/Processes for serious android app development
Tools/Processes for serious android app development
 
Modularizing your Grails Application with Private Plugins - SpringOne 2GX 2012
Modularizing your Grails Application with Private Plugins - SpringOne 2GX 2012Modularizing your Grails Application with Private Plugins - SpringOne 2GX 2012
Modularizing your Grails Application with Private Plugins - SpringOne 2GX 2012
 
RailsAdmin - Overview and Best practices
RailsAdmin - Overview and Best practicesRailsAdmin - Overview and Best practices
RailsAdmin - Overview and Best practices
 
Backbone
BackboneBackbone
Backbone
 
JavaScript DOM Manipulations
JavaScript DOM ManipulationsJavaScript DOM Manipulations
JavaScript DOM Manipulations
 
Cloud4all Architecture Overview
Cloud4all Architecture OverviewCloud4all Architecture Overview
Cloud4all Architecture Overview
 
How to be an agile programmer.
How to be an agile programmer.How to be an agile programmer.
How to be an agile programmer.
 
Build and Deploy Sites Using Features
Build and Deploy Sites Using Features Build and Deploy Sites Using Features
Build and Deploy Sites Using Features
 
Client Server 3.0 - 6 Ways JavaScript is Revolutionizing the Client/Server Re...
Client Server 3.0 - 6 Ways JavaScript is Revolutionizing the Client/Server Re...Client Server 3.0 - 6 Ways JavaScript is Revolutionizing the Client/Server Re...
Client Server 3.0 - 6 Ways JavaScript is Revolutionizing the Client/Server Re...
 
Reverse engineering and instrumentation of android apps
Reverse engineering and instrumentation of android appsReverse engineering and instrumentation of android apps
Reverse engineering and instrumentation of android apps
 
Разработка приложений в Android studio
Разработка приложений в Android studioРазработка приложений в Android studio
Разработка приложений в Android studio
 
Introduction To MongoDB
Introduction To MongoDBIntroduction To MongoDB
Introduction To MongoDB
 
Working With Social APIs - SoMeT12
Working With Social APIs - SoMeT12Working With Social APIs - SoMeT12
Working With Social APIs - SoMeT12
 
07 PhoneGap
07 PhoneGap07 PhoneGap
07 PhoneGap
 
The state of drupal 8 - Drupalcamp Gent
The state of drupal 8  - Drupalcamp GentThe state of drupal 8  - Drupalcamp Gent
The state of drupal 8 - Drupalcamp Gent
 
Fork cli tool
Fork cli toolFork cli tool
Fork cli tool
 

Recently uploaded

Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024BookNet Canada
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 
Science&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdfScience&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdfjimielynbastida
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
Bluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdfBluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdfngoud9212
 

Recently uploaded (20)

Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping Elbows
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptxVulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
 
Science&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdfScience&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdf
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
Bluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdfBluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdf
 

Android Code Templates: Custom Templates with FreeMarker

  • 1. Custom Android Code Templates Paul Murphy DROIDCON SPAIN 2012 Friday 7 December 12
  • 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 Template Friday 7 December 12
  • 3. What are Android Code Templates? Friday 7 December 12
  • 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 EMF Friday 7 December 12
  • 5. Code Generation Process recipe.xml.ftl UI Parameters FreeMarker MyActivity.java MyActivity.java.ftl Friday 7 December 12
  • 6. Code Generation Process recipe.xml.ftl UI Parameters FreeMarker output directory root (source directory) Friday 7 December 12
  • 7. Default ADT Templates Friday 7 December 12
  • 8. Default ADT Templates Template Types: 1. Android Application Templates 2. Android Activity Templates 3. Android Object Templates Friday 7 December 12
  • 14. Android Activity Template Template Parameters Friday 7 December 12
  • 16. Where are ADT Templates Located? Friday 7 December 12
  • 23. Android Object Templates Friday 7 December 12
  • 26. Custom Android Code Template Friday 7 December 12
  • 27. Tools and Configuration Friday 7 December 12
  • 28. Tools and Config • Eclipse • FreeMarker IDE • Create a location mapping in Eclipse to <android-sdk>/tools/templates • ADT Plugin Preview Releases Friday 7 December 12
  • 29. FreeMarker IDE Key Features: • Syntax Highlighting • Template Structure Outline • Developer: JBoss Tools Sub Project Friday 7 December 12
  • 32. Add Site http://download.jboss.org/jbosstools/updates/development/juno Friday 7 December 12
  • 34. Configuring Eclipse Workspace • Create New Eclipse Project • Set the Project Location to: <android-sdk>/tools/templates Friday 7 December 12
  • 35. Eclipse | New Project... Friday 7 December 12
  • 36. New Project <android-sdk>/tools/templates Friday 7 December 12
  • 39. Template Directory Structure Friday 7 December 12
  • 40. Template Directory Template Friday 7 December 12
  • 41. template.xml Template Metadata Friday 7 December 12
  • 42. root Source Directory Friday 7 December 12
  • 43. recipe.xml.ftl Recipe Friday 7 December 12
  • 44. globals.xml.ftl Global Variables Friday 7 December 12
  • 45. root Source Directory Friday 7 December 12
  • 46. app_package app_package Friday 7 December 12
  • 47. FreeMarker Template Files FreeMarker template Friday 7 December 12
  • 48. FreeMarker Template Files FreeMarker templates Friday 7 December 12
  • 49. Template Files Friday 7 December 12
  • 50. Template Files Friday 7 December 12
  • 51. template.xml Template Metadata Friday 7 December 12
  • 52. template.xml Metadata UI Parameters Global Values Recipe to Execute Friday 7 December 12
  • 53. template.xml Metadata UI Parameters Global Values globals.xml.ftl Recipe to Execute recipe.xml.ftl Friday 7 December 12
  • 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. 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. 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. recipe.xml.ftl Recipe Friday 7 December 12
  • 58. recipe.xml <copy> <instantiate> <merge> Friday 7 December 12
  • 59. recipe.xml <copy> <instantiate> root output directory (source directory) <merge> Friday 7 December 12
  • 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. globals.xml.ftl Global Variables Friday 7 December 12
  • 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. Template Variable Dataflow UI Parameters Global Values globals.xml.ftl Recipe Execution Friday 7 December 12
  • 64. FreeMarker Template Language Friday 7 December 12
  • 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.org Friday 7 December 12
  • 66. Introduction to FreeMarker Template Data <manifest> <activity name="{activityName}"> <global id="HomeActivity" value= <manifest> "HomeActivity"/> FreeMarker Merge <manifest> <activity name="HomeActivity"> <manifest> Output Friday 7 December 12
  • 67. FreeMarker “Markup” • Interpolations ${duration} • FTL Tags <#if animationType == "zoom"> </#if> • Comments <#-- FreeMarker Comment --> Friday 7 December 12
  • 68. Interpolations package ${packageName}; public class ${activityClass} extends Activity { protected void onCreate(Bundle savedInstanceState) { setContentView(R.layout.${layoutName}); } } Friday 7 December 12
  • 69. Conditional Tags • If Statement: <#if animationType == "zoom"> </#if> • Else Statement: <#if animationType == "zoom"> <#else> </#if> Friday 7 December 12
  • 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. Demo Animation Template Friday 7 December 12
  • 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. Questions? Twitter: @murphonic Friday 7 December 12
  • 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.html Friday 7 December 12