Gradle Tips for Android Studio

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

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"

DevOps
  • Software Development
  • 19th Jun, 2023
  • Rinkal J.

Role of DevOps in Modern IT Business Operations

DevOps has become a buzzword with the rise of digital transformation. The fast-paced nature of today's digital business world demands that companies keep up with...
Keep Reading
SaaS A Comprehensive Guide to Software as a Service
  • Software Development
  • 18th Jul, 2023
  • Rinkal J.

SaaS: A Comprehensive Guide to Software as a Service

In order to keep pace with the rapid pace of digital transformation in today's world, organizations frequently explore creative methods of streamlining their operations and...
Keep Reading
Face Detection
  • Software Development
  • 16th Jul, 2021
  • Hardik D.

Real-time Face Detection on iOS

In an era dominated by technological advancements, face detection has emerged as a groundbreaking technology, transforming the way we interact with devices and systems. From enhancing...
Keep Reading