Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Three product flavors: two very similar, one smaller but different

Right now I have two productFlavors in gradle. I need to add a third one. The two existing product flavors share pretty much everything. Call them Cat and Dog — they are both animals. The third flavor I need to add is essentially a sandboxed version of the app. We might call it Bike.

Say my app now has twelve activities all shared by Cat and Dog. But the Bike product flavor should only have access to three of those Activities and Bike needs to have it’s own launcher activity. How do I refactor my code to accommodate this intelligently? Again, two flavors share pretty much everything, while one flavor share much less with the other two.

UPDATE

There seems to have a smart way to solve this using Change default source set configurations. Basically, I would keep the /main sourceSet for all that is common over the whole app; a /dogCat sourceSet for all that is common to both Dog and Cat; and a /bike sourceSet for what belongs only to Bike.

Having figured out so much, I am still having some problems writing the gradle sourceSets portion

android {
  ...
  sourceSets {
    . . . 
  }
}
like image 933
Nouvel Travay Avatar asked Jan 13 '17 20:01

Nouvel Travay


4 Answers

Keep all the common classes in the main flavor. For Example, you have three flavors, Cat, Dog and Bike. In these three flavors, Cat and Dog are mostly same, except some. On the other hand Bike is also having some Classes which is common.

Three Scenarios:

01. When all flavor have common functionality

Like, Cat, Dog and Bike all have one class which is named as PriceInformation. Then keep this class in the main flavor.

02. When Cat and Dog have same functionality but Bike don't.

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

03. When only Bike have common functionality, but the other two flavor don't.

Then keep that 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 I'm attaching.

Since, MainActivity is common then mention only in main flavor.

like image 93
Aman Shekhar Avatar answered Sep 20 '22 01:09

Aman Shekhar


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 am going to test to confirm

like image 32
Nouvel Travay Avatar answered Sep 20 '22 01:09

Nouvel Travay


You can create multiple flavours of your project just follow below step:

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

Step 2. App build.gradle

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 : Left side of android studio there are two slide option Favourites and Build Varients enter image description here click on Build Varients and select BuildVarient for run perticulat flavour

Step 4 : Run Appication

like image 39
Vishal Patoliya ツ Avatar answered Sep 18 '22 01:09

Vishal Patoliya ツ


@Nouvel Travay, See what happens is when you define flavors you define different ways that application behave. Suppose if you want to show different strings in different flavors then you will need to define those strings in those different flavors individually. Similarly if you want to define different behavior/functionality then you will have to individually define those behavior in different flavor. IMPORTANT :- The File name should remain the same though, like you can name MainActivity for one flavor but not different name like StartActivity for same behavior for other flavor. Hope I made you understand. Feel free to ask for more clearance.

like image 29
Gaurav A Dubey Avatar answered Sep 22 '22 01:09

Gaurav A Dubey