Application construction speed will directly affect the development efficiency, this article will take you through the transformation of an Android application: “Google Santa Tracker” provides you with ten tips to help you build Gradle faster. When we applied all the tips, the demo app built more than three times faster.

Second, before we turn on speed up tuning, let’s take a look at the three performance metrics:

  • Full build, that is, restart the debug version of the entire project;
  • Code incremental build, where we modify the Java/Kotlin code of the project;
  • Incremental build of resources refers to the changes we made to the resource files, adding and removing images and strings, etc.

After each tip was implemented, we compared the build times of the above three scenarios as our quantification criteria. Please note that the results of actual operations by developers may differ from those in this article due to different project sizes and development environments.

Tip 1: Use the latest version of the Android Gradle plugin

Starting with version 3.0, we will distribute the new Android Gradle plugin through Google () ‘s Maven repository, Therefore, we need to add Google () to our repository for the latest plugins (currently, new Projects in Android Studio will have Google () ‘s Maven repository by default).

Tip 2: Avoid activating older versions of Multidex

Enabled multidex

If you’re building from Android Studio’s Run/debug button, you don’t have to worry about this. Newer versions of Android Studio automatically detect connected devices and emulators, and native Multidex support is provided if the system API level is greater than 21. The minimum API level (minSdkVersion) in the project is also ignored.

Developers who are used to building projects from the command line window should try to avoid this problem by configuring a new productFlavor, setting the minimum API level for the project to 21 or above, and calling assembleDevelopmentDebug from the command line.

Tip 3: Disable Multiple APK builds

Multiple APK

Build variant

Tip 4: Minimize resource files

When your application contains a lot of localized resources or special resources for different pixel densities, you may want to apply this trick to speed up the build — minimizing the amount of resources packaged into the application during development.

By default, the build system packages all declared or used resources into APK, but we may only use one of them during development. In this case, we need to use resConfigs() to specify the resources, such as language version and screen pixel density, that will be used to build the development version.

Tip 5: Disable PNG compression

Note that API level 15 and above can support opaque WebP images, while transparent WebP images require API level 18 and above.

One particular question about the size of APK – when we compared the size of APK with and without PNG compression, we found that the size did not change much, indicating that the PNG images used in the project were fully optimized before being imported and PNG compression was unnecessary.

Tip 6: Use Apply Changes

Tip # 7: Avoid passive change

Tip 8: Don’t use dynamic version ids

Even if you don’t particularly care about these performance losses, it’s still a risk — depending on a library version update, your build is so uncertain that two weeks later you’re building a completely different project because the library code updates aren’t visible to developers.

Tip 9: Gradle memory allocation tuning

At the same time, since Android Gradle 2.1, dex is already in the process by default, so if you set javaMaxHeapSize, you can delete it.

Tip 10: Enable Gradle build cache

Gradle build cache

conclusion

Code warehouse
The official documentation

Click here toSubmit product feedback suggestions