Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

android

gradle

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?

like image 469
SteveC Avatar asked Dec 20 '13 09:12

SteveC


People also ask

Where do I put meta data in Android Manifest?

Add meta-data: Add metadata to the app module's AndroidManifest. xml file under the application tag. You can add as many metadata elements as you want and you can also add metadata under the activity, service, broadcast receiver, content provider tags. The metadata tag has name , value , and resource .

How do I edit Android Manifest?

The Android manifest file is a specially formatted XML file. You can edit the XML manually by clicking on the AndroidManifest. xml tab. Android manifest files generally include a single <manifest> tag with a single <application> tag.

Where is the manifest file in Android Studio?

The file is located at WorkspaceName>/temp/<AppName>/build/luaandroid/dist. The manifest file provides essential information about your app to the Android operating system, and Google Play store. The Android manifest file helps to declare the permissions that an app must have to access data from other apps.

How do I add a dependency in gradle?

To add a dependency to your project, specify a dependency configuration such as implementation in the dependencies block of your module's build.gradle file.


2 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"/>
like image 90
ultraon Avatar answered Sep 28 '22 02:09

ultraon


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.

like image 21
Andros Avatar answered Sep 28 '22 01:09

Andros