11

有没有办法在 Android Studio (build.gradle) 中通过多种方式编译依赖项?

我有 2 个风味组,每个有 2 个变体。在 4 种可能的组合中,我希望只有在我既是最新版又是免费版的情况下才能依赖一个库。latestCompile 或 freeCompile 有效,但 latestFreeCompile 无效。这是我的 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 中(不需要),如果我使用:

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

那么它也将包含在 sdk7Free 中(尽管它需要 SDK 8)

4

3 回答 3

4

正如官方Android 开发者网站上现在所描述的,如果您想声明产品风味和构建类型的特定组合的依赖项,您需要首先声明该配置。

例如free风味和debug类型:

configurations {
    freeDebugImplementation {}
}

dependencies {
    freeDebugImplementation 'com.google.firebase:firebase-ads:9.8.0'
}

高级:多种风味维度

请注意,如果您的应用具有多个风味维度,则必须创建与完整构建变体(所有风味,然后是构建类型)相对应的配置,如下所示:

flavorDimensions "money", "image" 
productFlavors {
    free { dimension "money" }
    paid { dimension "money" }

    picasso { dimension "image" }
    glide { dimension "image" }
}

configurations {
    freePicassoDebugImplementation {}
    freeGlideDebugImplementation {}
}

dependencies {
    freePicassoDebugImplementation 'com.google.firebase:firebase-ads:9.8.0'
    freeGlideDebugImplementation 'com.google.firebase:firebase-ads:9.8.0'
}
于 2020-03-20T14:24:52.540 回答
1

我有同样的问题。我用 build.gradle 中的一些 gradle 代码解决了这个问题:

// global variables
ext {
    buildType = ""
    groupCost = ""
    groupSdk = ""
}

def splitCamelCase(String word) {
    def result = []
    int nextStart = 0;
    for (int i = 1; i < word.length(); i++) {
        if(word.charAt(i).isUpperCase()) {
            result.add(word.substring(nextStart, i));
            nextStart = i;
        }
    }

    result.add(word.substring(nextStart));
    return result;
}

// start parameters
println "Start parametes: tasks = " + gradle.startParameter.getTaskNames()

gradle.startParameter.getTaskNames().each { task ->

    // This line is needed to skip other projects' tasks
    // You can safely remove it if you have only one project
    if(!task.startsWith(':<your_application_name>:')) return;

    def taskParts = splitCamelCase(task.split(":").last());
    def groupCostPrefix = taskParts[taskParts.size() - 3];
    def groupSdkPrefix = taskParts[taskParts.size() - 2];
    def buildTypePrefix = taskParts[taskParts.size() - 1];

    if("Debug".startsWith(buildTypePrefix)) {
        buildType = 'debug';
    }
    else if("Release".startsWith(buildTypePrefix)) {
        buildType = 'release';
    }
    else {
        return; // do not process tasks that are not ending with proper build type.
    }

    if("Free".startsWith(groupCostPrefix)) {
        groupCost = 'free';
    }
    else if("Pro".startsWith(groupCostPrefix)) {
        groupCost = 'pro';
    }

    if("Sdk7".startsWith(groupSdkPrefix)) {
        groupSdk = 'froyo';
    }
    else if("Latest".startsWith(groupSdkPrefix)) {
        groupSdk = 'latest';
    }
}

然后,您只需在“依赖项”部分中添加以下代码:

if(groupSdk == 'latest' && groupCost == 'free') {
    compile 'com.google.android.gms:play-services:4.1.32'
}
于 2014-03-22T23:14:45.780 回答
1

这里有同样的问题,但 Pawel 的解决方案不起作用,因为 gradle 依赖项还有其他问题,在这些问题中,它不仅开始构建选定的风味/构建类型,而且还开始构建所有这些问题,并且它需要更动态的解决方案。

我仍然发现这个问题跟踪器: 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

还有一个关于修复我上面提到的全部重建错误的参考,但我还没有尝试过。

并实施了这个解决方案(根据问题跟踪器回复 #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)
        }
    }
于 2015-03-07T16:29:20.597 回答