Skip to content Skip to sidebar Skip to footer

How To Write Meta-data Info To Android Manifest At Runtime

I know its possible to edit a android manifest component, for example, set it enabled/disabled etc. I'd like to insert meta value tag into the application tag of the android manife

Solution 1:

You can't edit the manifest at runtime.

If you need runtime solution, you should try other ways like share preference or something can save value persist.

If not just put it in manifest, take google play service for Example:

<application><meta-dataandroid:name="com.google.android.gms.version"android:value="@integer/google_play_services_version" />
    .....
</application>

Solution 2:

I think its late, but will help for any other searching for this answer

first Go to res > values > strings.xml and create the string resource

 <string name="my_api_key">Your_api_key</string>

and After that Go to manifest file

Be sure you want to add this meta tag in your application or activity if You want to add inside the application then just start after the application tag or if activity the after the activity tag on which you want to write

   <meta-data
            android:name="apikey"
            android:value="@string/my_api_key" />

Now finally its time to retrieve the meta data inserted in the application or the activity,

try {
        ActivityInfo ai = getPackageManager().getActivityInfo(this.getComponentName(), PackageManager.GET_META_DATA);
        Bundle bundle = ai.metaData;
        if (bundle != null) {
            String apiKey = bundle.getString("apikey");
            Log.d(this.getClass().getSimpleName(), "apiKey = " + apiKey);

            }
        }
    } catch (PackageManager.NameNotFoundException e) {
        Utilities.log(this.getClass().getSimpleName(), "Failed to load meta-data, NameNotFound: " + e.getMessage());
    } catch (NullPointerException e) {
        Log.e(this.getClass().getSimpleName(), "Failed to load meta-data, NullPointer: " + e.getMessage());
    }

if you have put the metatag in the application then use

ApplicationInfoai= getPackageManager().getApplicationInfo(this.getPackageName(), PackageManager.GET_META_DATA); 

instead of Activity info , thats all.

Post a Comment for "How To Write Meta-data Info To Android Manifest At Runtime"