干,是否有更好的gradle依赖声明?

时间:2016-05-27 19:59:57

标签: gradle dependency-management

有没有更好的方法从相同的组(例如'com.android.support')使用相同的版本(例如'23 .4.0')声明多个包(例如'appcompat-v7')?

实际声明:

...
def androidSupport = '23.4.0'
def rxBinding = '0.4.0'

dependencies {
    ...

    // android support
    compile "com.android.support:appcompat-v7:$androidSupport"
    compile "com.android.support:design:$androidSupport"
    compile "com.android.support:percent:$androidSupport"

    // rxJava
    compile "com.jakewharton.rxbinding:rxbinding:$rxBinding"
    compile "com.jakewharton.rxbinding:rxbinding-support-v4:$rxBinding"
    compile "com.jakewharton.rxbinding:rxbinding-appcompat-v7:$rxBinding"
    compile "com.jakewharton.rxbinding:rxbinding-design:$rxBinding"
    compile "com.jakewharton.rxbinding:rxbinding-recyclerview-v7:$rxBinding"
...

是否可以做类似的事情?:

...
compile(group: 'com.android.support', version: '23.4.0') {
    modules: "appcompat-v7", "design", "percent" 
}
...

2 个答案:

答案 0 :(得分:2)

一旦你意识到Gradle构建脚本只是一个Groovy脚本,这意味着每个脚本都是#34;编译"行只是方法调用,你开始看到很多可能性。

例如,指定"内联"工件名称数组,调用"每个()"在那,并传递一个指定" compile group:$it:$version" (或类似的东西)。

答案 1 :(得分:1)

def multiArtifactDependency = { String group, String version, List<String> artifacts -> artifacts.each { compile "${group}:${it}:${version}" } }

multiArtifactDependency('com.android.support', '23.3.0', ['appcompat-v7', 'design'])
multiArtifactDependency('com.squareup.retrofit2', '2.0.2', ['retrofit', 'converter-gson', 'adapter-rxjava'])