如何管理和引用特定的依赖库版本?

刘奔康*_*刘奔康 2 android gradle android-studio build.gradle android-gradle-plugin

我有这样的根build.gradle:

...
ext{
  buildToolsVersion = '23.2.1'
}
...
Run Code Online (Sandbox Code Playgroud)

为什么我不能像这样管理我的Android支持库版本:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:${rootProject.ext.buildToolsVersion}'
    compile 'com.android.support:design:${rootProject.ext.buildToolsVersion}'
}
Run Code Online (Sandbox Code Playgroud)

${} 在Groovy中工作,为什么它在Gradle中不起作用?

Gab*_*tti 5

使用:

root/build.gradle:

ext {
    //Version
    supportLibrary = '23.2.1'

    //Support Libraries dependencies
    supportDependencies = [
            design           :         "com.android.support:design:${supportLibrary}",
            recyclerView     :         "com.android.support:recyclerview-v7:${supportLibrary}",
            cardView         :         "com.android.support:cardview-v7:${supportLibrary}",
            appCompat        :         "com.android.support:appcompat-v7:${supportLibrary}",
            supportAnnotation:         "com.android.support:support-annotations:${supportLibrary}",
    ]
}
Run Code Online (Sandbox Code Playgroud)

在你的module/build.gradle:

dependencies {
    //......
    compile supportDependencies.appCompat
    compile supportDependencies.design
}
Run Code Online (Sandbox Code Playgroud)