问题描述
我有老的Android应用程序,我想移植到了Android摇篮构建系统。这个应用程序目前正在建在一个多项目的设置和发布为四个不同的应用程序(两个不同的数据集包括和两个数据集的免费/付费版本)。我设法用flavorDimensions(previously叫flavorGroups)摆脱多项目的设置,但我想不出如何设置不同的applicationID针对每种口味组合。
由于应用程序版本已经发布,我需要保持相同的applicationID,因为他们目前拥有。因为我原来的包命名是怎么做的,我不能简单地用香味buildtype结合packageNameSuffix(这本来是一个很好的选择,如果它是一个未发布的应用程序)。
http://stackoverflow.com/a/20956353/4177090 是回答如何使用不同的源文件夹的味道组合,但不知道如何(如果甚至有可能)来设置特定的配置在构建文件中的每个组合。
摇篮建立文件片段:
flavorDimensions价格,数据集
productFlavors {
免费{flavorDimension价格}
支付{flavorDimension价格}
为dataSet1 {flavorDimension数据集}
dataset2 {flavorDimension数据集}
}
我想有这样的事情在我的摇篮构建文件中的以下(注意如何unlogic我命名,这就是为什么我不能用packageNameSuffix):
freeDataset1 {的applicationIDcom.beansys.freeappdataset1}
freeDataset2 {的applicationIDcom.beansys.freedataset2}
paidDataset1 {的applicationIDcom.beansys.dataset1paid}
paidDataset2 {的applicationIDcom.beansys.mypaiddataset2}
我终于设法解决这个问题。我认为,解决的办法是优雅的(虽然实际code可能最有可能被写入要好很多被人用常规的知识)。
解决方案设置特定的applicationID的每个组合的味道:
flavorDimensions价格,数据集
productFlavors {
免费{flavorDimension价格}
支付{flavorDimension价格}
为dataSet1 {flavorDimension数据集}
dataset2 {flavorDimension数据集}
}
android.variantFilter {变种 - >
高清flavorString =
高清香精= variant.getFlavors()
的for(int i = 0; I< flavors.size();我++){
flavorString + =口味[我]。名称;
}
如果(flavorString.equalsIgnoreCase(freeDataset1)){
variant.getDefaultConfig()的applicationIDcom.beansys.freeappdataset1
}
如果(flavorString.equalsIgnoreCase(freeDataset2)){
variant.getDefaultConfig()的applicationIDcom.beansys.freedataset2
}
如果(flavorString.equalsIgnoreCase(paidDataset1)){
variant.getDefaultConfig()的applicationIDcom.beansys.dataset1paid
}
如果(flavorString.equalsIgnoreCase(paidDataset2)){
variant.getDefaultConfig()的applicationIDcom.beansys.mypaiddataset2
}
}
I have and old android app that I am trying to migrate to the android gradle build system. The app is currently built in a multi project setup and published as four different apps (two different data sets included and free/paid versions for both datasets). I have managed to get away from the multi project setup by using flavorDimensions (previously called flavorGroups), but I can not figure out how to set a different applicationId for each flavor combination.
Since the app versions are already published, I need to keep the same applicationid as they currently have. Because of how my original package naming was done, I can not simply use flavor-buildtype combination with "packageNameSuffix" (which would have been a great option if it was an unpublished app).
http://stackoverflow.com/a/20956353/4177090 is answering how to use different source folders for flavor combinations, but not how (if even possible) to set specific configuration for each combination in the build file.
Gradle build file snippet:
flavorDimensions "price", "dataset"
productFlavors {
free { flavorDimension "price" }
paid { flavorDimension "price" }
dataset1 { flavorDimension "dataset" }
dataset2 { flavorDimension "dataset" }
}
I would like to have something like the following in my gradle build file (notice how unlogic my naming is, which is why I cannot use packageNameSuffix):
freeDataset1 { applicationId "com.beansys.freeappdataset1" }
freeDataset2 { applicationId "com.beansys.freedataset2" }
paidDataset1 { applicationId "com.beansys.dataset1paid" }
paidDataset2 { applicationId "com.beansys.mypaiddataset2" }
I finally managed to solve this. I think the solution is elegant (although the actual code could most likely be written a lot nicer by someone with groovy knowledge).
Solution for setting a specific applicationId for each combined flavor:
flavorDimensions "price", "dataset"
productFlavors {
free { flavorDimension "price" }
paid { flavorDimension "price" }
dataset1 { flavorDimension "dataset" }
dataset2 { flavorDimension "dataset" }
}
android.variantFilter { variant ->
def flavorString = ""
def flavors = variant.getFlavors()
for (int i = 0; i < flavors.size(); i++) {
flavorString += flavors[i].name;
}
if(flavorString.equalsIgnoreCase("freeDataset1")) {
variant.getDefaultConfig().applicationId "com.beansys.freeappdataset1"
}
if(flavorString.equalsIgnoreCase("freeDataset2")) {
variant.getDefaultConfig().applicationId "com.beansys.freedataset2"
}
if(flavorString.equalsIgnoreCase("paidDataset1")) {
variant.getDefaultConfig().applicationId "com.beansys.dataset1paid"
}
if(flavorString.equalsIgnoreCase("paidDataset2")) {
variant.getDefaultConfig().applicationId "com.beansys.mypaiddataset2"
}
}
这篇关于如何设置不同的applicationID使用flavorDimensions各味合用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!