What is the proper way to use the ` ```plugins.wit...
# android
s
What is the proper way to use the `
Copy code
plugins.withType<com.android.build.gradle.LibraryPlugin> { }
in Gradle Kotlin Script DSL? I’m trying to configure multiple gradle modules via the gradle
submodules
block in the root build.gradle.kts. I’ve never been able to figure out how to do this though.
a
Almost the same:
Copy code
plugins.withType(com.android.build.gradle.LibraryPlugin::class) {}
One of the advantages of kts is the auto completion so it should be much easier to find what methods are available (and their signatures).
s
I believe these 2 statements are equivalent. I get an error when doing this:
Copy code
plugins.withType(com.android.build.gradle.LibraryPlugin::class) {
        android {
            
        }
    }
Copy code
Unresolved reference: android
j
what is the context? this or it? I saw in some places where it should be this, it is it, a workaround is use configure<…>
don't remember the exact name, configure<BaseExtension> I think
a
Yes you should use extension instead of plugin for this. See here.
CommonExtension
is for all android modules. There are also
com.android.build.gradle.LibraryExtension
and
com.android.build.gradle.AppExtension
.
s
Thanks I was able to get this working:
Copy code
plugins.withType(com.android.build.gradle.LibraryPlugin::class) {
        configure<com.android.build.api.dsl.LibraryExtension> {
            compileSdk = AndroidConfig.compileSdkVersion

            defaultConfig {
                minSdk = AndroidConfig.minSdkVersion
                targetSdk = AndroidConfig.targetSdkVersion
            }

            compileOptions {
                sourceCompatibility = JavaVersion.VERSION_1_8
                targetCompatibility = JavaVersion.VERSION_1_8
            }

            sourceSets {
                named("main") {
                    manifest.srcFile("src/androidMain/AndroidManifest.xml")
                }
            }
        }
    }