android - 在依赖项中选择特定的构建类型

标签 android android-gradle-plugin android-build

假设我有一个具有三种构建类型的 Android 应用:

buildTypes {
    release {
        ....
    }
    optRelease {
        ....
    }
    debug {
        ....
    }
}

我有一个依赖模块:

dependencies {
    implementation project(':myDependency')
}

假设这个依赖项只有两种构建类型(比如 debugrelease),我想完全控制我的应用程序的哪个构建类型使用哪个依赖项的构建类型。例如,我希望我的应用程序的 optRelease 使用库的 release,而应用程序的 release 使用库的 debug.

在 Android Studio 3.0 之前这曾经是可能的,但新的构建变体系统似乎不再允许这样做了。

如何明确说明要使用的构建类型?假设我无法控制依赖项,也无法修改其 gradle 配置。

最佳答案

你可以使用matchingFallback

buildTypes {
    release {
       matchingFallbacks = ['debug']
       ... 
    }
    optRelease {
       matchingFallbacks = ['release']  
       ...
    }
    debug {
       matchingFallbacks = ['debug']  
       ...
    }
}

您还可以指定回退列表,如下所示:

optRelease {
    matchingFallbacks = ['release','debug']  
}

This will specify a sorted list of fallback build types that the plugin should try to use when a dependency does not include a "optRelease" 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.

可以引用official document了解更多详情。

但是,如果您想在针对外部依赖项时使用特定于变体的配置。您可以按如下方式进行:

debugImplementation 项目(':myDependency')

这将使 myDependency 仅依赖于模块的“调试”版本。

关于android - 在依赖项中选择特定的构建类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50771532/

相关文章:

android - 共享首选项和屏幕方向

android - 如何将单选按钮居中对齐

android - 不编译Svg image Loader的库文件

flutter - 5 月 1 日之后,我们将如何处理 jcenter() 存储库,它有什么替代方案?

android - 如何在 Gradle 中使用 Renderscript 支持库

android - 如何为 Android 开发 Web 服务

java - ListView 中的物理距离

android - "more than one library with package name ' com.google.android.gms '"在 Ionic 上

android - 在 android studio 中将模块用作 jar

android - ManifestPlaceholder 将 NumericString 转换为实际数字?