5

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
Tom
  • 3,587
  • 9
  • 69
  • 124
  • The exception says it can't read packageName from AndroidManifest.xml. What manifest files in your flavors actually exist? – Sofi Software LLC May 24 '18 at 16:55
  • But why do you need 3 dimensions? Have you tried to define just one and use that one? – Cata May 24 '18 at 16:55
  • Also you are asking about debug variants. You're only defining one buildType: release. Add another one for debug. It can be empty. – Sofi Software LLC May 24 '18 at 16:56
  • You probably need only 1 dimension – EpicPandaForce May 24 '18 at 17:09
  • @Cata I have changed it to a single now - no difference in results though – Tom May 25 '18 at 07:36
  • @SofiSoftwareLLC I have added it now - no difference in results though – Tom May 25 '18 at 07:38
  • @SofiSoftwareLLC each flavor has each own complete manifest. (In the past I would simply manually copy assets, resources, manifest etc. into "main" folder) --- however - bear in mind that the error message acually states it is trying o read the manifest file in the "main" folder – Tom May 25 '18 at 07:39
  • Share the link to your full project if possible. – Akshar Patel May 25 '18 at 11:47
  • Hello dear don't miss the following link https://stackoverflow.com/a/66315794/12134511 your problem fully satisfy. I have using following link code.It's code working fine. – Er.Prem Singh daksha Feb 23 '21 at 04:27

3 Answers3

2

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

Santhosh
  • 160
  • 7
2

I think there are two unrelated problems :

  • Currently you have 2 build types (the automatically created debug and release) and 3 dimensions (dim1, dim2 and dim3), each one having 1 variant (flavor1 for dim1, flavor2 for dim2, ...) this gives at most :

    2 * 1 * 1 * 1 = 2 combinations

    You should switch to 2 build types and 1 dimension (say dim1) having 3 variants (flavor1, flaver2 and flavor3) to have :

    2 * 3 = 6 apks

  • You should have a main manifest. Unlike other resources the manifest is not simply overriden but merged from multiple sources (see Merge Multiple Manifest Files for more details).

    It should at least contains a package name (possibly different from the final applicationId(s)) as explained by this note from Configure Product Flavors :

Note : You still need to specify a package name using the package attribute in the main/ manifest file. You must also use that package name in your source code to refer to the R class, or resolve any relative activity or service registration. This allows you to use applicationId to give each product flavor a unique ID for packaging and distribution, without having to change your source code.

bwt
  • 17,292
  • 1
  • 42
  • 60
0

I would have liked to get

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

For this, you need to define the same dimension for all flavors.

I get this error

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

You get this error because the path is not reachable.

Just think, how can app find W: when it is running?

So, you need to use a relative path here.

Also from official documentation (https://developer.android.com/studio/build/build-variants#configure-sourcesets):

If you have sources that are not organized into the default source set file structure that Gradle expects, as described above in the section about creating source sets, you can use the sourceSets block to change where Gradle looks to gather files for each component of a source set. You don't need to relocate the files; you only need to provide Gradle with the path(s), relative to the module-level build.gradle file, where Gradle should expect to find files for each source set component

Akshar Patel
  • 8,998
  • 6
  • 35
  • 50