Android - Gradle Build Configuration
Android uses Gradle, an advanced build toolkit, to automate and manage the build process, while allowing you to define flexible custom build configurations. One of the most important file in Android application development is build.gradle
, which is used in the process of building an android app.
Let’s see how we can configure android builds with build.gradle
.
What is Gradle?
Gradle is an open source build automation system. It brings the convenience of a Groovy-based DSL along with the advantages of Ant and Maven. Android Gradle plugin was introduced as the build tool built into the first preview of Android Studio during Google I/O in May 2013. Since then lots of improvents have been added in Android Build Configuration.
Why configure the build?
With different build configurations, we can define different set of code and resources while reusing common parts in all build versions.
Android Build Configurations
With the help of Gradle and Android Plugin, we can configure the build in following ways:
Build Types
Using build types we can configure the different stages of development lifecycle. By default, there are two build types – debug and release. But we can add more such as uat depending on project requirement and available environments.
With different build types, we can specify that release build type may shrink, obfuscate, and sign your APK with a release key for distribution and debug build will only print logs. We can even specify different resources for different build types.
Product Flavors
Product/Build Flavours are useful when we have different versions for application such as premium or free. Flavors are a way to differentiate the properties of an app such as App Name. WIth different flavours, we can change app identifies such as,
The manifestPlaceholders
allows you to modify properties in your AndroidManifest.xml
file at build time.
In above example, we are specifying which app name to choose from strings.xml
file,
Dimensions are used to combine configurations from multiple product flavors. For eg, we may want to create difernt configurations according to api level and add some feature for that api level.
To ensure the target device receives the version of the app with the highest compatible API level, assign version codes in increasing value with API level.
Build Variants
With use of build type and product/build flavour, what we have actually generated are Build Variants. The number of build variants will be equal to the product of the number of flavors in each flavor dimension and the number of build types configured. Gradle will name them as follows:
- Build variant:
[minApi24, minApi21][free, premium][release, debug]
- Corresponding APK:
app-[minApi24, minApi21]-[free, premium]-[release, debug].apk
Build variants are the result of Gradle using a specific set of rules to combine settings, code, and resources configured in your build types and product flavors.
Conclusion
Gradle build configurations are very powerful and are useful to simplify the build process for different versions of app. The things stated above are just a glimpse in gradle world and there are tons of features to improve build process.