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.

Juan Bentel
  • 477
  • 2
  • 5
  • 15
  • 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? – Turnsole Mar 05 '14 at 22:13
  • for future readers, make sure you've put under tag. Not under tag. – aldok Oct 14 '17 at 16:16

2 Answers2

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"));
Jorgesys
  • 124,308
  • 23
  • 334
  • 268
  • 1
    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! – Juan Bentel Mar 05 '14 at 23:57
  • 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. – ashubuntu Jan 17 '17 at 05:22
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>
IAmCoder
  • 3,179
  • 2
  • 27
  • 49