Home » Android » android – How to add meta-data to gradle/AndroidStudio generated manifest

android – How to add meta-data to gradle/AndroidStudio generated manifest

Posted by: admin June 15, 2020 Leave a comment

Questions:

I’m migrating from an Ant/Eclipse environment to Gradle/Android Studio, and some tools we use need the Android app to have meta-data tags present with both android:name and android:value fields set.

We currently have two build-variants of the app, specified as productFlavours/buildTypes in the build.gradle file. As the Android manifest.xml files are generated by gradle at build-time, I obviously can’t just put the meta-data in the xml file directly.

Is there a way to specify this in the build.gradle file so that both buildTypes have the meta-data field added to the generated Manifest.xml, but with different “android:value” values?

How to&Answers:

You can use this approach:

  1. 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 sync project with Gradle files use in AndroidManifest.xml:
    <meta-data android:name="MY_META" android:value="@string/my_string"/>

Answer:

Why can’t you put the 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 in debug, gradle will merge the “main” manifest with the debug manifest.

Answer:

inspired by a few other answers, this works well…

1) In your manifest put a placeholder:

<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 making a resource, and the raw manifest reads a little more clear that a value is going to be injected at build time.

For more on manifest placeholder support: Android Tools and Manifest Placeholders

Answer:

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.