一篇关于meta-data的转载记录
-
application下
<application ... >
<meta-data android:name="TEST_KEY" android:value="${test_key}"/>
</application>
try {
ApplicationInfo info = getPackageManager().getApplicationInfo(getPackageName(), PackageManager.GET_META_DATA);
Log.e("info", info.metaData.getString("TEST_KEY"));
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
-
activity下
<activity ... >
<meta-data android:name="TEST_KEY" android:value="${test_key}"/>
</activity>
try {
ActivityInfo info = getPackageManager().getActivityInfo(getComponentName(), PackageManager.GET_META_DATA);
Log.e("info", info.metaData.getString("TEST_KEY"));
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
-
service下
<service ... >
<meta-data android:name="TEST_KEY" android:value="${test_key}"/>
</service>
try {
ServiceInfo info = getPackageManager().getServiceInfo(getComponentName(), PackageManager.GET_META_DATA);
Log.e("info", info.metaData.getString("TEST_KEY"));
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
-
receiver下
<receiver ... >
<meta-data android:name="TEST_KEY" android:value="${test_key}"/>
</receiver>
try {
ActivityInfo info = getPackageManager().getReceiverInfo(getComponentName(), PackageManager.GET_META_DATA);
Log.e("info", info.metaData.getString("TEST_KEY"));
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
manifestPlaceholders使用
在build.gradle的多版本中设置
manifestPlaceholders = ["test_key": "修改的内容"]
好比如下
productFlavors{
dev{
manifestPlaceholders = ["test_key": "测试版key"]
}
free{
applicationIdSuffix ".free"
manifestPlaceholders = ["test_key": "Free版key"]
}
}
效果如下
测试 Free
资料
Android获取Manifest中<meta-data>元素的值
使用 gradle 在编译时动态设置 Android resValue / BuildConfig / Manifes中<meta-data>变量的值
网友评论