Optimising Android build by migrating to Android Gradle Plugin v8.2

Android May 31, 2024

In the constantly evolving world of Android development, keeping up with the latest tools and technologies is crucial. This is essential for building high-performance and competitive apps. Halodoc consistently focuses on leveraging the latest technologies. One of the essential components of Android development is the Android Gradle Plugin (AGP), which plays a pivotal role in compiling, building, and packaging Android applications. With each new version, Google introduces improvements, optimizations, and new features aimed at enhancing the developer experience and app performance.

In this blog post, we'll delve into the benefits and steps involved in migrating to Android Gradle Plugin v8.2, and how it has significantly improved the performance of the Halodoc app.

Understanding Android Gradle Plugin (AGP):

The Android Gradle Plugin (AGP) is a set of build tools provided by Google that integrates with the Gradle build system to automate and streamline the process of building Android apps. It handles tasks such as compiling source code, managing dependencies, packaging resources, and more. Each new version of AGP introduces enhancements and optimizations that can positively impact the development workflow and app performance.

Why we migrated to AGP 8.2:

Android Gradle Plugin v8.2 brings several improvements and optimizations that can contribute to better app performance and a smoother development experience. Here are some key benefits:

1.Improved Build Speed:

AGP 8.2 introduces optimizations to the build process, resulting in faster build times. By leveraging incremental build improvements and optimized task execution, developers can significantly reduce the time it takes to build Android apps, leading to a more efficient development workflow.

2.Enhanced Dependency Management:

AGP 8.2 introduces enhancements to dependency resolution and caching mechanisms, reducing the time spent resolving dependencies during the build process. This leads to quicker builds and fewer potential issues related to dependency conflicts. The improved dependency management system also makes it easier to manage and resolve transitive dependencies, which can help prevent version conflicts and reduce the risk of compatibility issues.

3. Android Resource Optimization:

AGP 8.2 introduces optimizations for handling Android resources, such as layouts, drawables, and assets. These optimizations help to reduce the size of the APK file and improve runtime performance by eliminating unnecessary resources and optimizing resource packaging. This can lead to faster app loading times, reduced memory footprint, and better overall app performance.

4. Compatibility and Stability:

Migrating to the AGP 8.2 ensures compatibility with the latest versions of Android Studio and the Android Gradle Plugin ecosystem. It also includes bug fixes and stability improvements, providing a more reliable development environment. This helps to reduce the risk of compatibility issues and ensures that developers can take advantage of the latest features and improvements offered by the Android development tools.

5. Improved R8 Optimization:

AGP 8.2 includes enhancements to the R8 code shrinker and optimizer, which helps to reduce the size of the APK file by removing unused code and resources. This optimization can lead to improved app performance, reduced memory footprint, and faster app loading times. The R8 tool also offers advanced code optimization techniques, such as inlining and method devirtualization, which can further improve app performance.

6. Better Support for Android Jetpack:

AGP 8.2 provides better support for the Android Jetpack libraries, which are a suite of components, tools, and guidance designed to help developers follow best practices and build high-quality apps more easily. Jetpack includes libraries for architecture, UI, and behavior components, as well as tools for testing, debugging, and performance optimization. With improved support for Jetpack, developers can more easily leverage these powerful tools and libraries to build robust and efficient apps.

7. Improved Incremental Build Support:

AGP 8.2 includes improvements to incremental build support, which means that only the necessary parts of the project are rebuilt when changes are made. This can significantly reduce build times, especially for large projects or when making small changes. Incremental builds can save developers a considerable amount of time and improve overall productivity.

8. Better Lint Support:

AGP 8.2includes improvements to the Lint tool, which helps to detect and highlight potential issues in your code, such as performance problems, compatibility issues, and security vulnerabilities. Better Lint support can help developers identify and fix these issues more easily, leading to higher-quality code and better app performance.

9. Improved Build Caching:

AGP 8.2 introduces improvements to build caching, which can significantly speed up builds by reusing previously built outputs. This feature can be particularly beneficial for larger projects or when building on multiple machines, as it reduces the need to rebuild everything from scratch.

10. Better Support for Dynamic Features:

AGP 8.2 improves support for dynamic features, which allow developers to create modular and customizable app experiences. Dynamic features enable users to download only the app components they need, reducing the initial app size and improving the overall user experience.

11. Improved Build Insights:

