Gradle访问属性来自`apply from:''`在同一个文件中

时间:2018-02-12 17:20:03

标签: android gradle android-gradle build.gradle

Error:(12, 0) Cannot get property 'kotlin' on extra properties extension as it does not exist

我的配置文件:buildsystem/configurations.gradle

ext {
    android = [
            buildToolsVersion: "26.0.2",
            minSdkVersion    : 23,
            targetSdkVersion : 27,
            compileSdkVersion: 27
    ]

    kotlin = [
            version      : '1.2.21',
            serialization: '0.4.1'
    ]
}

顶级build.gradle

apply from: 'buildsystem/configurations.gradle'

buildscript {
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.1'
        //get kotlin version from configurations.gradle
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:${rootProject.ext.kotlin.version}"

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

如何从kotlin文件中引用configurations.gradle数组?我已尝试过上面的内容,project.ext.kotlin.versionext.kotlin.versionkotlin.version。这必须是显而易见的,我只是遗漏了一些东西。如果我深入了解子模块构建文件,我可以像我期望的那样引用所有内容,但不是在这里。

1 个答案:

答案 0 :(得分:2)

buildscript块是它自己的脚本。解决此问题的一种方法是在buildscript块中应用脚本插件:

buildscript {
  apply from: 'buildsystem/configurations.gradle'
  // ... rest of buildscript configuration