Streamlining Your Android App Development: Unlocking the Power of File Templates in Android Studio

Android Development Jun 23, 2023

At Halodoc, we are constantly on the lookout to try and optimise on the processes we follow.

One of the key process that we are actively exploring is to follow the common coding standards across all the modules that we use at Halodoc.

Different enterprise applications follow different types of architectural patterns, based on the use-cases it has. And in order to adopt to those patterns, we need to define certain guidelines to make the code consistent across all the project.

There are different methods by which we can achieve this. One such method that we have adopted is the File Templates tool that's present in Android Studio.

What are templates?

Templates are specifications of the default contents to be generated when creating a new file. When used, these templates provide initial code and formatting that is expected to be in a file/feature as per industry standards/company’s architecture and adhered guidelines.

Benefits of Templates

  • We can use Templates to avoid adding boilerplate codes we use everyday while creating any new screens, or relevant ViewModels or Repository, if needed
  • Boosting Productivity & reducing development time
  • Using consistent coding pattern across teams
  • It helps to follow best practices
  • Customise templates to suit project requirements

How to create a template

We can create different types of Templates based on the architectural pattern we follow like: Creating a new Fragment, ViewModel, Repository, etc

To create a new type of File Template, you need to go to:

  • For Mac:
    Android Studio -> Preferences -> Editor -> File and Code Templates
  • For Windows:
    File -> Settings -> Editor -> File and Code Templates

You will see something like this:

You need to pick the language of your choice, in this case it can either be Java or Kotlin.

To create a new File Template, follow the steps given below:

  • Click on + button
  • Enter the File Template Name of your Choice

If you look at the existing templates, you can see ${NAME} and/or ${PACKAGE_NAME} used in the existing or custom Templates. Please note that ${NAME} is a name that we will enter when we select to create a file from this template & ${PACKAGE_NAME} is just a name of the package where we are creating the file.

Below given are some examples on how we are leveraging this in our current project:

MVVM Template

While creating any feature, we use this MVVM Feature template which generates the boilerplate code to create any basic feature. It includes the following components:

Generic Fragment

This Generic Fragment Template consists of:

  • {featureName}ViewModel: To handle business logic
  • initView: Function to initialise UI components for the fragment
  • setupViewModel: To setup ViewModel using {featureName}ViewModelFactory
  • observeViewModel: To observe data streams like LiveData, Flow, etc

Generic ViewModel

This Generic ViewModel consists of:

  • {featureName}Repository: We would be using this as data layer
  • contextProvider: This is the class which gives us all the scopes that are needed to launch coroutines

Generic ViewModelFactory

We are using ViewModelFactory to inject {featureName}Repository to {featureName}ViewModel

Usecase Template

These classes usually depend on repository classes, and they communicate with the UI layer the same way repositories do.

Repository Template

We use repository to abstract the data layer, and integrate the repository class with the ViewModel. We keep contract and implementation separately to follow the clean architecture.

.

You can also export these custom templates as a zip with the below steps:

  • Create a new Project
  • Add Custom File Templates
  • Go to File -> Manage IDE Settings -> Export Settings
  • Select only File Templates (schemes)

You can then import these settings to any other Android Studio Project by following below steps:

  • Go to File -> Manage IDE Settings -> Import Settings
  • Select the settings.zip file you created
  • Choose the settings to import & Click OK

Summary

Every developer at Halodoc is using these templates to develop features for different business verticals. And in order to speed up the development, we use different techniques to speed up the development efforts. File Templates is one  of the best technique which is very specific to the architecture you chose. It also helps your code to be consistent across different modules.

Join us

Scalability, reliability and maintainability are the three pillars that govern what we build at Halodoc Tech. We are actively looking for engineers at all levels and if solving hard problems with challenging requirements is your forte, please reach out to us with your resumé at careers.india@halodoc.com.


About Halodoc

Halodoc is the number 1 all around Healthcare application in Indonesia. Our mission is to simplify and bring quality healthcare across Indonesia, from Sabang to Merauke. We connect 20,000+ doctors with patients in need through our Tele-consultation service. We partner with 3500+ pharmacies in 100+ cities to bring medicine to your doorstep. We've also partnered with Indonesia's largest lab provider to provide lab home services, and to top it off we have recently launched a premium appointment service that partners with 500+ hospitals that allow patients to book a doctor appointment inside our application. We are extremely fortunate to be trusted by our investors, such as the Bill & Melinda Gates Foundation, Singtel, UOB Ventures, Allianz, GoJek, Astra, Temasek and many more. We recently closed our Series C round and In total have raised around USD$180 million for our mission. Our team works tirelessly to make sure that we create the best healthcare solution personalised for all of our patient's needs, and are continuously on a path to simplify healthcare for Indonesia.


Vipul Patil

A Technology Products lover and Engineer working on Mobile Development impacting many lives daily.