There are two product flavors:

productFlavors { demo { ... } full { ... } } 

and two build types:

 buildTypes { release { ... } debug { ... } } 

Proceeding from the existing product flavors and build types, there are four build versions:

 demoRelease demoDebug fullRelease fullDebug 

Is it possible for each of the four build options in AndroidManifest.xml to prescribe different values ​​for some attributes? If possible, how? Using manifest placeholders or something else?

For example, there are four API keys for some service, and depending on the specific build version in AndroidManifest.xml, you need to insert your own key.


Now I have this implemented using a separate strings.xml for each build option, but I would like to write API keys somewhere in one file and not create a bunch of additional files .

Interested in something like this solution (in the first part of the answer).

  • And what's the problem to use the option that you suggested? - zTrap
  • @zTrap, Look at the penultimate paragraph of the question. I want to get a more beautiful solution. - post_zeew
  • I mean the link you added is zTrap
  • @zTrap, That option just doesn't work :) - post_zeew
  • Perhaps this option will push you to the right solution - zTrap

2 answers 2

I have done it this way (build.gradle):

 android { defaultConfig { applicationId "blah-blah" targetSdkVersion targetSdkVersionVar } productFlavors { honeycumb { minSdkVersion minSdkHoneycumb targetSdkVersion targetSdkHoneycumb } kitkat { minSdkVersion minSdkKitkat targetSdkVersion targetSdkVersionVar } } } 

The rest is written in src/main , src/honeycumb , src/kitkat

  • Here you have separate parameters for each of the product flavors , and I need separate parameters for each of the build options . - post_zeew

You can supplement the option proposed by you in this way:

build.gradle

 android { defaultConfig { manifestPlaceholders = [ apiKey:project.property('default.ApiKey') ] } } applicationVariants.all{ variant-> if (variant.productFlavors.get(0).name.equals("demo")) { if (variant.buildType.name.equals("release")) { manifestPlaceholders = [ apiKey:project.property('release.demo.ApiKey') ] } else { manifestPlaceholders = [ apiKey:project.property('debug.demo.ApiKey') ] } } else if (variant.productFlavors.get(0).name.equals("full")){ if (variant.buildType.name.equals("release")) { manifestPlaceholders = [ apiKey:project.property('release.full.ApiKey') ] } else { manifestPlaceholders = [ apiKey:project.property('debug.full.ApiKey') ] } } } 

AndroidManifest.xml

 <meta-data android:name="your name" android:value="${apiKey}"/> 

gradle.properties (global)

If it is not there, then it is necessary to create and place it along the path <USER_HOME>/.gradle .

 release.demo.ApiKey="your_key" debug.demo.ApiKey="your_key" release.full.ApiKey="your_key" debug.full.ApiKey="your_key" default.ApiKey="your_key" 

A global file is needed only if the code is uploaded to a public repository, since it is ignored by version control systems (relevant for libraries). Otherwise, you can use the project gradle.properties .

a source

  • I tried your solution. As a result, in any build version, default.ApiKey used. - post_zeew