Android Gradle Plugin 8.0 Speeds Up the Building Process

The upcoming version 8.0 of the Android Gradle Plugin (AGP), which Google has announced, will cut down on the amount of time it takes to build applications by doing away with the Transform APIs and replacing them with the Artifacts API and the Instrumentation API. The migration to the new APIs is mandatory for any and all third-party plugins and applications that rely on the older ones.

Android Gradle Plugin 8.0 Speeds Up the Building Process

The removal of the Transform API is being done in order to speed up the building process. AGP is forced to use a flow for the build that is less optimized when it is forced to work with projects that use the Transform API. This can result in significant build time regressions.

You may also like: Learn about Java methods and their benefits

The Android Gradle plugin offers a collection of APIs known collectively as the Transform APIs for the purpose of processing intermediate artifacts produced during the build process. This makes it possible to transform bytecode, synthesize new classes, or analyze the entire program.

Instrumenting compiled classes to add traces, custom logging, performance analysis, and other similar features is a common example of how this feature can be put to use. For instance, the Android Hilt plugin uses the Transform APIs to embed Dagger dependency injection into an Android app, and Firebase uses it for performance monitoring. Both of these examples can be found here. Although the Transform APIs are most frequently utilized in Gradle plugins, there are a number of libraries, such as the mobile database Realm, that also make use of these APIs. In addition, features of the Transform library might be useful when developing test classes.

This change indicates that developers may need to migrate their own codebases and update any dependencies that use the Transforms APIs to newer versions using the replacement APIs. In addition, they may need to update any dependencies that use the replacement APIs. Although a number of plugins and libraries, such as the ones mentioned above—Hilt, Firebase, and Realm—have been working on adopting the new APIs, it is not impossible that the migration process will cause some frustration for developers. Specifically, upgrading to the most recent version of a plugin or library might not be as easy as changing its version number in a configuration file. Instead, it might necessitate making changes to the codebase that is currently making use of the plugin or library.

Developers will be able to modify their code in preparation for version 8 because Google is making the new replacement APIs available in AGP 7.2. This is being done to make the migration as simple and painless as possible. Each time the Transform APIs are used, AGP 7.2 will issue a prominent warning, which will result in a failure in AGP 8. In the file, you can add the line android.debug.obsoleteApi=true to make the AGP warning provide more specific information about which component is causing it.

As was previously mentioned, there are two distinct APIs that can take the place of Transform. These APIs are the Instrumentation API, which can be used for transforming bytecode, and the Artifacts API, which enables adding new classes to an application. Google has not provided any specific information about the reduction in build time brought about by the new plugin. In any case, a number of specifics regarding how it is accomplished can be deduced from Google docs.

To be more specific, AGP restricts the instrumentation code to run independently on each class without any visibility of other classes. This ensures that the Instrumentation API remains as quick as possible. The first advantage this offers is that instrumentation of libraries can take place concurrently rather than at the very end of the compilation process. The fact that a modification to a class will only necessitate the processing of that class and the classes that depend on it makes it possible for it to contribute to the acceleration of incremental builds.

When it comes to the Artifacts API, the key factor that appears to be responsible for bringing the performance benefit appears to be the isolating of the transformation of whole-program analysis into a specific API known as Artifacts.forScope. According to Google, this application programming interface (API) should be used with caution because it has a high build-performance cost, and making it a separate part means you can handle cases where this kind of whole-program analysis is not used in a more efficient manner.

Make the necessary preparations for the Android Gradle plugin 8.0 API changes in your Android project.

In version 8.0 of the Android Gradle plugin (AGP), the Transform APIs will be removed. This change will make the build process faster and will provide more stable APIs. The vast majority of use cases have replacement APIs that are available beginning with version 7.2 of the AGP. Continue reading for further information.

The top request from the Android developer community has been to increase build speed while ensuring that the Android Gradle plugin (AGP) has a robust, stable, and well-supported API.

We have updated the Instrumentation API and stabilized the Artifacts API in order to improve the overall speed of the build process beginning with AGP 7.2. These APIs take the place of the Transform APIs for common use cases. The Transform APIs were the cause of longer build times and were removed in AGP 8.0.

Guidance for Gradle plugins

In order to speed up the building process, we partitioned the functionality of Transform into the following APIs, which were designed with the most common applications in mind:

Using ASM callbacks, the Instrumentation Application Programming Interface (API) enables users to transform and analyze compiled app classes. Use this API, for instance, to add custom traces to methods or classes so that you can log additional or customized information.

During the build process, the Artifacts API provides access to any files or directories that are produced by AGP, regardless of whether they are temporary or permanent. To do the following, use this API:

It would be helpful if the application had some additional generated classes, such as glue code for dependency injection.

Implement transformations determined by an analysis of the entire program whenever it is possible to transform all classes concurrently as a single task. This feature is only accessible beginning with the release of AGP 7.4.0-alpha06. The instructions for doing so can be found in the build.gradle.kts file that is part of the “modifyProjectClasses” Gradle recipe.

Please refer to the Transform API update note as well as our Gradle recipes for specific examples of how to use the replacement APIs.

Guidance for apps

Before you update your app to version AGP 8.0, check to see that all of your plugins have been brought up to date to comply with the new version. Please create a bug report that includes a link to this post and send it to the authors of the relevant plugins if the plugins do not comply with the standards.

One of the most widely used plugins, the Hilt Gradle plugin, is among the many that have already migrated to make use of these new APIs.

Android Studio Tutorial : How To Update Android Gradle Plugin

Leave a Comment