在依赖项中选择特定的构建类型 [英] Picking a specific build type in a dependency

查看:98
本文介绍了在依赖项中选择特定的构建类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Suppose I have an Android app with three build types:

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

我有一个从属模块:

dependencies {
    implementation project(':myDependency')
}

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

Suppose this dependency only has two build types (let's say debug and release), and I want full control over which of my app's build types uses which of the dependency's build types. For example, I'd like my app's optRelease to use the library's release, and the app's release the use the library's debug.

过去在Android Studio 3.0之前是可以实现的,但是新的构建变体系统似乎不再允许这种情况了.

That used to be possible before Android Studio 3.0, but the new build variant system doesn't seem to allow for that anymore.

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

How can I explicitly state which build type to use? Let's say that I have no control over the dependency and cannot modify its gradle configuration.

推荐答案

您可以使用 matchingFallback

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

您还可以如下指定后备列表:

You can also specify list of fallback as follows:

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

这将指定后备构建类型的排序列表,当依赖项不包含插件时,插件应尝试使用"optRelease"构建类型.您可以指定与您一样多的后备广告例如,插件选择第一个可用的构建类型依赖性.

您可以参考官方文件有关更多详细信息.

You can refer to official document for more details.

但是,如果要在定位外部依赖项时使用特定于变体的配置.您可以按照以下步骤进行操作:

However if you want to use variant-specific configurations when targeting external dependencies. You can do it as follows:

debugImplementation项目(':myDependency')

这将使 myDependency 成为仅对模块调试"版本的依赖.

This will make myDependency as a dependency to only the "debug" version of your module.

这篇关于在依赖项中选择特定的构建类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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