Android Gradle 8.0 plugin improves build times, requires updating third-party plugins and libraries


Google has announced the upcoming Android Gradle Plugin (AGP), version 8.0, which reduces build times by removing the Transform APIs and replacing them with the Artifacts API and Instrumentation API. All third-party plugins and applications that depend on it must migrate to the new APIs.

Transform API is removed to improve build performance. Projects that use the Transform API force AGP to use a less optimized stream for the build, which can cause significant regressions in build times.

Transformation APIs are a collection of APIs provided by the Android Gradle plugin to process intermediate artifacts of the build process, allowing to transform bytecode, synthesize new classes, or analyze the whole program.

A typical use of this feature is to instrument compiled classes to add traces, custom logging, performance analysis, etc. For example, the Hilt Android plugin uses Transform APIs to integrate Dagger dependency injection into an Android application and Firebase uses it for performance monitoring. Although transformation APIs are typically used in Gradle plugins, there are also a number of libraries that use them, such as the Realm mobile database. Also, transformation features can be useful for creating test classes.

For developers, this change means they may need to migrate their own codebases, as well as update any dependencies using the Transforms APIs to newer versions using the replacement APIs. While a number of plugins and libraries, including the mentioned Hilt, Firebase, and Realm, have been working on adopting the new APIs, the migration is not unlikely to cause developers headaches. In particular, upgrading to the latest version of a plugin/library may not be as simple as changing its version number in a configuration file, but may also require codebase changes who is using it.

To make the migration as easy as possible, Google is making new replacement APIs available in AGP 7.2 so that developers can adapt their code in preparation for version 8. AGP 7.2 will display an important warning for each use of the Transform APIs, which will cause a failure in AGP 8. To make the AGP warning provide more details about the component that is causing it, you can set android.debug.obsoleteApi=true in gradle.properties.

As mentioned, there are two separate APIs that replace Transform: the Instrumentation API, which can be used to transform bytecode, and the Artifacts API, which allows new classes to be added to an application. Google hasn’t provided specific information regarding the reduction in build time brought about by the new plugin. Anyway, some details about how it’s done can be inferred from Google docs.

Specifically, to make the instrumentation API fast, AGP constrains the instrumentation code to run independently on each class with no visibility of other classes. The first advantage this provides is that libraries can be instrumented in parallel rather than at the end of compilation. This also helps to speed up incremental builds, since a change in a class will only require that class and those that depend on it to be processed again.

When it comes to the Artifacts API, the key factor providing the performance benefit seems to be the isolation of the entire program analysis transformation into a specific API, called Artifacts.forScope. This API should be used with caution, Google says, because it has a high build performance cost and making it a separate part means you can handle cases where this sort of program-wide analysis isn’t used more effectively.