Android How to - Get APK MetaData








Question

We would like to know how to get APK MetaData.

Answer

//from w w w. j  ava2 s. c om
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;

public class Main {
  public static String getApkMetaData(Context context, String apkPath,
      String key) {

    if (context == null || key == null) {
      return null;
    }

    PackageManager pm = context.getPackageManager();
    PackageInfo info = pm.getPackageArchiveInfo(apkPath,
        PackageManager.GET_META_DATA);
    if (info != null) {
      ApplicationInfo appInfo = info.applicationInfo;

      if (appInfo == null || appInfo.metaData == null) {
        return null;
      }

      return appInfo.metaData.getString(key);
    }

    return null;
  }
}