如何匹配Android Gradle项目依赖项的构建类型?

dgjrabp2  于 2022-11-24  发布在  Android
关注(0)|答案(1)|浏览(187)

我有一个包含2个模块的项目:libraryapp中的一个或多个。
library模块当然是一个库,只有releasedebug构建类型。
app模块有4种风格以及releasedebug构建类型,总共有8种构建变体。它还声明了对library模块的依赖关系,如下所示:
compile project(path:':library', configuration:'release')
我希望能够根据应用的构建变体设置library配置:release内部版本变体应使用release版本的库,debug内部版本变体应使用debug版本的库。
显而易见的答案是列出8个变体中的每一个,并进行适当的配置,这样就可以工作了,但这不是最佳答案:它很难看,而且使构建脚本过于混乱。
我已经尝试了几种project.configurations.all{}applicationVariants.all{}的方法,但是我找不到一种确定的方法来设置依赖项配置。
有没有更干净的方法?

3pvhb19x

3pvhb19x1#

如果现在还有人遇到这个(或类似的)问题,您可能希望使用matchingFallbacks来指定在您所依赖的库没有匹配的配置时要回退到哪个已解析的buildType或flavor。
https://developer.android.com/studio/build/build-variants#resolve_matching_errors
默认情况下,应该有一个debugrelease配置文件,所以要解决OP问题,您只需要删除依赖声明中的显式配置设置:

// forces release configuration on library
compile project(path:':library', configuration:'release')

// Allows gradle to match buildType from library to 
// what the current module's buildType is:
compile project(path:':library')

来自开发站点的片段,以备将来移动(. kts):

// In the app's build.gradle file.
android {
    defaultConfig {
        // Do not configure matchingFallbacks in the defaultConfig block.
        // Instead, you must specify fallbacks for a given product flavor in the
        // productFlavors block, as shown below.
    }
    buildTypes {
        getByName("debug") {}
        getByName("release") {}
        create("staging") {
            // Specifies a sorted list of fallback build types that the
            // plugin should try to use when a dependency does not include a
            // "staging" build type. You may specify as many fallbacks as you
            // like, and the plugin selects the first build type that's
            // available in the dependency.
            matchingFallbacks += listOf("debug", "qa", "release")
        }
    }
    flavorDimensions += "tier"
    productFlavors {
        create("paid") {
            dimension = "tier"
            // Because the dependency already includes a "paid" flavor in its
            // "tier" dimension, you don't need to provide a list of fallbacks
            // for the "paid" flavor.
        }
        create("free") {
            dimension = "tier"
            // Specifies a sorted list of fallback flavors that the plugin
            // should try to use when a dependency's matching dimension does
            // not include a "free" flavor. You may specify as many
            // fallbacks as you like, and the plugin selects the first flavor
            // that's available in the dependency's "tier" dimension.
            matchingFallbacks += listOf("demo", "trial")
        }
    }
}

相关问题