Skip to content

Instantly share code, notes, and snippets.

@ghale
Last active December 7, 2020 16:26
Show Gist options
  • Save ghale/00b43a3f028d7433d63a8be3c9d885bb to your computer and use it in GitHub Desktop.
Save ghale/00b43a3f028d7433d63a8be3c9d885bb to your computer and use it in GitHub Desktop.
Fix ProGuardTask to be cacheable
tasks.withType(ProGuardTask).configureEach { task ->
// Enable caching for the proguard tasks
outputs.cacheIf { true }
// Fix inputs and outputs so that caching works
def originalOutJars = []
def originalInJars = []
def originalLibraryJars = []
def originalConfigurationFiles = []
project.gradle.taskGraph.beforeTask {
if (it == task) {
// Right before the task executes, clear outJars (to prevent it being treated like a file) and
// create a synthetic output property which will be treated as a directory instead.
originalOutJars.addAll(getOutJarFileCollection())
task.outJarFiles.clear()
// This assumes that all of the outJars will be directories
originalOutJars.each { task.outputs.dir(it) }
// Right before the task executes, clear inJars and libraryJars (to prevent the original input from having an effect)
// and create a synthetic input property which will be treated as a classpath instead (i.e. only the contents of
// the jar files will affect cacheability).
originalInJars.addAll(getInJarFileCollection())
originalLibraryJars.addAll(getLibraryJarFileCollection())
originalConfigurationFiles.addAll(getConfigurationFileCollection())
task.inJarFiles.clear()
task.libraryJarFiles.clear()
task.configurationFiles.clear()
task.inputs.files(originalInJars).withPropertyName("inJars.workaround").withNormalizer(ClasspathNormalizer)
task.inputs.files(originalLibraryJars).withPropertyName("libraryJars.workaround").withNormalizer(ClasspathNormalizer)
task.inputs.files(originalConfigurationFiles).withPropertyName("configurationFiles.workaround").withPathSensitivity(PathSensitivity.RELATIVE)
// After snapshotting has taken place, and before the main task action executes,
// reset inJars and outJars so that they have the original values again
task.doFirst {
task.outJarFiles.addAll(originalOutJars)
task.inJarFiles.addAll(originalInJars)
task.libraryJarFiles.addAll(originalLibraryJars)
task.configurationFiles.addAll(originalConfigurationFiles)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment