如题,在AndroidManifest里面的meta-data里面配置:
<application>
<meta-data
android:name="TTAPPID"
android:value="APPID123" />
</application>
在代码里面可以获取value的值:
public static String getMetaDataValue(Context ctx, String name) {
Object value = null;
PackageManager packageManager = ctx.getPackageManager();
ApplicationInfo applicationInfo;
try {
applicationInfo = packageManager.getApplicationInfo(ctx.getPackageName(), PackageManager.GET_META_DATA);
if (applicationInfo != null && applicationInfo.metaData != null) {
value = applicationInfo.metaData.get(name);
}
} catch (Exception e) {
}
if (value == null) {
value = new String();
}
return value.toString();
}
在Android Gradle manifestPlaceholders自定义变量取值: 如图,在Gradle里面配置了一个TTAPPID占位符,那么在AndroidManifest里面就可以这样写了:
<application>
<meta-data
android:name="TTAPPID"
android:value="${TTAPPID}" />
</application>
在多渠道打包,不同的渠道就可以更改这个值了:
productFlavors {
google {
manifestPlaceholders.put("UMENG_CHANNEL","google")
}
baidu {
manifestPlaceholders.put("UMENG_CHANNEL","baidu")
}
}
|