問題描述
我正在構建 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模板網!