繁体   English   中英

以编程方式获取 manifestPlaceholders

[英]Programmatically get manifestPlaceholders

我想在我的代码中读取 manifestPlaceholders 的值。
我找到了一些代码,但它们总是返回 null。

我的 gradle 是:

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 值,但始终为 null。

问题是什么?

尝试:

    BuildConfig.APPLICATION_ID;
    BuildConfig.VERSION_CODE;
    BuildConfig.VERSION_NAME;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM