风味特定的变体如何工作? [英] How the flavor-specific variants are working?

查看:66
本文介绍了风味特定的变体如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果需要风味,请转到build gradle并添加所需风味

If you need flavor you should go to build gradle and add the flavors that you need

productFlavors {
mock {
  applicationIdSuffix = ".mock"
}
prod {}
}

然后您需要创建相应的目录,例如/src/prod/java/

and then you need to create corresponding dir like this /src/prod/java/

根据例如选择的构建变体,我认为它应该如何工作prodDebug androidStudio将作为基础主要源,并根据选择的构建变体从dir替换核心响应类.

How I thought it should work, according to build variant that was choosen for example prodDebug androidStudio will take as a base main source and substitute coresponding classes from dir according to choosen build variant.

但是后来我发现了下面的片段

But then I found this snippet which said next

特定于风味的文件夹中的文件不会替换主源集中的文件.尝试这样做将导致重复的类异常.这是一个常见的误解,因为这就是资源的合并方式.

Files in the flavor-specific folders do not replace files in the main source set. Trying to do that will result in a duplicate class exception. This is a common misconception because it's how resources are merged.

推荐答案

好,因此,通过带有风味的基本配置,您有两种种类源集:

Ok, so with basic configuration with flavors, you have two kinds of source sets:

  1. main来源集
  2. 特定口味的源集,例如您的mockprod
  1. main source set
  2. flavor-specific source sets, like your mock and prod

使用标准的buildTypes配置( debug release),这将为您提供以下构建变体(构建类型和产品的组合)口味):

With standard buildTypes configuration (debug and release), this gives you the following build variants (combinations of build types and product flavors):

  1. mockDebug
  2. mockRelease
  3. prodDebug
  4. prodRelease

其中每个使用与风味/类型名称对应的每个来源集,因此,例如 prodRelease 将使用以下所有来源一次设置:

Each one of them uses every source set that corresponds with flavor/type name and the main set, so for example, the prodRelease will use all of the following source sets at once:

  1. /src/main
  2. /src/prod
  3. /src/release

有效地,构建系统会将所有这些合并"到一个源集中,这意味着,如果这些集中存在具有相同路径和名称的类,则会发生名称冲突,并且编译器将失败.

Effectively, the build system will 'merge' all of these into one source set, and that means if there are classes with the same path and name in these sets, a name clash occurs and compiler will fail.

正确使用源代码集的方法是从 main 集合中省略每个集合都需要不同的类,而是为每种风味/每个buildType提供所有的集合,例如:

The way to use source sets correctly is to omit the class that you need to be different for each set from the main set, but instead provide it with all the sets for each flavor / each buildType, for example:

  1. main 集具有引用了 B.java 类的类 A.java .主集中省略了B.java.
  2. mock 和 prod 集中包含了不同的 B.java 文件(当然,不必有所不同,但需要提供相同的界面,最好提供 main 集中包含的界面).
  3. 编译器使用当前配置所使用的集合中的 B.java -build变体,因此 mock prod 一个.
  4. 是的!现在您有两种功能上不同的产品口味.
  1. main set has class A.java that references class B.java. B.java is omitted from main set.
  2. Different B.java files are included in mock and prod sets (of course, don't need to be different, but need to provide the same interface, preferably with interface included in main set).
  3. Compiler uses B.java from the set that is being used by the current configuration - build variant, so either the mock or the prod one.
  4. Yay! Now you have two functionally different product flavors.

这种行为不仅限于类,您可以使用风味或键入特定的资源, AndroidManifest.xml 文件以及几乎所有进入源目录的内容.

This behavior doesn't limit to classes, you can use flavor or type specific resources, AndroidManifest.xml files and just about anything that goes into the source dir.

提示:在Android Studio中,您可以在项目文件"部分中看到将针对特定变体选择要编译的文件.要切换构建变体,请点击Cmd + Shift + A(mac键映射),然后搜索构建变体短语.通常,它还会显示在Android Studio窗口的左下角.

Tip: In Android Studio you can see in the 'project files' section which files will be chosen to compile for a specific variant. To switch build variants, hit Cmd+Shift+A (mac keymap) and search for Build Variants phrase. It usually shows also on the left bottom side of the Android Studio window.

这篇关于风味特定的变体如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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