在Android Studio中如何批量设置flavor依赖项?

时间:2015-07-22 08:05:52

标签: android-studio gradle dependencies

假设我有很多口味:

android
{
    productFlavors {
        flavor1 { ... }
        flavor2 { ... }
        flavor3 { ... }
        flavor4 { ... }
        flavor5 { ... }
        ...
    }
}

而不是逐个设置flavor的依赖项:

dependencies {
    flavor1Compile fileTree(include: ['*.jar'], dir: 'src/flavor1/libs')
    flavor2Compile fileTree(include: ['*.jar'], dir: 'src/flavor2/libs')
    flavor3Compile fileTree(include: ['*.jar'], dir: 'src/flavor3/libs')
    flavor4Compile fileTree(include: ['*.jar'], dir: 'src/flavor4/libs')
    flavor5Compile fileTree(include: ['*.jar'], dir: 'src/flavor5/libs')
    ...
}

我想要一种简单的循环方式,例如伪代码:

dependencies {
    all {
        entry ->
          compile fileTree(include: ['*.jar'], dir: 'src/' + entry.name + '/libs')
    }
}

这可以通过build.gradle实现吗?如果是这样的话怎么做?

1 个答案:

答案 0 :(得分:0)

我发现了。只需循环productFlavors中的dependencies,然后手动调用add方法:

dependencies {
    android.productFlavors.each {
        flavor ->
            add(flavor.name + 'Compile', fileTree(include: ['*.jar'], dir: 'src/' + flavor.name + '/libs'))
    }
}