markdown Gradle的buildscript块中的共享变量

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了markdown Gradle的buildscript块中的共享变量相关的知识,希望对你有一定的参考价值。

*Source:* [wtanaka](https://wtanaka.com/node/8111), [StackOverflow](https://stackoverflow.com/a/27382409)

**Question:** How can we use a shared variables as depency versions across multiple modules?

**Answer:** 

We define those values inside a `ext` block in the top level `build.gradle` so it can be used by every module.
But those value won't be visible witin the `buildscript` block in the top level `build.gradle` so we need to moove it into that.

Top level `build.gradle`:

```groovy

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    //moved inside buildscript
    ext {
        compileSdkVersion = 27
        minSdkVersion = 15
        targetSdkVersion = 22
        kotlin_version = '1.1.51'
        support_version = '27.0.0'
        anko_version = '0.10.2'
    }
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.0'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

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

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

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

Module `build.gradle`:

```groovy
apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion rootProject.compileSdkVersion
    defaultConfig {
        applicationId "com.vxhviet.kotlinweather"
        minSdkVersion rootProject.minSdkVersion
        targetSdkVersion rootProject.targetSdkVersion
        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-jre7:$rootProject.kotlin_version"
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'

    compile "com.android.support:appcompat-v7:$rootProject.support_version"
    compile "org.jetbrains.anko:anko-common:$rootProject.anko_version"
}

```

以上是关于markdown Gradle的buildscript块中的共享变量的主要内容,如果未能解决你的问题,请参考以下文章

markdown Gradle的buildscript块中的共享变量

markdown Lightning Fast Gradle Build

markdown [Android studio / Gradle性能提示] #android

markdown 如何解决Gradle的“必须使用完全相同的版本规范”错误

markdown [Gradle basics] #android #android_tutorial #tutorial

markdown 如何在Android Studio中使用捆绑的JDK在终端中运行Gradle命令