AGP 8.2 includes improvements to build insights, which provide detailed information about the build process, including timing and performance metrics. These insights can help developers identify bottlenecks and optimize their build process for better performance.

Migration Journey from AGP 7.6 to 8.2:

1. Update Gradle Wrapper:

First step is to update the Gradle wrapper version to AGP 8.2. This can be done by modifying the gradle.wrapper.properties file in the project directory.

2. Update Gradle Plugin Version:

Next, update the Gradle plugin version in the project's  build.gradle.kts file to  com.android.tools.build:gradle:8.2.0

This can also be done by running the following command on terminal:

./gradlew wrapper --gradle-version 8.2

3. Namespace required in module-level build script:

It is mandatory to set the namespace in the module-level build.gradle.kts file, rather than the manifest file.

4. R classes are not transitive by default for library modules:

From AGP 8.0, in the gradle.properties file, the default value of android.nonTransitiveRClass is set to true when not specified. So it generates R classes for the resources defined in its own module only and not from dependencies.

Previously, we could directly access resources from dependencies using a simplified syntax. For example, accessing a font resource was looking like this:

However, with AGP 8.0.0, we now need to specify the full namespace of the module where the resource resides. This means we must include the module's package name to avoid conflicts between multi module resources and ensure proper resolution. This namespace change is to be done across all modules in our project. This change reduces size of R class for a module and hence improves the build time.

5. Build option default values:

Starting with AGP 8.0, the default values for below flags have changed to improve build performance.

AGP 8.0 doesn't generate BuildConfig by default. We need to specify this option using the DSL in the app level  build.gradle.kts file.

AGP 8.0 enables R8 full mode by default. If you want to change this values then, we can change this option in gradle.properties file.

android.enableR8.fullMode = false

6. JDK 17 migration:

When using Android Gradle Plugin 8.0 to build app, JDK 17 is required to run Gradle.

We need to specify this option in the app and module level  build.gradle.kts file.

Along with this, CI/CD pipeline environment should also be updated to JDK 17.

7. Jacoco Report configuration:

With AGP 8.2 update, required is a property, not a method and destination is replaced by outputLocation .

Once the migration is complete, run Gradle tasks and tests to ensure that the project builds successfully and all functionality is intact.

Conclusion:

Migrating to Android Gradle Plugin v8.2 offers numerous benefits, including improved build speed, enhanced dependency management, resource optimization, compatibility/stability improvements, and better support for the latest Android development tools and libraries. By leveraging these improvements, developers can streamline workflow, reduce build times, and deliver high-performance apps more efficiently.

At Halodoc, we were successfully able to reduce the App size by 1.62 Mb with migration to AGP 8.2 and this lead to an approximate reduction in application's compile time by about 7-10%.

Keeping development tools and dependencies up-to-date is essential for staying competitive in the fast-paced world of Android app development, and AGP 8.2 is a significant step forward in that direction. The Halodoc team's successful migration to this latest version demonstrates our commitment to delivering a top-notch user experience while embracing the latest advancements in Android development.

References:

Android Gradle Plugin 8.0.0 (April 2023) | Android Developers
Release notes of Android Gradle Plugin 8.0.0
Android Gradle Plugin 8.2.0 (Nov 2023) | Android Developers
Release notes of Android Gradle Plugin 8.2.0

Join us

Scalability, reliability and maintainability are the three pillars that govern what we build at Halodoc Tech. We are actively looking for engineers at all levels and if solving hard problems with challenging requirements is your forte, please reach out to us with your resume at careers.india@halodoc.com.

About Halodoc

Halodoc is the number 1 all around Healthcare application in Indonesia. Our mission is to simplify and bring quality healthcare across Indonesia, from Sabang to Merauke. We connect 20,000+ doctors with patients in need through our Tele-consultation service. We partner with 3500+ pharmacies in 100+ cities to bring medicine to your doorstep. We've also partnered with Indonesia's largest lab provider to provide lab home services, and to top it off we have recently launched a premium appointment service that partners with 500+ hospitals that allow patients to book a doctor appointment inside our application. We are extremely fortunate to be trusted by our investors, such as the Bill & Melinda Gates Foundation, Singtel, UOB Ventures, Allianz, GoJek, Astra, Temasek, and many more. We recently closed our Series D round and In total have raised around USD$100+ million for our mission. Our team works tirelessly to make sure that we create the best healthcare solution personalised for all of our patient's needs, and are continuously on a path to simplify healthcare for Indonesia.

Priya Bhat

SDE, Android