我正在构建 4 种不同风格的 Android 应用.
I am building 4 different flavors of my Android app.
我有一个类 Customization.java
,其中 3 个相同,1 个不同.
I have a class Customization.java
that is the same for 3 of them and different for 1.
由于我不能将同一个类同时放在主文件夹和风味文件夹中,我现在必须为这 3 个风味维护完全相同的类的 3 个副本.
Since I cannot put the same class both in the main folder and in the flavor folder, I now have to maintain 3 copies of the exact same class for those 3 flavors.
我有什么办法可以只保留这个类的两个版本吗?
Is there any way that I could do with keeping just two versions of this class?
到目前为止我考虑过的事情:
我想知道是否有开箱即用的清洁剂.
I am wondering if there is something cleaner out of the box.
我想将 CommonsWare 的评论转换为答案.然后我将解释最终的目录设置应该是什么样子.我希望这可以帮助人们通过搜索偶然发现这个问题.
I would like to convert CommonsWare's comment to an answer. I'll then explain how the final directory setup should look like. I hope this helps out the people stumbling upon this question through search.
好吧,您可以在风味中覆盖资源.所以,有一个共同的在 main/res/layout/
和风味特定的yourFlavorHere/res/layout/
.
Well, you can override resources in flavors. So, have the common one in
main/res/layout/
and the flavor-specific one inyourFlavorHere/res/layout/
.
因此,如果 Customization
活动的布局文件名为 activity_customization.xml
,您将在 src/main 下的三种风格之间共享其公共副本/res/layout
目录并将修改后的布局 xml 放置在其相应的源集目录 src/flavorFour/res/layout
下,例如 flavorFour
.
So, if the Customization
activity's layout file is called activity_customization.xml
, you'll leave its common copy shared among the three flavors under src/main/res/layout
directory and place the modified layout xml to be used by, say flavorFour
, under its corresponding source set directory src/flavorFour/res/layout
.
其工作方式是,由于风味一到三(与风味四不同)没有提供自己的 activity_customization.xml
版本,它们将继承来自 的版本main
源码集.
The way this works is that since flavor one to three (unlike flavor four) haven't provided their own versions of activity_customization.xml
, they'll inherit the one coming from the main
source set.
是活动 Java 类变得棘手.另一种可能性也就是使用相同的活动实现来配置风味从两个源目录中提取:一个特定于风味的目录和一个与通用类实现通用的一种.
It's the activity Java class that gets tricky. Another possibility for that is to configure the flavors with the same activity implementation to pull from two source directories: a flavor-specific one and a common one with the common class implementation.
与资源不同,Java 代码文件不会合并或覆盖.因此,您不能在 main
下以及任何风味源集中拥有具有相同完全限定类名的 Java 文件.如果这样做,您将收到重复类错误.
Unlike resources, Java code files are not merged or overridden. So, you can't have Java files with the same fully qualified class name under main
as well as in any of your flavor source sets. If you do, you'll receive a duplicate class error.
要解决此问题,最简单的解决方案是将 Customization
活动移出 main
并移到每个风味源集中.这是可行的,因为风味目录是互斥的(彼此互斥,而不是与 main
),因此避免了冲突.
To resolve this issue, the simplest solution is to move Customization
activity out of the main
and into each flavor source set. This works because the flavor directories are mutually exclusive (with each other, not with main
) hence avoiding the conflict.
但这意味着四种风格中的三种具有活动的重复副本 - 维护噩梦 - 只是因为其中一种风格需要对其进行一些更改.为了解决这个问题,我们可以引入另一个源目录,只保留三种风格之间共享的公共代码文件.
But this means three out of the four flavors have a duplicate copy of the activity - a maintenance nightmare - just because one of the flavors required some changes to it. To resolve this issue we can introduce another source directory that keeps just the common code files shared between the three flavors.
所以,build.gradle
脚本看起来像
android {
...
productFlavors {
flavorOne {
...
}
flavorTwo {
...
}
flavorThree {
...
}
flavorFour {
...
}
}
sourceSets {
flavorOne.java.srcDir 'src/common/java'
flavorTwo.java.srcDir 'src/common/java'
flavorThree.java.srcDir 'src/common/java'
}
}
注意使用 java.srcDir
(而不是 srcDirs
)添加另一个 Java 源目录到已经存在的默认 src/flavorX/java
.
Notice the use of java.srcDir
(and not srcDirs
) which adds another Java source directory to the already existing default src/flavorX/java
.
现在我们需要做的就是将通用的 Customization
活动文件放到 src/common/java
中,以使其可用于一到三种风格.flavorFour
所需的修改版本将位于其自己的源集 src/flavorFour/java
下.
Now all we need to do is to drop the common Customization
activity file in src/common/java
to make it available to the flavors one to three. The modified version required by flavorFour
would go under its own source set at src/flavorFour/java
.
所以,最终的项目结构应该是这样的
So, the final project structure would look something like
+ App // module
|- src
|- common // shared srcDir
|- java
|- path/to/pkg
|- CustomizationActivity.java // inherited by flavors 1, 2, 3
+ flavorOne
+ flavorTwo
+ flavorThree
+ flavorFour
|- java
|- path/to/pkg
|- CustomizationActivity.java // per-flavor activity class
|- res
|- layout
|- activity_customization.xml // overrides src/main/res/layout
|- main
+ java
|- res
|- layout
|- activity_customization.xml // inherited by flavors 1, 2, 3
这篇关于不同安卓风格的通用代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!