問題描述
我有一個舊的 android 應用程序,我正在嘗試遷移到 android gradle 構建系統.該應用程序當前構建在多項目設置中,并作為四個不同的應用程序發布(包括兩個不同的數據集和兩個數據集的免費/付費版本).通過使用 flavorDimensions(以前稱為 flavorGroups),我設法擺脫了多項目設置,但我不知道如何為每種風味組合設置不同的 applicationId.
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.
由于應用程序版本已經發布,我需要保持與當前相同的應用程序 ID.由于我最初的包命名是如何完成的,我不能簡單地使用帶有packageNameSuffix"的flavor-buildtype組合(如果它是一個未發布的應用程序,這將是一個很好的選擇).
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).
https://stackoverflow.com/a/20956353/4177090 正在回答如何使用不同的源文件夾來調味組合,而不是如何(如果可能的話)為構建文件中的每個組合設置特定配置.
https://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 構建文件片段:
Gradle build file snippet:
flavorDimensions "price", "dataset"
productFlavors {
free { flavorDimension "price" }
paid { flavorDimension "price" }
dataset1 { flavorDimension "dataset" }
dataset2 { flavorDimension "dataset" }
}
我想在我的 gradle 構建文件中包含以下內容(注意我的命名是多么不合邏輯,這就是我不能使用 packageNameSuffix 的原因):
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" }
推薦答案
我終于設法解決了這個問題.我認為解決方案很優雅(盡管實際代碼很可能由具有 groovy 知識的人編寫得更好).
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).
為每種組合風味設置特定 applicationId 的解決方案:
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"
}
}
這篇關于如何使用 flavorDimensions 為每種風味組合設置不同的 applicationId?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!