7

how to change meta-data value Programmatically in manifest or string resource in string value

Ahmed Wahdan
  • 330
  • 4
  • 15

3 Answers3

7

Using Inject build variables into the manifest you can change metadata value programmatically

In build.gradle

defaultConfig {
        manifestPlaceholders = [ senderid:"12345"]
    }

buildTypes {
        release {
            manifestPlaceholders = [ senderid:"56789"]
        }
    }

In Manifest file

<meta-data
            android:name="SENDER_ID"
              android:value="${senderid}" />

Referance Link : https://developer.android.com/studio/build/manifest-build-variables.html

Kirti
  • 116
  • 1
  • 5
0

You can't modify a resource, when you provide info as a Resource (in the folder res) it can't be modified programmatically, You can't write in that directory. Is the same for Android Manifest, the manifest meta-data can't be changed at runtime.

It's not better to have two strings, like the first value and the second one so you can just use this:

Java:

thing.setText(getResources.getString(R.id.variablename2)) 

Kotlin:

thing.text = resources.getString(R.id.variablename2) 
Dinorah Tovar
  • 488
  • 2
  • 15
  • the problem is I have I have meta-data value on manifest i want to change it – Ahmed Wahdan Sep 17 '18 at 17:19
  • You cannot change the meta-data at runtime but you can set up multiple flavors and set up your build to build appropriately for each flavor of the application. If you are looking for a more robust solution I would recommend setting up a setting service that would allow you to give settings on a per client/organization basis. – Randy Sep 17 '18 at 17:41
-1

Changing metadata can be done by adding metadata to AndroidManifest.xml file. Later you can access it using Bundle from your activity. Here is the example.

AndroidManifest.xml

 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.example.readmetadata"
  android:versionCode="1"
  android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".MainMenu" android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <meta-data android:name="my_api_key" android:value="mykey123" />
    </application>
    <uses-sdk android:minSdkVersion="3" android:targetSdkVersion="8" />
</manifest>

Reading this meta-data takes just a few lines of Java

try {
ApplicationInfo ai = getPackageManager().getApplicationInfo(activity.getPackageName(), PackageManager.GET_META_DATA);
Bundle bundle = ai.metaData;
String myApiKey = bundle.getString("my_api_key");
} catch (NameNotFoundException e) {
Log.e(TAG, "Failed to load meta-data, NameNotFound: " + e.getMessage());
} catch (NullPointerException e) {
Log.e(TAG, "Failed to load meta-data, NullPointer: " + e.getMessage());         
}

Activity extends ContextWrapper which has a getPackageManager() method. That method returns the PackageManager, which is used to fetch the ApplicationInfo, passing the package name and the meta-data flag. The returned ApplicationInfo contains a field, metaData, which is actually a Bundle containing all the meta data. Line 4 fetches a String that is the same as the “android:name” parameter in the XML. Hope it helps!