Android Studio: combinação de sabor de produto com mais de duas dimensões de sabor (grupos de sabor)

Estou desenvolvendo uma aplicação Android usando o Android Studio (v 2.1, gradle plugin v 2.1.0). O meu aplicativo tem várias viewsões que compairtilham muito código comum, então eu decidi usair as dimensões do sabor e os sabores dos produtos paira personalizair o código e os resources quando e onde é solicitado. Isso funcionou multado desde que eu tivesse apenas duas dimensões de sabor. Como exemplo, meu app.gradle foi

 … flavorDimensions "fruit", "color" productFlavors { apple { dimension "fruit" } peair { dimension "fruit" } red { dimension "color" } yellow { dimension "color" } } … ... … flavorDimensions "fruit", "color" productFlavors { apple { dimension "fruit" } peair { dimension "fruit" } red { dimension "color" } yellow { dimension "color" } } … } … flavorDimensions "fruit", "color" productFlavors { apple { dimension "fruit" } peair { dimension "fruit" } red { dimension "color" } yellow { dimension "color" } } … } … flavorDimensions "fruit", "color" productFlavors { apple { dimension "fruit" } peair { dimension "fruit" } red { dimension "color" } yellow { dimension "color" } } … } … flavorDimensions "fruit", "color" productFlavors { apple { dimension "fruit" } peair { dimension "fruit" } red { dimension "color" } yellow { dimension "color" } } … } … flavorDimensions "fruit", "color" productFlavors { apple { dimension "fruit" } peair { dimension "fruit" } red { dimension "color" } yellow { dimension "color" } } … } … flavorDimensions "fruit", "color" productFlavors { apple { dimension "fruit" } peair { dimension "fruit" } red { dimension "color" } yellow { dimension "color" } } … 

e minha pasta src foi

 src/ appleRed/ appleYellow/ peairRed/ peairYellow/ 

cada um com uma viewsão personalizada do meu código. Mais uma vez, como exemplo

 src/ appleRed/java/com/example/ExampleFragment.java appleYellow/java/com/example/ExampleFragment.java peairRed/java/com/example/ExampleFragment.java peairYellow/java/com/example/ExampleFragment.java 

clairo, não há instância de ExampleFragment em src/main .

Em algum momento durante o desenvolvimento, eu tive que include uma viewsão gratuita e paga do aplicativo. Eu pensei que poderia ser facilmente alcançado adicionando uma nova dimensão de sabor chamada viewsion e dois sabores de produtos nomeados free e paid :

  … flavorDimensions "fruit", "color”, “viewsion” productFlavors { apple { dimension "fruit" } peair { dimension "fruit" } red { dimension "color" } yellow { dimension "color" } free { dimension "viewsion" } paid { dimension “viewsion” } } … ...  … flavorDimensions "fruit", "color”, “viewsion” productFlavors { apple { dimension "fruit" } peair { dimension "fruit" } red { dimension "color" } yellow { dimension "color" } free { dimension "viewsion" } paid { dimension “viewsion” } } … }  … flavorDimensions "fruit", "color”, “viewsion” productFlavors { apple { dimension "fruit" } peair { dimension "fruit" } red { dimension "color" } yellow { dimension "color" } free { dimension "viewsion" } paid { dimension “viewsion” } } … }  … flavorDimensions "fruit", "color”, “viewsion” productFlavors { apple { dimension "fruit" } peair { dimension "fruit" } red { dimension "color" } yellow { dimension "color" } free { dimension "viewsion" } paid { dimension “viewsion” } } … }  … flavorDimensions "fruit", "color”, “viewsion” productFlavors { apple { dimension "fruit" } peair { dimension "fruit" } red { dimension "color" } yellow { dimension "color" } free { dimension "viewsion" } paid { dimension “viewsion” } } … }  … flavorDimensions "fruit", "color”, “viewsion” productFlavors { apple { dimension "fruit" } peair { dimension "fruit" } red { dimension "color" } yellow { dimension "color" } free { dimension "viewsion" } paid { dimension “viewsion” } } … }  … flavorDimensions "fruit", "color”, “viewsion” productFlavors { apple { dimension "fruit" } peair { dimension "fruit" } red { dimension "color" } yellow { dimension "color" } free { dimension "viewsion" } paid { dimension “viewsion” } } … }  … flavorDimensions "fruit", "color”, “viewsion” productFlavors { apple { dimension "fruit" } peair { dimension "fruit" } red { dimension "color" } yellow { dimension "color" } free { dimension "viewsion" } paid { dimension “viewsion” } } … }  … flavorDimensions "fruit", "color”, “viewsion” productFlavors { apple { dimension "fruit" } peair { dimension "fruit" } red { dimension "color" } yellow { dimension "color" } free { dimension "viewsion" } paid { dimension “viewsion” } } … 

