This repository is maintained by the Gradle Enterprise Solutions team, as one of several publicly available repositories:
- Gradle Enterprise Build Configuration Samples
- Gradle Enterprise Build Optimization Experiments
- Gradle Enterprise Build Validation Scripts
- Gradle Enterprise Open Source Projects
- Common Custom User Data Maven Extension
- Common Custom User Data Gradle Plugin
- Android Cache Fix Gradle Plugin (this repository)
Android Cache Fix Gradle Plugin
Some Android plugin versions have issues with Gradle's build cache feature. When applied to an Android project this plugin applies workarounds for these issues based on the Android plugin and Gradle versions. For other versions, please see older versions.
- Supported Gradle versions: 7.0+
- Supported Android Gradle Plugin versions: 7.0.4, 7.1.3, 7.2.2, 7.3.1, 7.4.2, 8.0.2, 8.1.0 and 8.2.0-alpha14
- Supported Kotlin versions: 1.7.0+
We only test against the latest patch versions of each minor version of Android Gradle Plugin. This means that although it may work perfectly well with an older patch version (say 7.0.1), we do not test against these older patch versions, so the latest patch version is the only version from that minor release that we technically support.
The Android cache fix plugin is compatible with the Gradle Configuration Cache when used in conjunction with Android Gradle Plugin 4.2.0 and above. Using the configuration cache with earlier versions of the Android Gradle Plugin is not supported.
Should I use this?
Take a look at the list of issues especially unresolved issues. If any of these apply to your project, you can use this plugin to solve them.
How can I discover issues?
You can discover issues by using the task input comparison tool in Gradle Enterprise. More information about how to diagnose cache misses here. You can compare the inputs of a build that seeds the build cache - typically CI - with a build that consumes from the build cache like a local developer build. If you discover an issue related to the Android Gradle Plugin, please file an issue in the Android Bug Tracker. You can also file an issue here and we can see if a workaround is possible.
Applying the plugin
This plugin should be applied anywhere the com.android.application
or com.android.library
plugins are applied. We recommend adding the plugin to your project's conventions plugin.
Kotlin
// in build.grade.kts for convention plugin build
dependencies {
// ...
implementation("org.gradle.android.cache-fix:org.gradle.android.cache-fix.gradle.plugin:2.7.2")
// ...
}
// in com.myconventions.build.gradle.kts
plugins {
id("com.android.application") // or "com.android.library"
// Add this next line to your existing convention plugin.
id("org.gradle.android.cache-fix")
}
Groovy
// in build.grade for convention plugin build
dependencies {
// ...
implementation("org.gradle.android.cache-fix:org.gradle.android.cache-fix.gradle.plugin:2.7.2")
// ...
}
// in com.myconventions.build.gradle
plugins {
id 'com.android.application' // or 'com.android.library'
// Add this next line to your existing convention plugin.
id 'org.gradle.android.cache-fix'
}
If you are not using convention plugins and would like a quick way of testing the plugin you can alternatively place it in the root project's build.gradle (change '2.7.2' to the latest version of the cache fix plugin here). We discourage this approach because it uses cross project configuration.
Kotlin
plugins {
id("org.gradle.android.cache-fix") version "2.7.2" apply false
}
subprojects {
plugins.withType<com.android.build.gradle.api.AndroidBasePlugin>() {
apply(plugin = "org.gradle.android.cache-fix")
}
}
Groovy
plugins {
id "org.gradle.android.cache-fix" version "2.7.2" apply false
}
subprojects {
plugins.withType(com.android.build.gradle.api.AndroidBasePlugin) {
project.apply plugin: "org.gradle.android.cache-fix"
}
}
Note that if you are currently exporting schemas with the Room annotation processor, you will need to change how you specify the output directory according to the instructions here.
List of issues
You can take a look at the list of issues that the plugin fixes by looking at the classes in org.gradle.android.workarounds
. It contains a number of Workaround
implementations annotated with @AndroidIssue
. The Javadoc has a short description of the problem, and the annotation gives information about when the problem was introduced, what is the first version of the Android plugin that fixes it, and there's a link to the issue on Android's issue tracker:
/**
* Fix {@link org.gradle.api.tasks.compile.CompileOptions#getBootClasspath()} introducing relocatability problems for {@link AndroidJavaCompile}.
*/
@AndroidIssue(introducedIn = "3.0.0", fixedIn = "3.1.0-alpha06", link = "https://issuetracker.google.com/issues/68392933")
static class AndroidJavaCompile_BootClasspath_Workaround implements Workaround {
// ...
}
Unresolved Issues
Please vote for the linked issues if you are experiencing them in your project.
Fixed by the Android Cache Fix plugin, but unresolved in any current or upcoming preview release of the Android Gradle Plugin:
- Room annotation processor causes cache misses, doesn't declare outputs, overlapping outputs, etc: https://issuetracker.google.com/issues/132245929
Not fixed by the Android Cache Fix plugin since it has no workaround:
- CompileLibraryResourcesTask outputs contain absolute paths: https://issuetracker.google.com/issues/282761461
Not fixed by the Android Cache Fix plugin since it has no workaround but is fixed in Android Gradle Plugin 8.0.0 or newer:
- MergeResources is not relocatable: https://issuetracker.google.com/issues/246529491
Implementation Notes
RoomSchemaLocationWorkaround
Most of the workarounds in this plugin should apply to your project without any changes. However, one workaround requires some extra configuration. The Room schema location workaround allows you to specify an output directory for Room schema exports without breaking caching for your Java and/or Kapt tasks where the annotation processor has been configured. There are various issues with how this annotation processor works (see https://issuetracker.google.com/issues/132245929 and https://issuetracker.google.com/issues/139438151) which this workaround attempts to mitigate. However, in order to do so in a manageable way, it imposes some restrictions:
- The schema export directory must be configured via the "room" project extension instead of as an explicit annotation processor argument. If an explicit annotation processor argument is provided, an exception will be thrown, instructing the user to configure it via the extension:
Kotlin
room {
schemaLocationDir.set(file("roomSchemas"))
}
Groovy
room {
schemaLocationDir = file("roomSchemas")
}
- There can only be a single schema export directory for the project - you cannot configure variant-specific export directories. Schemas exported from different variants will be merged in the directory specified in the "room" extension.
Ksp
Since version 2.7.0, RoomSchemaLocationWorkaround
supports Kotlin Symbol Processing(Ksp). Like Kapt, applying the Room
processor with Ksp was causing cache misses. The workaround allows you to specify an output directory for Room schema
exports. The schema export directory must be configured via the "room" project extension instead of the Ksp
configuration:
Kotlin
room {
schemaLocationDir.set(file("roomSchemas"))
}
Groovy
room {
schemaLocationDir = file("roomSchemas")
}
Supported Ksp versions: 1.7.20-1.0.8+
MergeNativeLibs, StripDebugSymbols, MergeJavaResources, MergeSourceSetFolders, BundleLibraryClassesJar, DataBindingMergeDependencyArtifacts, LibraryJniLibs and ZipMerging Workarounds
It has been observed that caching the MergeNativeLibsTask
, StripDebugSymbols
, MergeSourceSetFolders
, BundleLibraryClassesJar
, DataBindingMergeDependencyArtifacts
, LibraryJniLibs
and ZipMergingTask
tasks rarely provide any significant positive avoidance savings. In fact, they frequently provide negative savings, especially when fetched from a remote cache node. As such, these workarounds disable caching for these tasks.
Older Android Gradle Plugin Versions
Use Android Cache Fix Plugin 2.4.6 when using an older Android Gradle Plugin version.
- Supported Gradle versions: 5.4.1+
- Supported Android Gradle Plugin versions: 3.5.4, 3.6.4, 4.0.1, 4.1.3, 4.2.2
Older Kotlin Gradle Plugin Versions
Use Android Cache Fix Plugin 2.6.0 when using an older Kotlin Gradle Plugin version.
- Supported Kotlin versions: [1.3.72-1.5.32]
License
The Android Cache Fix Gradle plugin is open-source software released under the Apache 2.0 License.