I'm currently running into trouble retrieving <meta-data>
tags from my AndroidManifest file while being inside an InstrumentationTest.
I am using a library ( Sugar ORM ) which stores some essential information inside these tags. As soon as I use the library inside the code to be tested I run into problems.
AndroidManifest.xml
<manifest package="org.something" ...>
...
<application ...>
<meta-data android:name="DATABASE" android:value="foo.db" />
...
</application>
</manifest>
Retrieval of the metadata works like that:
PackageManager pm = context.getPackageManager();
try {
ApplicationInfo ai = pm.getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);
value = ai.metaData.getString(name);
} catch (Exception e) {
Log.d("sugar", "Couldn't find config value: " + name);
}
The test is running as a InstrumentationTestCase
and sets up the context to be getInstrumentation().getContext()
.
I know that this is quite old, however if anyone else is interested, I had this same problem I fixed it by moving the meta-data fields into the application.
So this:
<?xml version="1.0" encoding="utf-8"?>
<manifest
package="com.percolate.coffee"
xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<meta-data android:name="DATABASE" android:value="coffee.db" />
<meta-data android:name="VERSION" android:value="2" />
<meta-data android:name="QUERY_LOG" android:value="true" />
<meta-data android:name="DOMAIN_PACKAGE_NAME" android:value="com.percolate" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
android:name="com.orm.SugarApp">
<activity
android:name=".activity.CoffeeTypeListViewActivity"
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=".activity.CoffeeTypeDetailedViewAcitivity"
android:label="@string/title_activity_coffee_type_detailed_view_acitivity"
android:parentActivityName=".activity.CoffeeTypeListViewActivity">
</activity>
<!-- SERVICES -->
<service
android:name="com.octo.android.robospice.Jackson2SpringAndroidSpiceService"
android:exported="false"/>
<service
android:name="com.octo.android.robospice.spicelist.okhttp.OkHttpBitmapSpiceService"
android:exported="false"/>
</application>
becomes this:
<?xml version="1.0" encoding="utf-8"?>
<manifest
package="com.percolate.coffee"
xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<meta-data android:name="DATABASE" android:value="coffee.db" />
<meta-data android:name="VERSION" android:value="2" />
<meta-data android:name="QUERY_LOG" android:value="true" />
<meta-data android:name="DOMAIN_PACKAGE_NAME" android:value="com.percolate" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
android:name="com.orm.SugarApp">
<activity
android:name=".activity.CoffeeTypeListViewActivity"
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=".activity.CoffeeTypeDetailedViewAcitivity"
android:label="@string/title_activity_coffee_type_detailed_view_acitivity"
android:parentActivityName=".activity.CoffeeTypeListViewActivity">
</activity>
<!-- SERVICES -->
<service
android:name="com.octo.android.robospice.Jackson2SpringAndroidSpiceService"
android:exported="false"/>
<service
android:name="com.octo.android.robospice.spicelist.okhttp.OkHttpBitmapSpiceService"
android:exported="false"/>
</application>
The problem is that InstrumentationTestCase
does not contain the meta data from your application. All you need to do is extend ApplicationTestCase<Application>
for your test case class.
You can call getContext() in your test cases and that will have the appropriate information in there.
PackageManager pm = getContext().getPackageManager();
ApplicationInfo ai = pm.getApplicationInfo(getContext().getPackageName(), PackageManager.GET_META_DATA);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.