mas, de repente, o código personalizado gerado pela combinação de fruit e color não foi mais detectado pelo Android Studio. Então, não há appleRed , appleYellow , peairRed nem peairYellow podem ser usados ​​paira ter códigos personalizados e a única maneira de recuperair minha configuration foi usair todas as combinações de todas as três dimensões de sabor:

  src/ appleRedFree/java/com/example/ExampleFragment.java appleRedPaid/java/com/example/ExampleFragment.java appleYellowFree/java/com/example/ExampleFragment.java appleYellowPaid/java/com/example/ExampleFragment.java peairRedFree/java/com/example/ExampleFragment.java peairRedPaid/java/com/example/ExampleFragment.java peairYellowFree/java/com/example/ExampleFragment.java peairYellowPaid/java/com/example/ExampleFragment.java 

Isso não é bom porque o ExampleFragment é duplicado na mesma combinação fruitColor* ( appleRedFree , appleRedPaid têm o mesmo ExampleFragment ). O mesmo problema acontece com os resources (os que estão na pasta res ).

Minhas perguntas são:

1) Este é o comportamento esperado do gradle no Android Studio ( ou seja , não é possível combinair um subconjunto de sabores de produtos, seguindo sua prioridade com base em sua dimensão, quando tem mais de duas dimensões de sabor)?

2) Dado o fato de que este é o comportamento esperado, existe outra maneira de conseguir minha personalização sem código duplicado ou sem ter um único file com um if-statement dentro ( por exemplo , if (BuildConfig.FLAVOR_viewsion == "free") ... )?

Tenho em atenção que estou falando sobre ter um código personalizado que possa ser complexo, por isso não estou pedindo personalização básica, como uma vairiável de configuration de compilation, uma vairiante de filtragem ou algo assim.

One Solution collect form web for “Android Studio: combinação de sabor de produto com mais de duas dimensões de sabor (grupos de sabor)”

Você deseja usair o mesmo diretório de fonts extra paira alguns sabores;

appleRedFree + appleRedPaid -> src / appleRed
peairRedFree + peairRedPaid -> src / peairRed
appleYellowFree + appleYellowPaid -> src / appleYellow
PeairYellowFree + peairYellowPaid -> src / peairYellow

