Gradle Tips for Android Studio

  • 13th Oct, 2021
  • Farhan S.
  • Facebook-icon
  • LinkedIn-icon
  • WhatsApp-icon

Gradle Tips for Android Studio

13th Oct, 2021 | Farhan S.

  • Software Development
Gradle Tips for Android Studio

Hey Android Devs, in this blog I’m writing some useful tips about using gradle features in Android studio. I hope you find it useful. These tips are tested on Android studio 4.0+ and Gradle version 6+.

What is Gradle and Why is it used in Android Studio?

Gradle is a build system (open source) which is used to automate building, testing, deployment etc. “Build. gradle” are scripts where one can automate the tasks. For example, the simple task to copy some files from one directory to another can be performed by Gradle build script before the actual build process happens.

1. Set Custom Name for your Autogenerated .apk or .aar Files

Add the following lines in your build.gradle (app).

    android.applicationVariants.all { variant ->
        variant.outputs.all {
            outputFileName = "${}-${}-${variant.versionName}.apk"


defaultConfig {
    setProperty("archivesBaseName", "${}-$versionName")

2. Multiple Dimensions and Flavours

To create multiple types or versions of the App, such as Free and Paid version, we can use productFlavours. We can also define different App name, application id suffix, version suffix and icons for different flavours of the App.

    flavorDimensions "type"

    productFlavors {
        free {
            dimension "type"
            resValue "string", "app_name", "App name"
            manifestPlaceholders = [
                    appIcon: "@mipmap/ic_launcher",
                    appIconRound: "@mipmap/ic_launcher_round"
        paid {
            dimension "type"
            applicationIdSuffix '.pro’
            versionNameSuffix '-pro'
            resValue "string", "app_name", "App name TEST"
            manifestPlaceholders = [
                    appIcon: "@mipmap/ic_launcher_test",
                    appIconRound: "@mipmap/ic_launcher_test_round"

3. Dependency Resolution

If multiple libraries are using the same packages, it could create a conflict for the gradle compiler, such as:

// opencv 3.4.5 implementation 'com.quickbirdstudios:opencv:3.4.5' //javacv library implementation 'org.bytedeco:javacv:1.5.5'

These dependencies use the same package org.opencv but have different features which you want to use.

To resolve this issue add these code to the end of your build.gradle (app) file:

configurations.all {
    resolutionStrategy {
        // add dependency substitution rules
        dependencySubstitution {
            // Substitute one module dependency for another
            substitute module('org.bytedeco:opencv') using module('com.quickbirdstudios:opencv:3.4.5')

This method substitutes a module with another specified module to resolve conflict.

4. Exclude Package/Module

Sometimes you don’t require packages that already exist in Android sdk or are useless for you. To exclude those packages we can use the exclude function in gradle.

implementation ('') {
        exclude group: 'org.json', module: 'json'

5. Signing Config

When you are working with a team on the same Android project or generating builds via CI/CD, you need to define a signing config to sign the apk.

Here is how you can do this. First you need to create Key Store file (i.e build menu > generate signed bundle/apk > create new ).

android {

    signingConfigs {
        debug {
            storeFile file('../keys/debug')
            storePassword 'pass123'
            keyPassword 'pass123'
            keyAlias 'key0'


Note: You must not write password and alias in gradle file for release builds, it should be kept in a separate file.

6. Gradle Task

You can create a custom task in the gradle for your specific needs. For example, I want to fetch the App version from the command line, so I can create a task for that as shown below.

Add In build.gradle (app)

task printVersionName {
    doLast {
        println android.defaultConfig.versionCode

To execute this task in terminal, write :

./gradlew -q printVersionName


As the gradle build system allows you to build and automate your tasks, there are so many features and possibilities to increase your productivity and save time.


1. Android Studio Gradle

2. Gradle