Unable to read the metadata of AndroidManifest.xml in unit test cases

Viewed 351

I am writing local unit testcases for my android application. In one of the test cases, i need to read the contents of metadata defined in the AndroidManifest.xml

I can easily read the data by calling these methods - metadata.getString() / metadata.getBoolean() while running the application

I Cannot access the same in unit testing. the values of the all the keys defined in maifest appears to be null here.

In the below code, object ai is created properly, res is null, aM is null,

metadata is not null and it has the values for hub_url, enable_transmission and authentication as defined in manifest file.

Its strange that when i try to access contents of metadata in variable a, b and c, all are null

    val context = ApplicationProvider.getApplicationContext<Context>()
    val ai = context!!.packageManager.getApplicationInfo( context.packageName, PackageManager.GET_META_DATA)
    val res = ai.metaData.getBoolean( context.getString(R.string.enable_transmission))

    val aM = ai.metaData.get( context.getString(R.string.enable_transmission))
    val metadata = ai.metaData!!;

    val a = metadata.getString(context.getString(R.string.hub_url))
    val b = metadata.get(context.getString(R.string.enable_transmission));
    val c = metadata.get(context.getString(R.string.authentication));

Here is the manifest file content

 <application>

    <meta-data
        android:resource="@string/basic"
        android:name="@string/authentication"
        android:value="@string/basic" />

    <meta-data
        android:resource="@string/hub_url"
        android:name="@string/hub_url"
        android:value="@string/hub_url" />

    <meta-data
        android:resource="@bool/enable"
        android:name="@string/enable_transmission"
        android:value="@bool/enable" />


</application>
1 Answers

Try this code,

val  context = applicationContext
val ai = context!!.packageManager.getApplicationInfo( context.packageName, PackageManager.GET_META_DATA)
val metadata = ai.metaData!!
val res = metadata.get( context.getString(R.string.enable_transmission))
val aM = metadata.get(context.getString(R.string.enable_transmission))

In manifest meta-data tag only using android:value not using android:resource than object direct convert in particular format.

Related