- 3rd Jan, 2024
- Arjun S.
13th Oct, 2021 | Farhan S.
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+.
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.
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")
}
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"
]
}
}
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.
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'
}
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.
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
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:
Get insights on the latest trends in technology and industry, delivered straight to your inbox.