Lê Khánh Vinh
Lê Khánh Vinh

Reputation: 2611

gradle kotlin-dsl move android {} into subproject {} in root project build.gradle.kts

I'm trying to move android {} (individual module) into subprojects {} (root project build.gradle.kts) (to avoid duplicate of same android {} in every module)

I was able to move when using gradle groovy. but in gradle kotlin-dsl. it seems impossible to reference android in subprojects {}

I have tried

subprojects {
    afterEvaluate {
       if (project.plugins.hasPlugin(Plugins.kotlinAndroidApplication)) {
          project.android {

       }
    }
}

But always getting this error: Unresolved reference: android

Is there any way to access android {} inside subprojects {} for kotlin-dsl?

Upvotes: 3

Views: 1221

Answers (1)

Kostek
Kostek

Reputation: 177

try this way:

subprojects {
    project.plugins.configure(project)
}

fun PluginContainer.configure(project: Project) {
    whenPluginAdded {
        if (this is AndroidBasePlugin) {
            project.extensions
                .getByType<BaseExtension>()
                .apply {
                    applyAndroidCommons()
                }
        }
    }
}

fun BaseExtension.applyAndroidCommons() {
    compileSdkVersion(BuildConfig.COMPILE_SDK)

    defaultConfig {
        minSdkVersion(BuildConfig.MIN_SDK)
        targetSdkVersion(BuildConfig.TARGET_SDK)
    }

    // other stuff you would put into android{}
}

Upvotes: 3

Related Questions