gradle kotlin-dsl将android {}移动到根项目build.gradle.kts中的子项目{}中

x33g5p2x  于 2023-06-04  发布在  Android
关注(0)|答案(1)|浏览(117)

我正在尝试将android {}(单个模块)移动到subprojects {}(根项目build.gradle.kts)(以避免每个模块中重复相同的android {}
使用gradle groovy时可以移动。但在gradle kotlin-dsl中。似乎无法在subprojects {}中引用android
我试过了

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

       }
    }
}

但总是得到这个错误:Unresolved reference: android
有没有办法访问kotlin-dslsubprojects {}内部的android {}

5m1hhzi4

5m1hhzi41#

试试这个方法:

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{}
}

相关问题