以编程方式获取manifestPlaceholders

Far*_*ABZ 5 android gradle android-gradle-plugin

我想在我的代码中读取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"]
    }
}
}
Run Code Online (Sandbox Code Playgroud)

我在 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();
    }
Run Code Online (Sandbox Code Playgroud)

使用此代码,捆绑包应包含 manifestPlaceholders 值,但它始终为 null。

问题是什么?

dha*_*ram 0

无法以编程方式从类文件 (java/kotlin) 中获取。正如 \xe2\x80\x93\nCommonsWare 在评论中建议的那样,您宁愿使用 buildConfigField 或 resValue 将数据从 Gradle 注入到应用程序中。

\n