How to add metadata to gradle / AndroidStudio generated manifest

I am moving from Ant / Eclipse to Gradle / Android Studio, and some of the tools we use should have an Android meta tag app with both android: name and android: value fields present.

We currently have two build options for the application, indicated as productFlavours / buildTypes in the build.gradle file. Since manifest.xml files for Android are generated by gradle during build, I obviously can't just put the metadata in the xml file directly.

Is there a way to specify this in the build.gradle file so that both types of buildTypes add a metadata field to the generated Manifest.xml, but with different "android: value" values?

+8
android gradle
source share
4 answers

You can use this approach:

  • build.gradle:
buildTypes { debug { ... resValue "string", "my_string", "string value debug" } release { ... resValue "string", "my_string", "string value release" } } 

or

  productFlavors { staging { ... resValue "string", "my_string", "string value staging" } production { ... resValue "string", "my_string", "string value production" } } 
  1. After the synchronization project with Gradle files is used in AndroidManifest.xml:
  <meta-data android:name="MY_META" android:value="@string/my_string"/> 
+21
source share

Why can't you put meta-data directly in the manifest?

You can specify a manifest for each gradle buildTypes via sourceSets :

 sourceSets { main { manifest.srcFile 'src/main/AndroidManifest.xml' res.srcDirs = ['src/main/res'] } debug { manifest.srcFile 'src/main/debug/AndroidManifest.xml' res.srcDirs = ['src/main/debug/res'] } release { manifest.srcFile 'src/main/release/AndroidManifest.xml' res.srcDirs = ['src/main/release/res'] } } 

here, if you build debug, gradle will combine the "main" manifest with the debug manifest.

+9
source share

inspired by several other answers, this works well ...

1) Placeholder is placed in your manifest:

 <meta-data android:name="MY_META" android:value="${someKeyForValue}"/> 

2) In your build.gradle set the value

 flavorDimensions "env" productFlavors { dev { flavorDimension "env" ... someKeyForValue = "TheDynamicValue_01" } prod { flavorDimension "env" ... someKeyForValue = "TheDynamicValue_02" } } 

This approach avoids creating the resource, and the raw manifest reads a little more clearly that the value will be entered during assembly.

More on box replacement support: Android tools and manifest placeholders

+6
source share
 android { ... buildTypes { debug { ... resValue "string", "GOOGLE_MAPS_ANDROID_API_KEY", "(your development Maps API key)" } release { ... resValue "string", "GOOGLE_MAPS_ANDROID_API_KEY", "(your production Maps API key)" } } } <meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="@string/GOOGLE_MAPS_ANDROID_API_KEY"/> Your IDE may complain about this string resource not existing, but it will build just fine. 
+1
source share

All Articles