Android Studio Gradle Templates

If you’ve been keeping track of the progress of Android Studio templates you are well aware of how helpful this has been over the years and how it speeds up app development. There are numerous posts about this on medium, personal blogs, etc.

However there was something that I still did in every project that templates didn’t seem to solve for me, possibly due to looking in the wrong place. This was importing dependencies, the usual suspects such as Retrofit, dagger, gson, et al.. So with every project I created, I always had to import these libraries. This also got quite annoying when I was just trying to create a toy app and had to hop onto the different github pages to import the dependencies.

Looking to try something different, I wanted to find out if I could just edit the default build.gradle so that any project that I created, my dependencies would be imported and I would get to work asap. So with the help of this stackoverflow post, I got working and managed to get it working, adjusting for the Studio version differences. Here’s what you can do:

Navigate to your Android Studio path:

<AndroidStudioPath>\plugins\android\lib\templates\gradle-projects\NewAndroidModule\root\

Open the build.gradle.ftl file with VS Code or your favourite text editor. It will look something like this:

<#import "./shared_macros.ftl" as shared>
<#import "root://activities/common/kotlin_macros.ftl" as kt>
<#if isLibraryProject>
apply plugin: 'com.android.library'
<#elseif isDynamicFeature>
apply plugin: 'com.android.dynamic-feature'
<#else>
apply plugin: 'com.android.application'
</#if>
<@kt.addKotlinPlugins />

<!-- omitted some lines for beverity -->

dependencies {
    ${getConfigurationName("compile")} fileTree(dir: 'libs', include: ['*.jar'])

<!-- omitted some lines for beverity -->
<#if isDynamicFeature>

  implementation project(':${baseFeatureName}')
<#elseif (WearprojectName?has_content) && (Mobileincluded!false) && (Wearincluded!false)>
  wearApp project(':${WearprojectName}')
</#if>
}

You can declare your dependencies after the last </#if> statement in the file. These are the ones I use in almost every project:

    implementation 'com.squareup.retrofit2:retrofit:2.8.1'
    implementation 'com.google.code.gson:gson:2.8.6'
    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.5'
    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.5'
    implementation 'com.google.android.material:material:1.1.0-rc02'
    implementation 'androidx.recyclerview:recyclerview:1.1.0'

The obvious drawback here is that you are hard-coding your library values so it could be a day/week/month down the line and you are out of date. However, with the help of the project structure suggestions dialog we can easily handle updating our libraries within the IDE.

The time savings here would be negligible for some, as different people manage their dependencies differently such as using a separate dependencies.gradle and want to extract out version numbers.

You can even go a step further and enable things like viewBinding or dataBinding by default too!

In order to do this, lets make sure kotlin-kapt is enabled by default as well:

Navigate back to the root template directory, then go to the following path (provided full path for reference):

<AndroidStudioPath>\plugins\android\lib\templates\activities\common\kotlin_macros.ftl

The file will have the following at the top:


<#macro addKotlinPlugins>
<#if generateKotlin>
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
</#if>
</#macro>

Just add the following line underneath the last “apply plugin”, , save and close the file:

apply plugin: 'kotlin-kapt'

Then let’s enable viewBinding (or dataBinding depending on your needs) by default for every project.

Navigate to the following file and open it with your favourite text editor:

<AndroidStudioPath>\plugins\android\lib\templates\gradle-projects\NewAndroidModule\root\shared_macros.ftl

This file contains the default Android config that gets generated with every project. Things like build types, flavours, java version etc. For our purposes here we’re just going to enable viewBinding. You can add it just after the last </#if> as well:

</#if>
  viewBinding {
      enabled = true
  }
}
</#macro>

When you’re all done, create a new project in Android studio and your changes should be apparent!

After having done this, creating new projects has definitely been less admin with chrome tabs and importing libs. I hope you find this as useful as I did.

As always, I am open to improving this, possibly with checking for whatever latest version there is first and then using that. For now this is all I have and I hope this works for you too.

quick sidebar: I suggested a variation of the idea via twitter in 2015 during Android fireside chat when I experienced the pain of setting these up and at the time. The Studio team response, in the YouTube clip was interesting

Here’s the YouTube clip 🙂