Gradle Tips for Android Studio

13th Oct, 2021 | Farhan S.

  • Software Development
Gradle Tips for Android Studio

Android Studio, the official integrated development environment (IDE) for Android app development, relies heavily on Gradle to manage project builds and dependencies.

Gradle is a powerful build tool that automates the build process, making it essential for efficient and scalable Android development.

In this blog post, we'll explore a variety of Gradle tips and tricks to help you optimize your workflow in Android Studio. These tips were tested on Android studio 4.0+ and Gradle version 6+.

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

Gradle is an open-source build automation tool designed for multi-project builds. It is capable of handling a variety of tasks, including compiling source code, managing dependencies, running tests, and packaging applications.

Gradle uses a Groovy-based domain-specific language (DSL) or, alternatively, a Kotlin DSL, to define project configurations and build scripts.

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{ 
    android.applicationVariants.all { variant ->
        variant.outputs.all {
            outputFileName = "${rootProject.name}-${variant.name}-${variant.versionName}.apk"
        }
   }
}

OR

defaultConfig {
    setProperty("archivesBaseName", "${rootProject.name}-$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.

android{
    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 ('io.socket:socket.io-client:2.0.0') {
        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

Conclusion

Mastering Gradle is crucial for Android developers seeking to optimize their build processes and streamline development workflows.

The tips provided in this blog post cover a range of topics, from fundamental concepts to advanced techniques, allowing you to harness the full power of Gradle in Android Studio.

Whether you're a beginner or an experienced developer, implementing these Gradle tips will contribute to more efficient, scalable, and maintainable Android projects.

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.

References:

1. Android Studio Gradle

2. Gradle

More blogs in "Software Development"

restaurant mobile app development
  • Software Development
  • 6th Jan, 2025
  • Kinjal D.

Restaurant Mobile App Development: A Complete Guide For 2025

Restaurant mobile app development has become a key factor in the success of the food industry. By simplifying ordering systems and enhancing customer loyalty, mobile...
Keep Reading
OTT Platform
  • Software Development
  • 6th Mar, 2025
  • Rahul C.

How to Develop an OTT Platform: A Complete Guide

The OTT industry is booming, and businesses have a golden opportunity to tap into this market. This guide covers everything about OTT platform development, including...
Keep Reading
iOS App Development
  • Software Development
  • 20th Nov, 2025
  • Aishwarya Y.

Expand Your Business With Custom iOS App Development Service

iOS app development has become a critical component of business expansion in today's digital landscape. Custom iOS apps offer unique solutions tailored to specific business needs,...
Keep Reading
Mumbai, India Flag
Mumbai, India
Address Icon

18th Floor, Cyberone Sector 30, Vashi, Navi Mumbai, MH

Ahmedabad, India Flag
Ahmedabad, India
Address Icon

705, Colonnade - 2, Rajpath Rangoli Road, Ahmedabad, GJ

Ras Al Khaimah, UAE Flag
Ras Al Khaimah, UAE
Address Icon

BIZ01300, Compass Building, Al Shohada Road, RAK