我想在我的代码中读取 manifestPlaceholders 的值。
我找到了一些代码,但它们总是返回 null。
我的等级是:
android {
compileSdkVersion 29
buildToolsVersion "29.0.2"
defaultConfig {
applicationId "com.some.test"
minSdkVersion 19
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
manifestPlaceholders = [app_id: "xxx123"]
}
debug {
manifestPlaceholders = [app_id: "xxx123"]
}
}
}
我在 Onesignal SDK 中发现这段代码可以从 gradle 中读取值,但它不起作用:
try {
ApplicationInfo ai = context.getPackageManager().getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);
Bundle bundle = ai.metaData;
appId = bundle.getString("app_id");
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
使用此代码,包应包含 manifestPlaceholders 值,但它始终为空。
问题是什么?
最佳答案
无法以编程方式从您的类文件 (java/kotlin) 中获取。作为 - CommonsWare 在评论中建议您宁愿使用 buildConfigField 或 resValue 将数据从 Gradle 注入(inject)应用程序。
关于android - 以编程方式获取 manifestPlaceholders,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58451826/