Why build time is so important?
As an android developer, we are spending a lot of time looking at your screen and waiting for Gradle build to finish. In the new Android studio, it has been a lot faster than the previous version but still, there is a lot of scopes to fasten it so Let’s Make it Fast by Doing some quick fixes in Android Studio.
These are the few tips which will help you to reduce your build time in Android Studio:
- Always use latest android Gradle Plugin -Android studio incrementally improves performance in newer Gradle version so it is recommended to use the latest one. By default, Android Studio now uses a new DEX compiler called D8. By default, Android Studio now uses D8(You can also disable it ). DEX compilation is the process of transforming .classbytecode into .dex bytecode for the Android Runtime (or Dalvik, for older versions of Android). Compared to the previous compiler, called DX, D8 compiles faster and outputs smaller DEX files, all while having the same or better app runtime performance. This newer plugin version will lead to up to 25% reduce the timing of build.
- Disable PNG Crunching- Android build tools perform PNG size optimization by default, It’s good for release build but for development, it increases build time. Either use WEBP instead of PNG or disable it in development flavor.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
android { buildTypes { release { // Disables PNG crunching for the release build type. crunchPngs false } } // If you're using an older version of the plugin, use the // following: // aaptOptions { // cruncherEnabled false // } } |
3. Instant RUN- instant run creates a lot of problems till Android Studio 3.0, now it is stable and works on devices sdkVersion ≥21. It also has applied changes that will reflect changes directly into the live process rather than restarting the app.
4. Enable Gradle caching-Caching Gradle will caches task outputs from any previous build from any location. set org.gradle.caching= true in Gradle.properties file to enable caching.
1 2 3 |
// To re-enable the build cache, either delete the following // line or set the property to 'true'. android.enableBuildCache=true |
5. Multi-Module Project- Modularity also increase Gradle build time by building modules in parallel.
1 2 3 4 5 6 7 8 9 |
// Add module in build.gradle implementation project(":login") implementation project(":feature1") implementation project(":feature2") // setting.gradle include ':app', ':login', ':feature2', ':feature1', ':feature1app', ':feature2app' |
6. Don’t use dynamic dependency version- It will lead to check studio every time whether a new library version of this is available.
1 2 3 |
//When specifying dependencies, you should not use dynamic version numbers,such as 'com.android.tools.build:gradle:3.+' // Using this feature can cause unexpected version updates and difficulty resolving version differences. |