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.xmlunder the application tag and are available through the
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 metadata to the app module's
AndroidManifest.xmlfile under the
applicationtag. 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
nameshould be unique and
resourceis 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
Read the value from the meta-data tag:
Create a Library Module and an entry class for the library. Using the
PackageManagerwe 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.
Read the resource from the meta-data tag
Add the cities array to
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
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.