Android metadata from manifest returning null
Asked Answered
P

2

26

I have a class with one method that tries to retrieve meta-data from the manifest. Everything works fine except that the bundle that I create from the application info returns null values

Here's the code:

    private int getCurrentVersion(){
    int currVersion = 0;

    try {
        ApplicationInfo app = context.getPackageManager().getApplicationInfo(context.getPackageName(),PackageManager.GET_META_DATA);
        Bundle bundle = app.metaData;

        for (String key: bundle.keySet())
        {
          Log.d ("TEST", key + " is a key in the bundle");
        }

        Log.d("TEST","google: "+bundle.getString("com.google.android.gms.version"));
        Log.d("TEST","version: "+bundle.getString("dbVersion"));

        //currVersion = Integer.valueOf(bundle.getString("dbVersion"));
        currVersion = 1;
    } catch (NameNotFoundException e) {
        e.printStackTrace();
    } catch (NullPointerException e) {
        e.printStackTrace();         
    }

    return currVersion;
}


03-05 18:53:23.818: D/TEST(31400): com.google.android.gms.version is a key in the bundle
03-05 18:53:23.818: D/TEST(31400): dbVersion is a key in the bundle
03-05 18:53:23.828: D/TEST(31400): google: null
03-05 18:53:23.828: D/TEST(31400): version: null

As you can see I'm using some logs to see if the bundle is empty, but its not.

Presbyter answered 5/3, 2014 at 22:6 Comment(2)
It looks like whatever they are, they aren't Strings. Is it possible you're providing a value like "1" and that it's being interpreted as the int 1?Tina
for future readers, make sure you've put <meta-data> under <application> tag. Not under <manifest> tag.Cortezcortical
V
31

In some cases you need to use getInt() method:

bundle.getInt("com.google.android.gms.version"));

because the value of this meta-data is defined as an integer.

Log.d("TEST","google: "+bundle.getInt("com.google.android.gms.version"));

For example if the value defined in your meta-data is a String:

     <meta-data 
         android:name="com.elenasys.a"
         android:value="abcXJ434" />  

use:

   bundle.getString("com.elenasys.a"));   

if the value is an integer:

   <meta-data 
         android:name="com.elenasys.b"
         android:value="1234" />  

use getInt()

 bundle.getInt("com.elenasys.b"));
Vingtetun answered 5/3, 2014 at 23:9 Comment(2)
Oh I get it now. meta-data is a string type but it seems to be converted to int when I get the bundle from the app.metaData right? I can't believe I didn't notice. Thank you!Presbyter
I added android:value="true" and was trying with getString method when I should have used getBoolean. In my case getString was causing the error.Sambo
W
3

Also make sure the tag is under the manifest/application node in AndroidManifest.xml and not just under the root:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.example.readmetadata"
  android:versionCode="1"
  android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">

        <meta-data android:name="dbVersion" android:value="1.0" />

        <activity android:name=".MainMenu" 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>
    <uses-sdk android:minSdkVersion="3" android:targetSdkVersion="8" />
</manifest>
Whitten answered 13/12, 2018 at 14:24 Comment(2)
This did it for me. Thanks!Dahabeah
thanks a lot ! 1 hour or more left to find you :)Cahoot

© 2022 - 2024 — McMap. All rights reserved.