顶级build.gradle文件中的ext属性

时间:2018-07-26 15:08:47

标签: android android-studio gradle

我正在开发一个android应用程序。我在根项目中有一个“ dependencies.gradle”文件:

ext {
    // Android
    kotlinVersion = '1.2.51'
    gradleVersion = '3.1.3'
}

问题是我可以在App的“ build.gradle”文件中使用此属性,但不能在根“ build.gradle”文件中使用,这给了我这个错误:

Could not get unknown property 'kotlinVersion' for object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.

这是我的“ Root Build Gradle”:

apply from: 'dependencies.gradle'

buildscript {
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath "com.android.tools.build:gradle:3.1.3"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

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

这是我的“ App Build Gradle”:

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 27
    defaultConfig {
        applicationId "com.shimibox.client"
        minSdkVersion 18
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlinVersion"
    implementation 'com.android.support:appcompat-v7:27.1.1'
    implementation 'com.android.support.constraint:constraint-layout:1.1.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

1 个答案:

答案 0 :(得分:2)

我发现了问题。 ext必须在buildscript块内。因此,我将apply from: 'dependencies.gradle'移到了该块中。 现在,root build.gradle文件为:

buildscript {

    apply from: 'dependencies.gradle'

    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath "com.android.tools.build:gradle:3.1.3"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
    }
}