刘奔康
刘奔康

Reputation: 195

How to manage and refer to specific dependency libraries versions?

I have my root build.gradle like this:

...
ext{
  buildToolsVersion = '23.2.1'
}
...

Why cannot I manage my Android support libraries version like this:

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}'
}

${} works in Groovy, why does not it work in Gradle?

Upvotes: 2

Views: 396

Answers (2)

刘奔康
刘奔康

Reputation: 195

my fault.
'com.android.support:appcompat-v7:${rootProject.ext.buildToolsVersion}'
equals
"com.android.support:appcompat-v7:\${rootProject.ext.buildToolsVersion}".
so it should be "com.android.support:appcompat-v7:${rootProject.ext.buildToolsVersion}" with double quotes.

Upvotes: 0

Gabriele Mariotti
Gabriele Mariotti

Reputation: 364868

Use:

In 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}",
    ]
}

In your module/build.gradle:

dependencies {
    //......
    compile supportDependencies.appCompat
    compile supportDependencies.design
}

Upvotes: 5

Related Questions