Product flavors in Android Studio

心已入冬 提交于 2019-11-28 02:01:18

Based on this https://developer.android.com/studio/build/build-variants document you want this type of hierarchy structure of your application. Remove assets.srcDirs from all build flavours and you can try with this kind of structure.

Like : Flavor Image, AndroidManiFest file

buildTypes {
  release {
    // ... the usual stuff here
  }
  releaseAlt {
    // .. the usual stuff here too like signing config etc...
  }
}

file hierarchy You should have like :

project/
- app/
 - src/
  - main/
   - assets/
    - logo.png // Generic assets go here
   - java/
   - res/
   - ...

  - flavor1/
   - assets/
    - logo.png // Specific assets for all the flavor1 Variants

  - release/
   - assets/
    - logo.png // Specific assets for all the releaseAlt Variants.

  - flavor1Release/
   - assets/
    - logo.png // very specific assets for the flavor1ReleaseAlt Variant
- SDK/

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

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.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!