The app build speed process can drag projects at times. However, because there is no one template for building applications, there are some tips to help optimize your build speed.
As an Android developer building with Android Studio, there are ways to improve build speed outside the Android ecosystem for quality app development:
- Continuously testing your app during the build process. This enables you to catch any bugs before your app stops suddenly working.
- Carefully review the Android developer’s guidelines before submitting it to Google Play Store to avoid rejection.
- Don’t overlook your hardware, ensure your app can run on all relevant devices. This means sometimes you need to avoid cutting-edge features for widespread app compatibility.
While, with Android Studio, here are two general ways to optimize build speed that we will cover in this article: profile your build and ways to optimize your build configuration
Profile your build
For those building large projects or executing any custom build logic, you will need to dive deeper to optimize build speed by profiling how long the Gradle takes to implement build tasks.
There are two options for profiling your build outside the Android Studio.
Use gradle-profiler tool
This is a robust tool for analyzing your build deeply. It allows you to create build scenarios and run them multiple times to ensure the results are as desired.
Before you can use the Gradle profiler, you need some project specifications met. These include:
- Gradle versions
- Plugin versions
- Per-plugin options to further optimize performance
- JVM settings ( heap size, garbage collection, etc)
- Number of Gradle workers ( org.gradle.workers.max)
To get started with the Gradle profiler tool, you need to install it using these instructions, run it: gradle-profiler --benchmark --project-dir _<root-project>_ :app:assembleDebug
There are several scenarios you may want to benchmark beyond the HTML report generated may include:
- String edits to simulate dealing with translation work
- API changes in a module that is used throughout your project
- Clean builds to simulate changes to the build itself
- Layout edits to simulate iterating on UI work
- Code changes in a method body in a class where you do most of the work
Use the - - profile option
You will need a build profile report to inspect for optimization opportunities. To generate and view the build profile from the Gradle command line, follow these steps:
Step 1: Open the command line terminal at your root project
Step 2: Perform a clean build between each build because Gradle skips some tasks when inputs to these tasks don’t change. Run the command line clean to ensure that you profile the full build process.
Step 3: Execute a debug build on one of your product flavors with the following flags:
gradlew --profile --offline --rerun-tasks assembleFlavorDebug
Step 4: After the build completes, the Project window is to navigate to the project-root/build/reports/profile/ directory.
Step 5: To get the report, right-click on profile-timestamp.html file and select Open in Browser > Default.
Optimize your build configuration
Another option to improve the build speed for your Android Studio project is building configuration. Here are some ways to optimize the build speed.
Constantly update your Android tools
In case you are using old Android tools, the first step is to update to the latest version. Here are two essential tools to constantly keep updated.
Experiment with putting the Gradle plugin last
In case you need a third-party plugin that is resolved by gradlePluginPortal, you can experiment with it, consider placing them last in the repository block in your settings. gradle file. This minimizes the number of redundant plugin searches and improves your build speed.
Create library modules
Modulizing your code allows the build system to compile only the modules you modify for future builds.
However, you first need to create an Android library. This includes everything you need to build an Android app including resource files, Androud manifest, and source code.
The library module is beneficial in the following ways:
- Reduce redundancy when building a similar application; you can use the same activities, UI layouts, and more
- Same time on configuration when building an app that exists in multiple APK variations such as paid or free versions.
Disable PNG crunching
In case you don’t convert your PNG images into WebP, you can still speed up the app build process by disabling the PNG crunching. For those using the Android Gradle plugin 3.0 and above, the PNG crunching is disabled by default. To disable, add the following to your gradle.build file
android {
buildTypes {
getByName("release") {
// Disables PNG crunching for the "release" build type.
isCrunchPngs = false
}
}
}
Use experimental configuration cache
Configuration cache is a feature that improves the build performance significantly by caching the result of the configuration phase. With this, Gradle can skip the phase entirely without any effects on the build configuration.
However, before running the configuration cache, Gradle checks that none of the build configuration inputs has changed. These inputs include:
- Build scripts
- Settings scripts
- Configuration files accessed using value suppliers such as providers
- Gradle properties used during the configuration phase
- Init scripts
- buildSrc and plugin included build inputs, including build configuration inputs and source files.
- Environment variables used during the configuration phase
- System properties used during the configuration phase
To enable the configuration cache, do the following;
- Check that all project plugins are compatible using the build analyzer
- Add this code to the gradle.properties file.
org.gradle.unsafe.configuration-cache=true
# Use this flag carefully, in case some of the plugins are not fully compatible.
org.gradle.unsafe.configuration-cache-problems=warn
###
Consider non-transitive R classes
Use R classes to improve app build speed when dealing with multiple modules. If you are using Android Gradle 8.0 or higher, the non-transitive R classes are set by default.
Additionally, applying the R classes helps prevent resource duplication. This ensures each R module contains references from its resources without pulling them from its dependencies.
Create tasks for custom-build logic
You can move some build logic into a task to help ensure they run only when required.
These results can be cached for the subsequent builds making the build logic run in parallel if you enable parallel project execution. As a result, parallel execution will improve the build speed.
Use static dependency versions
Avoid using dynamic dependency versions when declaring dependencies in your build. gradle. The disadvantages of using the dynamic dependency version include;
- Slower builds caused by Gradle constantly checking for updates
- Difficulty resolving version differences
- Unexpected version updates
Increase the JVM heap size
Increasing the Java Virtual Machine (JVM) heap size optimizes the build speed. Here are the steps to increase application server JVM heap size:
- Log in to the Application Server Administration Server.
- Navigate to the JVM options.
- Edit the -Xmx256m option.
- This option sets the JVM heap size.
- Set the -Xmx256m option to a higher value, such as Xmx1024m.
- Save the new setting.
Disable Jetifier flag
The Jetifier tool lets you migrate an individual library directly instead of using the Android Gradle plugin bundled with Android Studio.
However, since most projects use AndroidX libraries directly, you can remove the Jetifier flag for better build speed performance.
Here is how to disable the Jetifier flag: set android.enableJetifier=false
in your gradle.properties
file.
Convert images to WebP
WebP is an image format for the web. The WebP lossless images are 26% smaller than the PNGs and more than 25% smaller compared to JPEs.
You can improve build speed by reducing the image file sizes. The downside however is an increase in the CPU usage when decompressing the WebP images.
Experiment with the JVM parallel garbage collector
The role of the JVM parallel garbage collector is set to find and delete any garbage from the memory space. It does this task in two steps:
- Mark by identifying which spaces of the memory is in use and those that aren’t.
- Sweep by removing the identified garbage in the memory
Aside from optimizing the app build speed, other benefits of experimenting with JVM parallel garbage collector include:
- Avoid manual memory deallocation
- Automate memory leak management
- Reduce overhead in handling dangling pointer
FAQs on Optimizing build speed
Why is Android Studio build so slow?
Android Studio build is so slow probably because of low memory space. The official system requires a minimum of 3 GB RAM to run smoothly. To increase Android Studio build speed, consider the following;
- Using Offline Gradle
- Disabling Instant Run
- Increasing memory in gradle.properties file
- Increasing the RAM to at least 5 GB
How to check build time in Android Studio?
To check the build time in Android Studio, click Tasks impacting build duration on the overview page or select Tasks from the dropdown. If your project uses Android Gradle Plugin 8.0 or higher the tasks are grouped by category by default, so it's easier to identify areas that impact build duration.
To calculate build time in Android Studio, use these steps:
- gradle plugin with BuildListener
- track start time: (gradle as GradleInternal).services[BuildStartedTime::class.java].startTime
- track end time BuildListener::buildFinished
- calculate and send to our server
How do I reduce my Gradle build time?
Here are ways to reduce your Gradle build time:
- Constantly update your Android tools
- Experiment with putting the Gradle plugin last
- Create library modules
- Disable PNG crunching
- Use experimental configuration cache
- Consider non-transitive R classes
- Create tasks for custom-build logic
- Use static dependency versions
- Increase the JVM heap size
- Disable Jetifier flag
- Convert images to WebP
- Use non-constant R classes
- Experiment with the JVM parallel garbage collector
Does Gradle build faster than Maven?
Gradle build is faster than Maven in several ways according to this Gradle vs Maven performance analysis.
- For building a cache, Gradle build is 3 to 30 times faster than Maven
- When building incremental changes, Gradle is between 7 and 85 times faster than Maven
Use tips to optimize build speed
Being able to build and ship applications fast is essential to your business or individual success. However, sometimes the development tools you use can slow your build process.
For instance, without the right setup and plugin integration, using Gradle build is hard at times. Instead, increase your Gradle build speed using the above tips and strategies.
Until the next content piece, keep learning, coding, and shipping great applications.