32

I would like to add a custom attribute to the application tag of my AndroidManifest.xml file. Is this possible in the Android environment?

2
  • i guess not ...but can you elaborate this... Commented Apr 25, 2012 at 8:22
  • Sure, my motivations are here: stackoverflow.com/q/10311504/183123. I would like to have an application register with a service I am developing. The target application need not be running Commented Apr 25, 2012 at 8:24

5 Answers 5

70

Yes. Here's an example. The custom tag is ContentVersion.

<application android:name=".MyApplication"
             android:icon="@drawable/icon"
             android:label="@string/app_name">

    <meta-data android:name="ContentVersion" android:value="1.9" />

    <activity android:name="com.someone.something.MainActivity"
              android:theme="@android:style/Theme.Translucent.NoTitleBar"
              android:screenOrientation="sensor"
              android:label="@string/app_name">

To access it:

    ApplicationInfo ai = _context.getPackageManager().getApplicationInfo(_context.getPackageName(),PackageManager.GET_META_DATA);
    ai.metaData.get("ContentVersion")
Sign up to request clarification or add additional context in comments.

4 Comments

It appears that you can't add <meta-data> to an <application> tag. Here's the relevant doc: developer.android.com/guide/topics/manifest/…
@JoeBowers Thanks for that. Interesting! However, it does work, and continues to work in published apps so presumably under all later versions. I will test again though.
@JoeBowers The documentation seems to be wrong. If I put that tag in a <service> it doesn't work (metaData is null), but when it is in <application> it works fine.
3

You cannot define custom attribute to a predefined tag, but you can add key-value pairs called meta-data.

Comments

0

You could go for a SharedPreferences, instead (aka, settings).

6 Comments

this is completely unrelated.
Not completely. I was going for other solutions that could fit in this situation.
tags in the manifest are added at compile time and accessed at runtime. sharedpreferences are created and accessed at runtime.
@njzk2 manifest is accessed even before runtime of your app to e.g. install the app.
@zapl : by the system, yes. but you can access it during the runtime of your app
|
0

In the tag (as well as service and receiver), you can use the tag (http://developer.android.com/guide/topics/manifest/meta-data-element.html )

It contains a name and a value or a resource ID.

You retrieve it through the PackageManager.

Comments

-2

If anybody needs that for Xamarin (Mono for Android) I couldn't find the constant, but i found the value for it, which is 128.

I used a "for" condition to go through all values from 0 to 1000 and check whenever the MetaData property was not null. lol

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.