The Android implementation gets the values of meta data and build. gradle

  • 2021-11-29 08:29:20
  • OfStack

Sometimes parameters in meta-data are used, such as defined channel numbers, similar to AU statistics. It will also be used under the defaultConfig tag defined under the android tag in the build. gradle file, and it is possible to use the channel value when adding the manifestPlaceholders tag.


<meta-data
      android:name="UMENG_CHANNEL"
      android:value="0"/>

First, get the values in meta-data and look directly at the method:


 /**
   *  Different types should be obtained differently. The following are String Type of 
   * @param context  In the morning and afternoon 
   * @param metaName meta-data The name of the definition 
   * @param defaultValue  Default value 
   * @return
   */
  public static String getAppMetaDataString(Context context, String metaName, String defaultValue) {
    try {
      //application Used under labels getApplicationinfo If it is activity Use under getActivityInfo
      //Sting Use of type getString , Boolean Type of getBoolean , other specific see api
      String value = context.getPackageManager()
            .getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA)
          .metaData.getString(metaName, defaultValue);
      return value;
    } catch (PackageManager.NameNotFoundException e) {
      e.printStackTrace();
      return defaultValue;
    }
  }

Gets the value in build. gradle:


 manifestPlaceholders = [
        JPUSH_PKGNAME : applicationId,
        JPUSH_APPKEY : "xxxxxxxxxxxxxxxxx", //JPush Corresponding to the package name registered on the appkey.
        JPUSH_CHANNEL : "developer-default", // Just fill in the default value temporarily .
    ]

The principle is one, and the value under the manifestPlaceholders label needs to be displayed in the manifest file AndroidManifest. xml, where the Aurora channel number is occupied (ps: I don't know if the Aurora document is directly obtained):

Here, the name name is random and does not repeat, and value is the value defined under manifestPlaceholders, which must adopt ${variable value}


<meta-data android:name="JPUSH_CHANNEL"
          android:value="${JPUSH_CHANNEL}"/>

Simultaneous method


 /**
   *  Get MetaData Information 
   *
   * @param name
   * @param def
   * @return
   */
  public static String getMetaDataValue(Context context, String name,
                     String def) {
    String value = getMetaDataValue(context, name);
    return (value == null) ? def : value;
  }

  public static String getMetaDataValue(Context context, String name) {
    Object value = null;
    PackageManager packageManager = context.getPackageManager();
    ApplicationInfo applicationInfo;
    try {
      applicationInfo = packageManager.getApplicationInfo(
          context.getPackageName(), PackageManager.GET_META_DATA);
      if (applicationInfo != null && applicationInfo.metaData != null) {
        value = applicationInfo.metaData.get(name);
      }
    } catch (PackageManager.NameNotFoundException e) {
      throw new RuntimeException(
          "Could not read the name in the manifest file.", e);
    }
    if (value == null) {
      throw new RuntimeException("The name '" + name
          + "' is not defined in the manifest file's meta data.");
    }
    return value.toString();
  }

So I went back to step 1 to get the values in meta-data. After all, are you familiar with PackageManager

Supplementary knowledge: android meta data Value Acquisition Pit

When getting meta data, the normal non-pure number string is set in value, and it is normal to get it with getString.

But if it contains a pure numeric string, it is returned as null with getString. Maybe you will think of using getInt, getLong to get it. Sorry, it may also be null.

There are two solutions:

1.

Continue to use value= "1234567890", but add "\" (backslash + space) at the beginning of the pure numeric string so that the system will automatically read it as a string instead of other formats, such as


<meta-data
  android:name="appkey"
  android:value="\ 1234567890" />

2. Use resources attribute to obtain resource id through getInt, and then obtain the corresponding value of resource id. This method has strong scalability and can obtain all resources instead of just string, as follows


<meta-data
  android:name="appkey"
  android:resource="@string/AppKey" />

The value of AppKey defined in strings. xml is

< string name="AppKey" > 1234567890 < /string >

3. Ways to obtain meta resources under Application


public static String getStringMetaData(String name) {
  int valueId = 0;
  try {
    ApplicationInfo appInfo = getApplicationContext().getPackageManager()
        .getApplicationInfo(getApplicationContext().getPackageName(),
            PackageManager.GET_META_DATA);
    valueId = appInfo.metaData.getInt(name);
    if (valueId != 0) {
      return getApplicationContext().getResources().getString(valueId);
    }
  } catch (PackageManager.NameNotFoundException e) {
    e.printStackTrace();
  }
  return "";
}

Related articles: