72

I am new to android and I have not seen or heard about meta data before. However I google it and search about it on YouTube that it is basically a information of your object. Correct me if I am wrong. Can any one help me to understand it in a better way.

1) What is meta data?

2) Why is it used in Android?

It will be good if explanation is given with example of why metadata is used in Android. I have seen them inside manifest's activity metadata tag.

CopsOnRoad
  • 237,138
  • 77
  • 654
  • 440
FaisalAhmed
  • 3,469
  • 7
  • 46
  • 76
  • they are just used for the storing the data in the key value pair which can be called from the parent component. This much knowledge is enough rest there is no end to exploration. – cammando Dec 15 '16 at 14:53

4 Answers4

130

In Android, you can define meta-data information in your AndroidManifest.xml

HERE IS THE DOCK LINK

Very basic usage

It is basically an additional option to store information that can be accessed through the entire project. In this case, <meta-data> is defined outside <activity> tag and inside <application> tag.

Defining:

<manifest>
    <application 
        android:icon="@drawable/icon" 
        android:label="@string/app_name">

        <meta-data android:name="my_test_metagadata" android:value="testValue" />

        <activity 
            android:name=".MainActivity" 
            android:label="@string/app_name">

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

        </activity>

    </application>
<manifest>

Reading:

ApplicationInfo ai = getPackageManager().getApplicationInfo(getPackageName(), PackageManager.GET_META_DATA);
Bundle bundle = ai.metaData;
String myApiKey = bundle.getString("my_test_metagadata");

You can save a boolean, an int, String or float.

It's useful for library or APIs

Let's say that you created an API/LIB which can be used for everyone. However, for a specific procedure, you need a KEY and that KEY must be defined by the developer who will use your API. This way, you can not predict which key the developer will share.

Using <meta-data>, a developer who wants to use your API/LIB can share the KEY with you. This way, you leave your API configured to read that KEY and raise an exception if the user did not define.

try {
    ApplicationInfo ai = getPackageManager().getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);
    Bundle bundle = ai.metaData;
    String myApiKey = bundle.getString("my_test_metagadata");
} catch (Exception e) {
    Log.e(TAG, "Dear developer. Don't forget to configure <meta-data android:name=\"my_test_metagadata\" android:value=\"testValue\"/> in your AndroidManifest.xml file.");
}

One classic example is Google Ads (Admob).

You must add following line to your AndroidManifest:

<!--This meta-data tag is required to use Google Play Services.  (adMob)-->
<meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />

This will load com.google.android.gms.version with value represented by @integer/google_play_services_version. Then, probably, Google Play Services (Admob) will read this metadata and it will be able to determine the version of Google Play Service that you used when you built your app.

Another example

Another usage for <meta-data> is when to use them to configure an Activity. This way you can pass valuable information to android about your activity, and then Android can handle your activity properly. In this case, the <meta-data> tag is added inside the <activity> tag.

The first example I see is when you define a Search Activity.

<manifest>
    <application 
        android:icon="@drawable/icon" 
        android:label="@string/app_name">
        <activity 
            android:name=".MainActivity" 
            android:label="@string/app_name">

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

        </activity>

        <activity android:name=".SearchableActivity" >
            <intent-filter>
                <action android:name="android.intent.action.SEARCH" />
            </intent-filter>
            <meta-data android:name="android.app.searchable"
                       android:resource="@xml/searchable"/>
        </activity>
    </application>
<manifest>

Then, to get the meta-data from the activity tag, use this:

try {
        ActivityInfo ai = getPackageManager().getActivityInfo(this.getComponentName(), PackageManager.GET_META_DATA);
        Bundle bundle = ai.metaData;
        if (bundle != null) {
            String apiKey = bundle.getString("apikey");
            Log.d(this.getClass().getSimpleName(), "apiKey = " + apiKey);
            }
        }
    } catch (PackageManager.NameNotFoundException e) {
        Utilities.log(this.getClass().getSimpleName(), "Failed to load meta-data, NameNotFound: " + e.getMessage());
    } catch (NullPointerException e) {
        Log.e(this.getClass().getSimpleName(), "Failed to load meta-data, NullPointer: " + e.getMessage());
    }
marienke
  • 2,465
  • 4
  • 34
  • 66
guipivoto
  • 18,327
  • 9
  • 60
  • 75
  • 2
    Hi we can also declare the key in the `string` resource then what is special in the `meta-data`? mean as you mention above `` I can declare same using string resource `3.4` so then what is special in the `meta-data`? – Asif Mushtaq Apr 14 '18 at 20:13
  • @AsifMushtaq Imagine you wrote once Android library which will be used by many developer. And your library needs some key to process data. In that case developer who are using your library will just add those key in there app manifest as meta data.And that key will be used by your library Hope it make sense – FaisalAhmed Dec 14 '18 at 12:45
  • additionally, meta-data can read by other applications with just package-name. – Hadi Mar 05 '20 at 16:30
  • May a `meta-data` tag be enabled/disabled programmatically? For instance, a web browser app implementing `WebView` that offers users the ability to [opt-in/out of Google metrics collection](https://developer.android.com/guide/webapps/managing-webview#metrics). – AdamHurwitz Dec 06 '20 at 18:35
  • @W0rmH0le, can we apply meta-data for activity dynamically (Not declare in Manifest )? – Hardik Paranjape Jan 14 '21 at 21:28
6

Let me give a simple example. It helps in finding additional information -- is the basic explanation of metadata.

317-745-4618

If I add the metadata that it's a phone number than you know or can figure out the geographical area from this. If I did not state that its a phone number than you do not have enough metadata to know what object is this. It could be a US SSN or it may be something else.

It's used in Android to add objects to the manifest, for example if using Google Service one would add it to designate the Google Services version the app binds to.

CopsOnRoad
  • 237,138
  • 77
  • 654
  • 440
Fred Grott
  • 3,505
  • 1
  • 23
  • 18
3

Metadata is data that describes other data to inform other applications how to use the data itself.

Android usage:
Intents are great example for that - If you want to pass data in intents it has to be primitive because Android only have pre-build metadata about those kind of objects. (String and integer have different binary structure that the system know how to work with).

Intents also allow you to build your own metadata to your custom objects via the Parcel class (this process of manually build you own metadata called marshalling)

Abhimanyu
  • 11,351
  • 7
  • 51
  • 121
Nir Duan
  • 6,164
  • 4
  • 24
  • 38
0

The Content Policy applies not only to app content, but also to the metadata for your app, such as app title, descriptions, images, and keywords. While an app itself might not violate any policy guidelines, the app might still be rejected or suppressed from the Appstore if the metadata contains violations. See Amazon Appstore Listing and Promotion Policy

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 21 '23 at 15:38