How to Read Metadata from AndriodManifest File

Metadata tags are key-value pairs and one of the decent ways to access the API keys from the libraries. They are added to the AndroidManifest.xml under the application tag and are available through the PackageManager
If you have worked with third-party libraries or even with App shortcuts, Downloadable Fonts, Firebase MLKit, you may have come across metadata tags.

In the article, let’s add the metadata tags to the app module’s Manifest file and read the metadata key-values and the key-resource from a library module both in kotlin and java.

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.



    android:value="masndfiu4er9funwikesd" />




The metadata tag has name, value, and resource. name should be unique and value or the resource is associated with the name. For simple data like strings, integers, float, booleans use value attribute and for XML resource ids use resource attribute.

In the first metadata, value is a string and in the second metadata, resource has reference to an array in the arrays.xml

Read the value from the meta-data tag:

Create a Library Module and an entry class for the library. Using the PackageManager we will try to read the metadata specified for the application. This object will be a bundle, so getting a string from it should be a familiar process.


@Nullable ApplicationInfo applicationInfo = null;

try {

    applicationInfo = context.getPackageManager()

            .getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);
} catch (PackageManager.NameNotFoundException e) {



@Nullable String apiKey = null;

if (applicationInfo != null) {

    // Get the value from the key

    apiKey = applicationInfo.metaData.getString("com.gsrikar.library.LIBRARY_ACCESS_KEY");


if (apiKey != null) {

    if (DBG) Log.d(TAG, "Api Key: " + apiKey);




    .getApplicationInfo(context.packageName, PackageManager.GET_META_DATA)

    .apply {

        val apiKey = metaData.getString("com.gsrikar.library.LIBRARY_ACCESS_KEY")

        if (DBG) Log.d(TAG, "Api Key: $apiKey")


Read the resource from the meta-data tag

Add the cities array to arrays.xml

<array name="cities">
    <item>New York</item>
    <item>Los Angeles</item>

When reading the resource from a bundle we have to look for the integer id first and then using it to get the resource. Since the resource id has a reference string array, we can get it using getStringArray().


// Get the resource id

final int resourceId = metaData.getInt("com.gsrikar.library.LIBRARY_RESOURCE_KEY");

// Get the list of cities

final String[] cityArray = context.getResources().getStringArray(resourceId);

// Print the list of cities to logcat

for (final String city : cityArray) {

    Log.d(TAG, "City Name: " + city);



// Get the list of cities

val resourceId = metaData.getInt("com.gsrikar.library.LIBRARY_RESOURCE_KEY")

val cities = context.resources.getStringArray(resourceId)

cities.forEach { Log.d(TAG, "City: $it") }

If you are having trouble understanding the article or want to explore the code yourself, feel free to clone the Read Manifest project on Github.

Popular posts from this blog

Add Spacing to Recycler View Linear Layout Manager Using Item Decoration

Translucent and Transparent Background for Activity

Add Options Menu to Activity and Fragment

Create Assets Folder, Add Files and Read Data From It