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.

<meta-data

    android:name="com.gsrikar.library.LIBRARY_ACCESS_KEY"

    android:value="masndfiu4er9funwikesd" />

<meta-data

    android:name="com.gsrikar.library.LIBRARY_RESOURCE_KEY"

    android:resource="@array/cities"/>

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.

Java:


@Nullable ApplicationInfo applicationInfo = null;

try {

    applicationInfo = context.getPackageManager()

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

    e.printStackTrace();

}



@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);

}

Kotlin:

context.packageManager

    .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>Bangalore</item>
    <item>Hyderabad</item>
    <item>Chennai</item>
    <item>New York</item>
    <item>Los Angeles</item>
    <item>London</item>
</array>

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().

Java:

// 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);

}

Kotlin:

// 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

How to Change Material Chip Text Size, Text Style and Font

Create Assets Folder, Add Files and Read Data From It

Add Spacing to Recycler View Linear Layout Manager Using Item Decoration

Change Snackbar Position When Bottom App Bar or Bottom Navigation View Is Present