Programmatically get manifestPlaceholders

Viewed 1505

I want to read manifestPlaceholders' values in my code.
I found some code but they always return null.

My gradle is:

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"]
    }
}
}

I found this code in Onesignal SDK to read values from gradle but it doesn't work:

    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();
    }

With this code, the bundle should contain manifestPlaceholders values, but it is always null.

what is the problem?

1 Answers

There is no way to get programmatically from your class file (java/kotlin). As – CommonsWare suggested in comment you rather use buildConfigField or resValue to inject data into the app from Gradle.

Related