我该如何指定每个口味(flavor)的buildType sourceSets?

14

我有两种应用程序版本,每个版本都有自己的 Google Maps (v1) 调试和发布密钥(即总共4个密钥)。因此,我想知道是否可以根据 buildType 和 productFlavor 来指定 sourceSets。本质上,我想知道如何实现类似于以下内容的功能:

src
├── debug
│   └── flavor1
│       └── res
│           └── values
│               └── gmaps_key.xml
├── release
│   └──flavor1
│       └── res
│           └── values
│               └── gmaps_key.xml

Gradle将使用src/<currentBuldType>/<currentProductFlavor>/*作为其sourceSet的一部分。

基本上,我希望如果我运行gradle assembleFlavor1Debug,它将包含src/main/*src/flavor1/* src/debug/flavor1/* 下的所有内容。

我的build.gradle非常简单:

buildscript {
    repositories {
        mavenCentral()
    }   

    dependencies {
        classpath 'com.android.tools.build:gradle:0.5.0'
    }   
}

apply plugin: 'android'

android {
    compileSdkVersion 8

    productFlavors {
        flavor1 {
            packageName 'com.flavor1'
        }
        flavor2 {
            packageName 'com.flavor2'
        }
    }
}

有什么想法吗?或者也许有更好的方法来处理这个问题?

2个回答

19
I happened to come back to this because of a comment on my answer and realized that this answer is superfluous (still better than the accepted one which is even more so). For each productFlavor and buildType, combination and individual source sets already exist. i.e. src/{buildType}, src/{productFlavor} and src/{productFlavor}{buildType} are already available source folders.
So essentially, all that was needed for the OP was to put the resources in src/flavor1Debug which is equivalent to the src/debug/flavor1 that the OP envisions.
OLD ANSWER: I've done something similar with buildConfig but hopefully it should work with sourceSets.
Basically, you define the common stuff at the productFlavors level in a variable and keep adding things as you move down.
productFlavors {
        def common = 'src/main'

        flavor1 {
            def flavor = 'src/main/flavor1'
            buildTypes {
                debug {
                    sourceSets {
                        res.srcDirs = [ common + ',' + flavor + ',' + 'src/main/debug'
                    }
                }

                release {
                    sourceSets {
                        res.srcDirs = [ common + ',' + flavor + ',' + 'src/main/release'
                    }

            }
        }
}

我还没有测试过这个。我认为你可能需要使用android.sourceSets而不是只使用sourceSets
我还需要为productFlavors定义单独的资源,所以我在构建文件的后期使用了一个单独的语句。像这样:
android.sourceSets.flavor1 {
    res.srcDirs = ['flavor_resources/flavor1/res']
}

如果需要,您只需使用android.sourceSets.flavor1.debug即可。

还要注意,根据Android Gradle user guide,使用srcDir将目录添加到默认源中,而srcDirs则替换它们。


我该如何动态地实现这个?我有sourceSets.whenObjectAdded { sourceSet -> sourceSet.java.srcDirs = "someDir"},然后每个口味都定义了源集,像这样:sourceSets { flavor1{} flavor2{}}。我想为每个产品口味和构建类型执行类似于sourceSet.debug.java.srcDirs = "someDir"的操作。 - box
我修改了答案,你还需要更多定制的东西吗? - Saad Farooq
嗨@saad farooq,我知道已经编辑过的解决方案(无论如何感谢您发布它!),但是我的产品口味很复杂并且重用资源。我可能需要重新考虑我的做法,但对我来说动态解决方案会更好,因为我有12个产品口味,我不想创建额外的文件夹,例如flavor1Release,flavor1Debug,flavor1DebugFull。这将导致太多文件夹的排列组合,维护起来很耗时。我也发布了一个问题:https://dev59.com/xJ7ha4cB1Zd3GeqPeRkq - box

5

我接受你的答案,因为这与我最终做事情的方式非常相似。 - smoak

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接