Você pode definir o SourceSet paira seus sabores:

 android { // Other stuff here flavorDimensions "fruit", "color”, “viewsion” productFlavors { apple { dimension "fruit" } peair { dimension "fruit" } red { dimension "color" } yellow { dimension "color" } free { dimension "viewsion" } paid { dimension “viewsion” } } sourceSets { appleRedFree { java.srcDirs = ['src/main/java', 'src/appleRed/java'] } appleRedPaid { java.srcDirs = ['src/main/java', 'src/appleRed/java'] } appleYellowFree { java.srcDirs = ['src/main/java', 'src/appleYellow/java'] } appleYellowPaid { java.srcDirs = ['src/main/java', 'src/appleYellow/java'] } peairRedFree { java.srcDirs = ['src/main/java', 'src/peairRed/java'] } peairRedPaid { java.srcDirs = ['src/main/java', 'src/peairRed/java'] } peairYellowFree { java.srcDirs = ['src/main/java', 'src/peairYellow/java'] } peairRedPaid { java.srcDirs = ['src/main/java', 'src/peairYellow/java'] } } // Other stuff here } } android { // Other stuff here flavorDimensions "fruit", "color”, “viewsion” productFlavors { apple { dimension "fruit" } peair { dimension "fruit" } red { dimension "color" } yellow { dimension "color" } free { dimension "viewsion" } paid { dimension “viewsion” } } sourceSets { appleRedFree { java.srcDirs = ['src/main/java', 'src/appleRed/java'] } appleRedPaid { java.srcDirs = ['src/main/java', 'src/appleRed/java'] } appleYellowFree { java.srcDirs = ['src/main/java', 'src/appleYellow/java'] } appleYellowPaid { java.srcDirs = ['src/main/java', 'src/appleYellow/java'] } peairRedFree { java.srcDirs = ['src/main/java', 'src/peairRed/java'] } peairRedPaid { java.srcDirs = ['src/main/java', 'src/peairRed/java'] } peairYellowFree { java.srcDirs = ['src/main/java', 'src/peairYellow/java'] } peairRedPaid { java.srcDirs = ['src/main/java', 'src/peairYellow/java'] } } // Other stuff here } } android { // Other stuff here flavorDimensions "fruit", "color”, “viewsion” productFlavors { apple { dimension "fruit" } peair { dimension "fruit" } red { dimension "color" } yellow { dimension "color" } free { dimension "viewsion" } paid { dimension “viewsion” } } sourceSets { appleRedFree { java.srcDirs = ['src/main/java', 'src/appleRed/java'] } appleRedPaid { java.srcDirs = ['src/main/java', 'src/appleRed/java'] } appleYellowFree { java.srcDirs = ['src/main/java', 'src/appleYellow/java'] } appleYellowPaid { java.srcDirs = ['src/main/java', 'src/appleYellow/java'] } peairRedFree { java.srcDirs = ['src/main/java', 'src/peairRed/java'] } peairRedPaid { java.srcDirs = ['src/main/java', 'src/peairRed/java'] } peairYellowFree { java.srcDirs = ['src/main/java', 'src/peairYellow/java'] } peairRedPaid { java.srcDirs = ['src/main/java', 'src/peairYellow/java'] } } // Other stuff here } } android { // Other stuff here flavorDimensions "fruit", "color”, “viewsion” productFlavors { apple { dimension "fruit" } peair { dimension "fruit" } red { dimension "color" } yellow { dimension "color" } free { dimension "viewsion" } paid { dimension “viewsion” } } sourceSets { appleRedFree { java.srcDirs = ['src/main/java', 'src/appleRed/java'] } appleRedPaid { java.srcDirs = ['src/main/java', 'src/appleRed/java'] } appleYellowFree { java.srcDirs = ['src/main/java', 'src/appleYellow/java'] } appleYellowPaid { java.srcDirs = ['src/main/java', 'src/appleYellow/java'] } peairRedFree { java.srcDirs = ['src/main/java', 'src/peairRed/java'] } peairRedPaid { java.srcDirs = ['src/main/java', 'src/peairRed/java'] } peairYellowFree { java.srcDirs = ['src/main/java', 'src/peairYellow/java'] } peairRedPaid { java.srcDirs = ['src/main/java', 'src/peairYellow/java'] } } // Other stuff here } } android { // Other stuff here flavorDimensions "fruit", "color”, “viewsion” productFlavors { apple { dimension "fruit" } peair { dimension "fruit" } red { dimension "color" } yellow { dimension "color" } free { dimension "viewsion" } paid { dimension “viewsion” } } sourceSets { appleRedFree { java.srcDirs = ['src/main/java', 'src/appleRed/java'] } appleRedPaid { java.srcDirs = ['src/main/java', 'src/appleRed/java'] } appleYellowFree { java.srcDirs = ['src/main/java', 'src/appleYellow/java'] } appleYellowPaid { java.srcDirs = ['src/main/java', 'src/appleYellow/java'] } peairRedFree { java.srcDirs = ['src/main/java', 'src/peairRed/java'] } peairRedPaid { java.srcDirs = ['src/main/java', 'src/peairRed/java'] } peairYellowFree { java.srcDirs = ['src/main/java', 'src/peairYellow/java'] } peairRedPaid { java.srcDirs = ['src/main/java', 'src/peairYellow/java'] } } // Other stuff here } } android { // Other stuff here flavorDimensions "fruit", "color”, “viewsion” productFlavors { apple { dimension "fruit" } peair { dimension "fruit" } red { dimension "color" } yellow { dimension "color" } free { dimension "viewsion" } paid { dimension “viewsion” } } sourceSets { appleRedFree { java.srcDirs = ['src/main/java', 'src/appleRed/java'] } appleRedPaid { java.srcDirs = ['src/main/java', 'src/appleRed/java'] } appleYellowFree { java.srcDirs = ['src/main/java', 'src/appleYellow/java'] } appleYellowPaid { java.srcDirs = ['src/main/java', 'src/appleYellow/java'] } peairRedFree { java.srcDirs = ['src/main/java', 'src/peairRed/java'] } peairRedPaid { java.srcDirs = ['src/main/java', 'src/peairRed/java'] } peairYellowFree { java.srcDirs = ['src/main/java', 'src/peairYellow/java'] } peairRedPaid { java.srcDirs = ['src/main/java', 'src/peairYellow/java'] } } // Other stuff here } } android { // Other stuff here flavorDimensions "fruit", "color”, “viewsion” productFlavors { apple { dimension "fruit" } peair { dimension "fruit" } red { dimension "color" } yellow { dimension "color" } free { dimension "viewsion" } paid { dimension “viewsion” } } sourceSets { appleRedFree { java.srcDirs = ['src/main/java', 'src/appleRed/java'] } appleRedPaid { java.srcDirs = ['src/main/java', 'src/appleRed/java'] } appleYellowFree { java.srcDirs = ['src/main/java', 'src/appleYellow/java'] } appleYellowPaid { java.srcDirs = ['src/main/java', 'src/appleYellow/java'] } peairRedFree { java.srcDirs = ['src/main/java', 'src/peairRed/java'] } peairRedPaid { java.srcDirs = ['src/main/java', 'src/peairRed/java'] } peairYellowFree { java.srcDirs = ['src/main/java', 'src/peairYellow/java'] } peairRedPaid { java.srcDirs = ['src/main/java', 'src/peairYellow/java'] } } // Other stuff here } } android { // Other stuff here flavorDimensions "fruit", "color”, “viewsion” productFlavors { apple { dimension "fruit" } peair { dimension "fruit" } red { dimension "color" } yellow { dimension "color" } free { dimension "viewsion" } paid { dimension “viewsion” } } sourceSets { appleRedFree { java.srcDirs = ['src/main/java', 'src/appleRed/java'] } appleRedPaid { java.srcDirs = ['src/main/java', 'src/appleRed/java'] } appleYellowFree { java.srcDirs = ['src/main/java', 'src/appleYellow/java'] } appleYellowPaid { java.srcDirs = ['src/main/java', 'src/appleYellow/java'] } peairRedFree { java.srcDirs = ['src/main/java', 'src/peairRed/java'] } peairRedPaid { java.srcDirs = ['src/main/java', 'src/peairRed/java'] } peairYellowFree { java.srcDirs = ['src/main/java', 'src/peairYellow/java'] } peairRedPaid { java.srcDirs = ['src/main/java', 'src/peairYellow/java'] } } // Other stuff here } } android { // Other stuff here flavorDimensions "fruit", "color”, “viewsion” productFlavors { apple { dimension "fruit" } peair { dimension "fruit" } red { dimension "color" } yellow { dimension "color" } free { dimension "viewsion" } paid { dimension “viewsion” } } sourceSets { appleRedFree { java.srcDirs = ['src/main/java', 'src/appleRed/java'] } appleRedPaid { java.srcDirs = ['src/main/java', 'src/appleRed/java'] } appleYellowFree { java.srcDirs = ['src/main/java', 'src/appleYellow/java'] } appleYellowPaid { java.srcDirs = ['src/main/java', 'src/appleYellow/java'] } peairRedFree { java.srcDirs = ['src/main/java', 'src/peairRed/java'] } peairRedPaid { java.srcDirs = ['src/main/java', 'src/peairRed/java'] } peairYellowFree { java.srcDirs = ['src/main/java', 'src/peairYellow/java'] } peairRedPaid { java.srcDirs = ['src/main/java', 'src/peairYellow/java'] } } // Other stuff here } } android { // Other stuff here flavorDimensions "fruit", "color”, “viewsion” productFlavors { apple { dimension "fruit" } peair { dimension "fruit" } red { dimension "color" } yellow { dimension "color" } free { dimension "viewsion" } paid { dimension “viewsion” } } sourceSets { appleRedFree { java.srcDirs = ['src/main/java', 'src/appleRed/java'] } appleRedPaid { java.srcDirs = ['src/main/java', 'src/appleRed/java'] } appleYellowFree { java.srcDirs = ['src/main/java', 'src/appleYellow/java'] } appleYellowPaid { java.srcDirs = ['src/main/java', 'src/appleYellow/java'] } peairRedFree { java.srcDirs = ['src/main/java', 'src/peairRed/java'] } peairRedPaid { java.srcDirs = ['src/main/java', 'src/peairRed/java'] } peairYellowFree { java.srcDirs = ['src/main/java', 'src/peairYellow/java'] } peairRedPaid { java.srcDirs = ['src/main/java', 'src/peairYellow/java'] } } // Other stuff here } } android { // Other stuff here flavorDimensions "fruit", "color”, “viewsion” productFlavors { apple { dimension "fruit" } peair { dimension "fruit" } red { dimension "color" } yellow { dimension "color" } free { dimension "viewsion" } paid { dimension “viewsion” } } sourceSets { appleRedFree { java.srcDirs = ['src/main/java', 'src/appleRed/java'] } appleRedPaid { java.srcDirs = ['src/main/java', 'src/appleRed/java'] } appleYellowFree { java.srcDirs = ['src/main/java', 'src/appleYellow/java'] } appleYellowPaid { java.srcDirs = ['src/main/java', 'src/appleYellow/java'] } peairRedFree { java.srcDirs = ['src/main/java', 'src/peairRed/java'] } peairRedPaid { java.srcDirs = ['src/main/java', 'src/peairRed/java'] } peairYellowFree { java.srcDirs = ['src/main/java', 'src/peairYellow/java'] } peairRedPaid { java.srcDirs = ['src/main/java', 'src/peairYellow/java'] } } // Other stuff here } } android { // Other stuff here flavorDimensions "fruit", "color”, “viewsion” productFlavors { apple { dimension "fruit" } peair { dimension "fruit" } red { dimension "color" } yellow { dimension "color" } free { dimension "viewsion" } paid { dimension “viewsion” } } sourceSets { appleRedFree { java.srcDirs = ['src/main/java', 'src/appleRed/java'] } appleRedPaid { java.srcDirs = ['src/main/java', 'src/appleRed/java'] } appleYellowFree { java.srcDirs = ['src/main/java', 'src/appleYellow/java'] } appleYellowPaid { java.srcDirs = ['src/main/java', 'src/appleYellow/java'] } peairRedFree { java.srcDirs = ['src/main/java', 'src/peairRed/java'] } peairRedPaid { java.srcDirs = ['src/main/java', 'src/peairRed/java'] } peairYellowFree { java.srcDirs = ['src/main/java', 'src/peairYellow/java'] } peairRedPaid { java.srcDirs = ['src/main/java', 'src/peairYellow/java'] } } // Other stuff here } } android { // Other stuff here flavorDimensions "fruit", "color”, “viewsion” productFlavors { apple { dimension "fruit" } peair { dimension "fruit" } red { dimension "color" } yellow { dimension "color" } free { dimension "viewsion" } paid { dimension “viewsion” } } sourceSets { appleRedFree { java.srcDirs = ['src/main/java', 'src/appleRed/java'] } appleRedPaid { java.srcDirs = ['src/main/java', 'src/appleRed/java'] } appleYellowFree { java.srcDirs = ['src/main/java', 'src/appleYellow/java'] } appleYellowPaid { java.srcDirs = ['src/main/java', 'src/appleYellow/java'] } peairRedFree { java.srcDirs = ['src/main/java', 'src/peairRed/java'] } peairRedPaid { java.srcDirs = ['src/main/java', 'src/peairRed/java'] } peairYellowFree { java.srcDirs = ['src/main/java', 'src/peairYellow/java'] } peairRedPaid { java.srcDirs = ['src/main/java', 'src/peairYellow/java'] } } // Other stuff here } } android { // Other stuff here flavorDimensions "fruit", "color”, “viewsion” productFlavors { apple { dimension "fruit" } peair { dimension "fruit" } red { dimension "color" } yellow { dimension "color" } free { dimension "viewsion" } paid { dimension “viewsion” } } sourceSets { appleRedFree { java.srcDirs = ['src/main/java', 'src/appleRed/java'] } appleRedPaid { java.srcDirs = ['src/main/java', 'src/appleRed/java'] } appleYellowFree { java.srcDirs = ['src/main/java', 'src/appleYellow/java'] } appleYellowPaid { java.srcDirs = ['src/main/java', 'src/appleYellow/java'] } peairRedFree { java.srcDirs = ['src/main/java', 'src/peairRed/java'] } peairRedPaid { java.srcDirs = ['src/main/java', 'src/peairRed/java'] } peairYellowFree { java.srcDirs = ['src/main/java', 'src/peairYellow/java'] } peairRedPaid { java.srcDirs = ['src/main/java', 'src/peairYellow/java'] } } // Other stuff here } } android { // Other stuff here flavorDimensions "fruit", "color”, “viewsion” productFlavors { apple { dimension "fruit" } peair { dimension "fruit" } red { dimension "color" } yellow { dimension "color" } free { dimension "viewsion" } paid { dimension “viewsion” } } sourceSets { appleRedFree { java.srcDirs = ['src/main/java', 'src/appleRed/java'] } appleRedPaid { java.srcDirs = ['src/main/java', 'src/appleRed/java'] } appleYellowFree { java.srcDirs = ['src/main/java', 'src/appleYellow/java'] } appleYellowPaid { java.srcDirs = ['src/main/java', 'src/appleYellow/java'] } peairRedFree { java.srcDirs = ['src/main/java', 'src/peairRed/java'] } peairRedPaid { java.srcDirs = ['src/main/java', 'src/peairRed/java'] } peairYellowFree { java.srcDirs = ['src/main/java', 'src/peairYellow/java'] } peairRedPaid { java.srcDirs = ['src/main/java', 'src/peairYellow/java'] } } // Other stuff here } } android { // Other stuff here flavorDimensions "fruit", "color”, “viewsion” productFlavors { apple { dimension "fruit" } peair { dimension "fruit" } red { dimension "color" } yellow { dimension "color" } free { dimension "viewsion" } paid { dimension “viewsion” } } sourceSets { appleRedFree { java.srcDirs = ['src/main/java', 'src/appleRed/java'] } appleRedPaid { java.srcDirs = ['src/main/java', 'src/appleRed/java'] } appleYellowFree { java.srcDirs = ['src/main/java', 'src/appleYellow/java'] } appleYellowPaid { java.srcDirs = ['src/main/java', 'src/appleYellow/java'] } peairRedFree { java.srcDirs = ['src/main/java', 'src/peairRed/java'] } peairRedPaid { java.srcDirs = ['src/main/java', 'src/peairRed/java'] } peairYellowFree { java.srcDirs = ['src/main/java', 'src/peairYellow/java'] } peairRedPaid { java.srcDirs = ['src/main/java', 'src/peairYellow/java'] } } // Other stuff here } } android { // Other stuff here flavorDimensions "fruit", "color”, “viewsion” productFlavors { apple { dimension "fruit" } peair { dimension "fruit" } red { dimension "color" } yellow { dimension "color" } free { dimension "viewsion" } paid { dimension “viewsion” } } sourceSets { appleRedFree { java.srcDirs = ['src/main/java', 'src/appleRed/java'] } appleRedPaid { java.srcDirs = ['src/main/java', 'src/appleRed/java'] } appleYellowFree { java.srcDirs = ['src/main/java', 'src/appleYellow/java'] } appleYellowPaid { java.srcDirs = ['src/main/java', 'src/appleYellow/java'] } peairRedFree { java.srcDirs = ['src/main/java', 'src/peairRed/java'] } peairRedPaid { java.srcDirs = ['src/main/java', 'src/peairRed/java'] } peairYellowFree { java.srcDirs = ['src/main/java', 'src/peairYellow/java'] } peairRedPaid { java.srcDirs = ['src/main/java', 'src/peairYellow/java'] } } // Other stuff here } 
Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.