markdown 如何在多个模块中使用共享变量作为依赖版本?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了markdown 如何在多个模块中使用共享变量作为依赖版本?相关的知识,希望对你有一定的参考价值。

*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 within the `buildscript` block in the top level `build.gradle` so we need to move 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 如何在多个模块中使用共享变量作为依赖版本?的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 R Markdown 显式设置 R DT 表的列宽?

如何使用 angularJS 在多个项目之间共享代码

跨语言共享环境变量

如何在 C++ 中跨多个源文件共享变量?

多个源文件共享同个变量的方法

如何在两个或多个 Servlet 之间共享变量或对象?