Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Custom templates in Android Studio

Custom templates in Android Studio

Andrei Dotsenko

August 28, 2020
Tweet

More Decks by Andrei Dotsenko

Other Decks in Programming

Transcript

  1. Plan - Types of templates in Android Studio - Live

    templates - File templates - Android component templates - How to create custom templates of each type - Some ideas about how to improve the development process with templates
  2. Live templates For insertion common constructs into your code, such

    as loops, conditions, various declarations etc. https://www.jetbrains.com/help/idea/using-live-templates.html
  3. Parameterized templates Contain variables that enable user input. - Loops

    - If checks - Function declarations - Simple & common pieces of code
  4. 1

  5. 2

  6. 3

  7. 4

  8. Freemarker template engine - Java library to generate text output

    - Templates are written in the FreeMarker Template Language - Android-specific tooling and GUI in Android Studio
  9. How to create custom component template? - Create template files

    according to Freemarker format - Place template files to ANDROID_STUDIO_DIR\plugins\android\lib\templates\ - Restart Android Studio
  10. Unofficial Jedi way - No official support custom templates -

    Narrow & outdated documentation - Tricky debugging - But a lot of profit
  11. Template structure - template.xml (required) - recipe.xml.ftl (required) - globals.xml.ftl

    - .ftl file for each file you’d like to generate - Image file with cat to show in template window
  12. Template.xml - metadata - String input fields - Checkboxes (boolean)

    - Enums - Required / optional - Constraints (e.g. "class|nonempty|unique") - Dependency between items (name suggestion, visibility) - Modify user input (activityToLayout, classToResource) - Define recipe and globals files
  13. .ftl files - actual template files - Use variables from

    template.xml and globals.xml - Use power of Freemarker Template language - Built-in Android Template functions and parameters
  14. Whole template creation flow User selects template in Android Studio

    GUI with template configuration (template.xml) User completes and confirms configuration Freemarker executes recipe.xml.ftl Output files generated based on .ftl templates and user data on previous steps
  15. Template debugging - Open Android Studio log (Help | Show

    log) - Find stacktrace and figure out what was wrong - Fix problem - Reboot Android Studio
  16. Pros & cons Pros: - Super-flexible - Project-specific - Serve

    any custom or third-party solutions - From several files to whole project draft - Development process boost Cons: - Merge only for XML and Gradle - Unofficial way - Lack of documentation - Awkward debugging - Android Studio updates destroy all your fancy setup - No guarantees
  17. Ideas for development process Cases: - Presentation layer (MVVM/MVP/MWhatever boilerplate,

    content/loading/error states management, data-binding lists initialization) - Domain layer structures (interactor, component etc) - Network layer (service, mapping, repository) - Custom views (class, declare-styleable, SavedState) - Gradle feature module with full initial setup - Module layout (Server Driven UI) - Draft project with all dependencies, base classes, DI setup etc ...
  18. Ideas for development process Advantages: - “Forced” code/component style -

    Development speed acceleration - Boring stuff automatization - Reusable templates across projects
  19. Links - IDEA help https://www.jetbrains.com/help/idea/using-file-and-code-templates.html https://www.jetbrains.com/help/idea/using-live-templates.html - Apache FreeMarker https://freemarker.apache.org/docs/

    - Google Git folder with docs for Android Component Templates https://android.googlesource.com/platform/sdk/+/refs/heads/master/templates/doc s/ - Demo project https://github.com/dosssik/AndroidComponentTemplatesExample