Precompiled script plugins

I am trying to create a precompiled script plugin in buildSrc, and the plugin just contains the necessary stuffs required for an android library module


val libs = the<LibrariesForLibs>()

plugins {
    id("com.android.library")
}

android {
    compileSdk = libs.versions.compileSdk.get().toInt()
    defaultConfig {
        minSdk = libs.versions.minSdk.get().toInt()
    }
}

In the buildSrc build.gradle, added the necessary dependencies for the android plugin

dependencies {
    implementation("com.android.tools.build:gradle:8.0.2")
    implementation(files(libs.javaClass.superclass.protectionDomain.codeSource.location))
}

My root build.gradle has the below code


plugins {
    //trick: for the same plugin versions in all sub-modules
    id("com.android.application").version("8.0.2").apply(false)
    id("com.android.library").version("8.0.2").apply(false)
    kotlin("android").version("1.8.21").apply(false)
    kotlin("multiplatform").version("1.8.21").apply(false)
}

Since the library plugin is applied at root level, when i add the android.tools.build dependency in buildSrc build.gradle, its failing as its unable to verify the versions used in the plugin. (this plugin could not be satisfied because the plugin is already on the classpath with an unknown version, so compatibility cannot be checked)

What’s the right way to go abt this problem. Can someone throw some light on this issue. Removing the plugins from root build.gradle fixes the compilation issue, but then it creates issues during runtime. Appreciate your help here

It shouldn’t cause any runtime issues, as the first two plugin lines in your root build script are effectively unused anyway.

As you have the AGP in your buildSrc dependencies, it is part of a class loader that is higher in the hierarchy than the actual build script classpath class loaders.

If you do not have that dependency in buidSrc but use the AGP in multiple projects, it might be necessary to put those lines in the root build script so that all projects use the same classes and not classes from different class loaders. But having it in the buildSrc dependencies should have the same effect already as it then is in a class loader even higher in the hierarchy.

You maybe could mitigate the problem with “unknown version” if you would use an included build instead of buildSrc which is always something very special, I’m not sure. But you most probably will run into the same runtime issues you have either way.

Renaming buildSrc folder and referring that as an included build did solve the problem. Thanks @Vampire

1 Like