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... 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
    – MM.
    Apr 25, 2012 at 8:24

5 Answers 5

69

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")
6
  • 8
    Huh. A downvote with no comment. That doesn't help! Please tell me what is wrong with my answer, if you ever come back!
    – Simon
    Nov 1, 2012 at 18:07
  • stackoverflow.com/questions/17972963/… can you suggest me Jul 31, 2013 at 14:26
  • It appears that you can't add <meta-data> to an <application> tag. Here's the relevant doc: developer.android.com/guide/topics/manifest/…
    – user128536
    Oct 9, 2013 at 23:28
  • @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.
    – Simon
    Oct 10, 2013 at 12:54
  • 3
    OK, 2 downvotes now on a perfectly good answer. I'm not in this for the points, and I really don't care about losing 2 of them, I've got thousands, so go ahead and downvote if you wish but PLEASE have the goodness to tell me why.
    – Simon
    Mar 19, 2014 at 16:03
3

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

0

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

6
  • 1
    this is completely unrelated.
    – njzk2
    Apr 25, 2012 at 8:21
  • Not completely. I was going for other solutions that could fit in this situation. Apr 25, 2012 at 8:24
  • 1
    tags in the manifest are added at compile time and accessed at runtime. sharedpreferences are created and accessed at runtime.
    – njzk2
    Apr 25, 2012 at 8:26
  • @njzk2 manifest is accessed even before runtime of your app to e.g. install the app.
    – zapl
    Apr 25, 2012 at 8:27
  • 1
    @zapl : by the system, yes. but you can access it during the runtime of your app
    – njzk2
    Apr 25, 2012 at 8:29
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.

-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

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.