【问题标题】:gradle kotlin-dsl move android {} into subproject {} in root project build.gradle.ktsgradle kotlin-dsl move android {} into subproject {} in root project build.gradle.kts
【发布时间】:2019-10-06 08:31:41
【问题描述】:

我正在尝试将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

有什么方法可以在subprojects {} 中为kotlin-dsl 访问android {}

【问题讨论】:

  • 嗨,Lê Khánh Vinh。您是否看到对先前问题的修改?您应该会收到每个通知。

标签: android gradle gradle-kotlin-dsl


【解决方案1】:

试试这个方法:

subprojects {
    project.plugins.configure(project)
}

fun PluginContainer.configure(project: Project) {
    whenPluginAdded {
        if (this is BasePlugin) {
            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{}
}

【讨论】:

    猜你喜欢
    • 2021-05-15
    • 2018-12-19
    • 1970-01-01
    • 2019-09-15
    • 1970-01-01
    • 2019-03-22
    • 2018-11-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多