Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Product flavors in Android Studio [duplicate]

Tags:

I can not get product flavours working. I have this gradle

apply plugin: 'com.android.application'
android {
    defaultConfig {
      minSdkVersion 14
      targetSdkVersion 24
      compileSdkVersion 27
    }
    signingConfigs {
        release {
      }
    }
    buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-project.txt'
            signingConfig signingConfigs.release
        }
    }
    repositories {
        maven { url "https://jitpack.io" }
    }
    flavorDimensions "dim1", "dim2", "dim3"
    productFlavors {
        flavor1 {
            dimension "dim1"
            applicationId "com.example.dim1.app"
        }
        flavor3 {
            dimension "dim2"
            applicationId "com.example.dim2.app"
        }
        flavor3 {
            dimension "dim3"
            applicationId "com.example.dim3.app"
        }
    }
    sourceSets {
        flavor1 {
          java.srcDirs = ["W:/android-studio-projects/sharedid/app/src/main/java/"]
          manifest.srcFile "W:/android-studio-projects/sharedid/app/src/example1/AndroidManifest.xml"
          assets.srcDirs = ["W:/android-studio-projects/sharedid/app/src/example1/assets/"]
          resources.srcDirs = ["W:/android-studio-projects/sharedid/app/src/main/res/", "W:/android-studio-projects/sharedid/app/src/example1/res/"]
        }
        flavor2 {
          java.srcDirs = ["W:/android-studio-projects/sharedid/app/src/main/java/"]
          manifest.srcFile "W:/android-studio-projects/sharedid/app/src/example2/AndroidManifest.xml"
          assets.srcDirs = ["W:/android-studio-projects/sharedid/app/src/example2/assets/"]
          resources.srcDirs = ["W:/android-studio-projects/sharedid/app/src/main/res/", "W:/android-studio-projects/sharedid/app/src/example2/res/"]
        }
        flavor3 {
          java.srcDirs = ["W:/android-studio-projects/sharedid/app/src/main/java/"]
          manifest.srcFile "W:/android-studio-projects/sharedid/app/src/example3/AndroidManifest.xml"
          assets.srcDirs = ["W:/android-studio-projects/sharedid/app/src/example3/assets/"]
          resources.srcDirs = ["W:/android-studio-projects/sharedid/app/src/main/res/", "W:/android-studio-projects/sharedid/app/src/example3/res/"]
        }
    }
}
dependencies {
    api 'com.google.android.gms:play-services-maps:15.0.0'
    api 'com.google.android.gms:play-services-location:15.0.0'
    api 'com.android.support:appcompat-v7:27.1.1'
    api 'com.github.PhilJay:MPAndroidChart:v2.0.8'

}

...

When I got to "Build | Select variant" I can only select

Module:app Build Variant:flavor1Flavor2Flavor3Debug,flavor1Flavor2Flavor3Release

I would have liked to get

  • the following build variants: flavor1Debug,flavor2Debug,flavor3Debug,flavor1Release,flavor2Release,flavor3Release

I have tried "File | Sync project with gradle file"

...

I get this error

Caused by: java.lang.RuntimeException: Cannot read packageName from W:\android-studio-projects\sharedid\app\src\main\AndroidManifest.xml

I have tried to both

  • have no such file (hoping it would take the product flavor one?)
  • have the "main" manifest only define shared stuff between all product flavors
like image 688
Tom Avatar asked May 22 '18 15:05

Tom


People also ask

How do you use product flavor on Android?

Creating Product Flavor is pretty much easy in Android Studio, we simply need to add productFlavors block inside the Android block in the application-level build. gradle file. It's that simple to create product flavors.

When would you use product flavor in your build setup?

When to use Product Flavors. When we want to address the issue of having separate project code for each version of the app while still having one project code. Given a scenario where you have a free and a paid app you can limit features in the free and expose all the other features in the paid version of the app.

What is productFlavors?

LibraryProductFlavor. TestProductFlavor. Encapsulates all product flavors properties for this project. Product flavors represent different versions of your project that you expect to co-exist on a single device, the Google Play store, or repository.


1 Answers

Just try like below,

 flavorDimensions "dim1"
productFlavors {
    flavor1 {
        dimension "dim1"
        applicationId "com.example.dim1.app"
    }
    flavor3 {
        dimension "dim1"
        applicationId "com.example.dim2.app"
    }
    flavor3 {
        dimension "dim1"
        applicationId "com.example.dim3.app"
    }
}

For more details about build variant see this link

like image 61
Santhosh Avatar answered Sep 28 '22 17:09

Santhosh