Increase your Gradle Build Speed
Rohan Lodhi
?? Senior Android Engineer | Java & Kotlin Expert | MVP, MVVM,MVI | API Integration | Passionate About Scalable Mobile Solutions
Long build times slow down your development process, so to overcome the long build process there are some important techniques to help you resolve build speed.
1. Always use the latest Gradle Plugin for Android
Android studio incrementally improves performance in newer gradle version so it is recommended to use latest one.Currently latest gradle plugin is 5.1.1.Newer version always resolved unwanted compilation errors or compatibility issues, follow the migration guide when upgrading your Gradle using android studio.
For more details about gradle latest version follow: https://gradle.org/whats-new/gradle-5/
2. Enable Offline mode, Gradle daemon and parallel build for the project
On slow network connections, while build the gradle it suffers problem to resolve the dependencies.You need to tell Gradle to avoid using network resources by using only the artifacts that it has cached locally.
To use Gradle offline when building with Android Studio, proceed as follows:
- Open the Preferences window by clicking File > Settings (on Mac, Android Studio > Preferences).
- In the left pane, click Build, Execution, Deployment > Gradle.
- Check the Offline work checkbox.
- Click Apply or OK.
- If you're building from the command line, pass the --offline option.
3. Don' use the dynamics versions
This means you tell gradle that I want the latest version of that library so every 24 hours gradle checks for the newer version or updates which causing your dependencies resolution time to increase.
Always use specific dependency versions!
4. Use Instant Run
Instant run is introduced in Android studio 2.0. The team said that Instant run in Android studio 3.0 has a lot of improvements and also better than the earlier version.
What is Instant Run?
In an ordinary build and run the whole APK file is updated and pushed to the device. But when using instant run only the updated part gets replaced in the device. Instant Run pushes updated code and resources to your connected device or emulator by performing a hot swap, warm swap, or cold swap. It automatically determines the type of swap to perform based on the type of change you made.
It may be faster than an ordinary build. But it requires the application to be running at the time of build below API 21.
5. Add below flags in gradle.properties
#1 org.gradle.jvmargs=-Xmx2048m
Increase the amount of memory allocated to the Gradle Daemon VM to 2 Gb.
# 2 org.gradle.daemon=true
This dramatically improves the performance of subsequent builds. This is background process that allows data and code to store in memory which can used for next build .
# 3 org.gradle.configureondemand=true
This feature can save you a couple of seconds from the Configuration phase of Gradle, as only ‘projects’ related to the current tasks are configured. This setting is relevant for multi- modules projects. When this property is set, Gradle configures modules that are only relevant to the requested tasks instead of configuring all of them, which is a default behavior.
# 4 org.gradle.parallel=true
This allows the execution of tasks from different projects in parallel. All modules in your Android app count as separate ‘projects’ in Gradle terms, so this feature is especially useful for modularised apps.
# 5 android.enableBuildCache=true
Build cache feature that can speed up build times (including full builds, incremental builds, and instant run) by storing and reusing files/directories that were created in previous builds of the same or different Android project.
Currently, the build cache contains only pre-dexed libraries; in the future, we will use it for caching other types of files as well.
# 6 org.gradle.caching=true
Turn on Gradle caching
Gradle caching is introduced in Gradle version 3.5. It reuses the outputs from the previous build. It can work across the projects and gives a high performance when used with Android Studio 3.0.
6. Disable Libraries from your debug builds (Ex. Crashlytics,PNG crunching )
As we know that we don't need crashlytics report in debug build , so it is better to disabled it.
android {
...
buildTypes {
debug {
ext.enableCrashlytics = false
}
}
PNG crunching
Disabled automatic conversion of PNG images to WebP . If you're using Android plugin 3.0.0 or higher, PNG crunching is disabled by default for only the "debug" build type.
android {
...
release {
crunchPngs false
}
}
At I/O ’17, google introduced most important tips from which you can fast your android gradle build.
For more details you can watch the video - how to speed up your android gradle builds.
For more details about latest gradle - What's new in Gradle 5.0
Happy Coding....