tldr: I want some task configuration to run automatically whenever the Android Gradle Plugin is used. How?
I have a very large gradle project with many different build scripts. Some are android, some are java, some are other things entirely.
For all the scripts that build android targets, I need to run some task configuration. A convention of sorts. Is there a way to do this implicitly?
I’ve written the code and demonstrated that it works in a build script that imports an android plugin, but if I copy that same code into my main build.gradle.kts
file, I get failed class resolution, since com.android.gradle
is not available.
The code I need to run looks something like:
tasks.withType(KotlinCompile::class.java).configureEach {
val ext = project.extensions.findByType(com.android.build.gradle.LibraryExtension::class)
if (ext != null) {
libraries
.from({ AndroidGradleWrapper.getRuntimeJars(project.plugins.getPlugin(com.android.build.gradle.LibraryPlugin::class.java), ext) })
}
}
How do I apply that configuration automatically across my project where its needed?
I know I could write a custom convention plugin, but then we’d need to go to all our build scripts and import it. We might miss some or forget to import it in the future. I’d like to just make the script automatic.