1.build.gradle:设置
manifestPlaceholders = [var:value,]
2.AndroidManifest.xml在application标签下写meta-data标签使用value引用:
<meta-data>
android:name="var"
android:value="{$value}"
</meta-data>
3.代码中读取manifest文件meta-data值
//不同的类型要区别获取,以下是布尔类型的
public static boolean getAppMetaDataBoolean(Context context, String metaName, boolean defaultValue) {
try {
//application标签下用getApplicationinfo,如果是activity下的用getActivityInfo
boolean value = context.getPackageManager()
.getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA)
.metaData.getBoolean(metaName, defaultValue);
L.d("meta-data", metaName + " = " + value);
return value;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
return defaultValue;
}
}
网友评论