Chatumbabub Chatumbabub - 3 months ago 23
C# Question

How to access metadata from AndroidManifest.xml in Xamarin.Froms

I'm trying to access metadata from AndroidManifest.xml in Xamarin.Forms Android project.

I have this code:

var ai = CrossCurrentActivity.Current.Activity.PackageManager
.GetApplicationInfo(CrossCurrentActivity.Current.Activity.PackageName,
PackageInfoFlags.MetaData);
var meta = ai.MetaData;


Problem is, that ai.Metadata is NULL. Any idea what is wrong?
Thanks.

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:installLocation="auto" package="com.my.test.x2.x8" android:versionName="15.0" android:versionCode="15">
<uses-sdk android:minSdkVersion="15" />
<application android:label="MyApp" android:icon="@drawable/Icon"></application>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application>
<meta-data android:name="MYENDPOINT" android:value="https://xxx.azurewebsites.net/" />
</application>
</manifest>

Answer

Place your meta-data inside your existing application span and remove the other one, i.e.:

Example:

<application android:label="MyApp" android:icon="@drawable/Icon">
        <meta-data android:name="MYENDPOINT" android:value="https://xxx.azurewebsites.net/" />
</application>

Usage:

var bundle = PackageManager.GetApplicationInfo(PackageName, Android.Content.PM.PackageInfoFlags.MetaData).MetaData;
foreach (var key in bundle.KeySet())
{
    Log.Debug("SO", $"{key} : {bundle.GetString(key)}");
}

Output:

08-03 09:18:56.642  3519  3519 D SO      : MYENDPOINT : https://xxx.azurewebsites.net
Comments