Three product flavors: two very similar, one smaller but the other

Right now I have two productFlavors in gradle. I need to add a third. Almost all of the two existing flavors of the product are shared. Call them "Cat and Dog" - they are both animals. The third taste that I need to add is essentially the sandbox of the application version. We could call it a bike.

Say that my application now has twelve actions that all share Cat and Dog. But the taste of a Bike product should only have access to three of these activities, and a bicycle should have its own launch activity. How do I reorganize my code to be reasonable? Again, almost everything shares two flavors, while one flavor shares much less with the other two.

UPDATE

There seems to be a reasonable way to solve this problem using Change the default source settings . Basically, I would keep the original set of /main for everyone, which is common to the whole application; a /dogCat sourceSet for all that is typical for dogs and cats; and /bike sourceSet for what belongs only to Bike.

Having figured out so much, I'm still having trouble writing the gradle part of sourceSets

 android { ... sourceSets { . . . } } 
+5
source share
5 answers

Keep all common classes in top flavor. For example, you have three flavors: cat, dog and bicycle. In these three flavors, Cat and Dog are basically the same, except for a few. On the other hand, Bike also has some Classes that are common.

Three scenarios:

01. When all the flavors have common functionality

It seems that Cat, Dog and Bike have one class called PriceInformation. Then save this class in the main flavor.

02. When Cat and Dog have the same functionality, but Bike does not.

Like, Cat and Dog have a common functionality called LifeSpan, then save this class only in this flavor.

03. When only a bicycle has common functionality, but the other two options do not.

Then save this particular class only in Bike Flavor.

 apply plugin: 'com.android.application' android { compileSdkVersion 25 buildToolsVersion "25.0.2" defaultConfig { applicationId "com.productflavor" minSdkVersion 14 targetSdkVersion 25 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } productFlavors { cat { applicationId "com.cat.theme" } dog { applicationId "com.dog.theme" } bike { applicationId "com.bike.theme" } } } 

Below is the image that I am attaching.

Since MainActivity is common, then only the main flavor is mentioned.

+3
source

I think I was able to solve my own problem. All I have to do is

 android { ... sourceSets { main { … } dog { java.srcDirs = ['dog_cat/java'] … } cat { java.srcDirs = ['dog_cat/java'] … } bike { java.srcDirs = ['bike/java']//which is actually superfluous … } } 

Now i'm going to check to confirm

+1
source

You can create several variations of your project simply by following below:

Step 1. Copy Res. folder App / src / res and paste the application / src / YourFlavourName (Cat / Dog / Bike) / Res.

Step 2. The build.gradle application

android {

  productFlavors { Cat { applicationId = "com.cat" } Dog { applicationId = "com.dog" versionCode 1 versionName "1.0" } Bike { applicationId = "com.bike" versionCode 1 versionName "1.0" } } 

sourceSets {

  main { aidl.srcDirs = ['src'] renderscript.srcDirs = ['src'] jniLibs.srcDirs = ['libs'] } Cat { java.srcDirs = ['src/Cat/java', 'src/Cat/java/'] } Dog { java.srcDirs = ['src/Dog/java', 'src/Dog/java/'] } Bike { java.srcDirs = ['src/Bike/java', 'src/Bike/java/'] } } 

}

Step 3: On the left side of the Android studio there are two slide options "Favorites" and "Build options" enter image description here click Build Options and select BuildVarient to launch the perticulat flavor.

Step 4: launch the application

+1
source

@Nouvel Travay. See what happens when you identify flavors that you define in different ways the application behaves. Suppose if you want to show different lines in different ways, you will need to define these lines in these different tastes individually. Similarly, if you want to define a different behavior / functionality, you will have to individually define this behavior for different tastes. IMPORTANT: - The file name should remain the same, but you can name MainActivity for one flavor, but not otherwise than StartActivity for the same behavior for another flavor. I hope I made you understand. Feel free to request more clearance.

+1
source

You can try the following two approaches:

  • Realize all the actions in your main fragrance . (java / main is shared with all Gradle specific flavors), and every flavored Android manifest should only reference actions that they should work on. With Proguard, you can ensure that unused code from unrelated actions is not deployed to your artifact (APK). You can have different launchers if you defined each flavored AndroidManifest.xml

  • Work with library projects. Thus, you can load in each color a different set of libraries and again a different manifest.

I will try to extend this answer to some examples at some other times.

0
source

Source: https://habr.com/ru/post/1262776/


All Articles