多味编译摇篮依赖 [英] Multi-flavor compile dependencies in gradle

查看:212
本文介绍了多味编译摇篮依赖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法让编译依赖由机器人工作室(build.gradle)?

Is there a way to have compile dependencies by multiple flavors in Android Studio (build.gradle)?

我有2 flavorGroups,并且在每个2变体。在对4种可能的组合,我想能够依靠只有当我无论是在最新的和免费的味道库。 latestCompile或freeCompile工作,但latestFreeCompile没有。这是我的build.gradle的相关部分:

I have 2 flavorGroups, and in each 2 variants. Out of the 4 possible combinations I would like to be able to depend on a lib only if I'm both in latest and in free flavor. latestCompile or freeCompile works, but latestFreeCompile doesn't. this is the relevant part of my build.gradle:

android {
    defaultConfig {
        minSdkVersion 7
        targetSdkVersion 19
        versionCode 15
        versionName "1.9." + versionCode
    }

    flavorGroups 'sdk', 'cost'

    productFlavors {
        latest {
            flavorGroup 'sdk'
            minSdkVersion 8
        }
        sdk7 {
            flavorGroup 'sdk'
            minSdkVersion 7
            versionName android.defaultConfig.versionName + ".sdk7"
        }
        free {
            flavorGroup 'cost'
        }
        pro {
            flavorGroup 'cost'
        }
    }
}
dependencies {
    // this works:
    freeCompile files('libs/StartAppInApp-2.2.1.jar')

    // and I would like something like this:
    latestFreeCompile 'com.google.android.gms:play-services:4.1.32' // minSdkVersion:8
}

如果我使用:

latestCompile 'com.google.android.gms:play-services:4.1.32'

那么这将被包含在latestPro以及(不需要) 如果我会使用:

then it would be included in latestPro as well (not needed) and if I'd use:

freeCompile 'com.google.android.gms:play-services:4.1.32'

那么这将被包含在sdk7Free以及(尽管它需要SDK 8)

then it would be included in sdk7Free as well (although it needs SDK 8)

推荐答案

同样的问题在这里,但帕维尔的解决方案没有工作,因为摇篮的依赖有它的启动建设,不仅选择的口味/生成类型等问题,但所有他们与它需要一个更加动态的解决方案。

Same issue here, but Pawel's solution didn't work because gradle dependencies have other issues in which it starts building not only the selected flavors/build type, but all of them and it requires a more dynamic solution.

不过,我发现这个问题跟踪: <一href="https://$c$c.google.com/p/android/issues/detail?can=2&start=0&num=100&q=&colspec=ID%20Type%20Status%20Owner%20Summary%20Stars&groupby=&sort=&id=52962" rel="nofollow">https://$c$c.google.com/p/android/issues/detail?can=2&start=0&num=100&q=&colspec=ID%20Type%20Status%20Owner%20Summary%20Stars&groupby=&sort=&id=52962

Still I found this issue tracker: https://code.google.com/p/android/issues/detail?can=2&start=0&num=100&q=&colspec=ID%20Type%20Status%20Owner%20Summary%20Stars&groupby=&sort=&id=52962

还有一个参照固定我上面提到的重建,所有的错误,但我没有尝试它。

There is also a reference to fixing the rebuild-all bug I mentioned above, but I didn't try it yet.

和实施这一解决方案(按照问题跟踪回复#60):

And implemented this solution (as per issue tracker reply #60):

    Map<String, Dependency> customDeps = new HashMap<String, Dependency>()

    customDeps.put('flavor1GrpXflabor1GrpYDebugCompile', dependencies.project(path: ':lib', configuration: 'debug'))
    customDeps.put('flavor1GrpXflavor1GrpYReleaseCompile', dependencies.project(path: ':lib', configuration: 'release'))

    customDeps.put('flavor2GrpXflavor1GrpYDebugCompile', dependencies.project(path: ':other_lib', configuration: 'debug'))
    customDeps.put('flavor2GrpXflavor1GrpYReleaseCompile', dependencies.project(path: ':other_lib', configuration: 'release'))

    ....

    configurations.all() { config ->
        Dependency d = customDeps.get(config.name)
        if (d != null) {
            config.dependencies.add(d)
        }
    }

这篇关于多味编译摇篮依赖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