What Is the Use of the Res/Values/Public.Xml File on Android

What is the use of the res/values/public.xml file on Android?

The file res/values/public.xml is used to assign fixed resource IDs to Android resources.

Consider these set of string resources in res/values/strings.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="string1">String 1</string>
<string name="string3">String 3</string>
</resources>

The Android Asset Packaging Tool (aapt) might assign the following resource IDs for these resources when the app is compiled:

public final class R {
// ...
public static final class string {
public static final int string1=0x7f040000;
public static final int string3=0x7f040001;
}
}

Now, change the set of string resources to

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="string1">String 1</string>
<string name="string2">String 2</string>
<string name="string3">String 3</string>
</resources>

and you'll notice that the resource ID for @string/string3 has changed:

public final class R {
// ...
public static final class string {
public static final int string1=0x7f040000;
public static final int string2=0x7f040001;
public static final int string3=0x7f040002; // New ID! Was 0x7f040001
}
}

To prevent this, you can use res/values/public.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<public type="string" name="string3" id="0x7f040001" />
</resources>

which will result in the resource IDs being assigned as follows:

public final class R {
// ...
public static final class string {
public static final int string1=0x7f040000;
public static final int string2=0x7f040002;
public static final int string3=0x7f040001; // Resource ID from public.xml
}
}

Applications rarely have any use for res/values/public.xml since the resource IDs assigned to resources does not matter. When they change, the entire application is rebuilt anyway so any references in Java code to resources by resource ID will be updated.

The most significant user of res/values/public.xml is the Android platform itself. Applications built against old versions of Android assumes that certain resource have a certain resource ID. For example, the Android resource @android:style/Theme must always have the resource ID 0x01030005 for the platform to be backwards compatible with apps built against old versions of the platform.

If you are curious about more details on how resource IDs are assigned, please refer to this answer: How does the mapping between android resources and resources ID work?

AndroidManifest.xml use integer ids stored in public.xml for themes

Referring to this post, I believe the use of @style/myCoolTheme is correct because you are using the name="myCoolTheme" of the public.xml.

The only purpose of the id="" attribute is to merge that integer value into R.java, not be referenced by the other XML files.

How to add id to public.xml?

Create a new xml ids.xml in values with all the id values which are to be constant.

<resources>
<item type="id" name="index_row_search" />
</resources>

I think its better if you copy the resource ids to public.xml from the R.java, the first time to avoid errors like entry index is larger than available symbols and also to keep the type of resource to be consistent.

Defining Strings in res/values/string.xml?

  • So you can easily translate them into different languages.
  • So they're nicely organized and you have them all in one place.

Android How to add a custom xml file in res/values and how to register the customvalues.xml with the system

Update: This doesn't seem to work anymore (but it used to) and you cannot create your own custom types in android. Only the standard available resource types work now.

Therefore, the only way to achieve something like this would be, have your separate file as suggested, url.xml and have all yours URLs in that file so that it doesn't get mixed up with the other Strings. It just improves readability and maintainability, AFAIK.

The URL item now looks like,

<item name="myUrl" type="string">http://myUrl.com</item>

And you needs to be accessed the usual way:

String myurl = getResources().getString(R.string.myUrl);

Original Answer:

Try something like this:-

url.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<item name="myUrl" type="urls">http://myUrl.com</item>
</resources>

And in your activity, get it like this:-

String s = getResources().getString(R.urls.myUrl);

Note:- You needn't register your xml anywhere. Just make sure its available in res/values folder.

Snapshot:-

How I used

How does the mapping between android resources and resources ID work?

At build time, the aapt tool collects all of the resources you have defined (though separate files or explicit definitions in files) and assigns resource IDs to them.

A resource ID is a 32 bit number of the form: PPTTNNNN. PP is the package the resource is for; TT is the type of the resource; NNNN is the name of the resource in that type. For applications resources, PP is always 0x7f.

The TT and NNNN values are assigned by aapt arbitrarily -- basically for each new type the next available number is assigned and used (starting with 1); likewise for each new name in a type, the next available number is assigned and used (starting with 1).

So if we have these resource files handled by aapt in this order:

layout/main.xml
drawable/icon.xml
layout/listitem.xml

The first type we see is "layout" so that is given TT == 1. The first name under that type is "main" so that is given NNNN == 1. The final resource ID is 0x7f010001.

Next we see "drawable" so that is given TT == 2. The first name for that type is "icon" so that gets NNNN == 1. The final resource ID is 0x7f020001.

Last we see another "layout" which has TT == 1 as before. This has a new name "listitem" so that gets the next value NNNN == 2. The final resource ID is 0x7f010002.

Note that aapt by default makes no attempt to keep these identifiers the same between builds. Each time the resources change, they can all get new identifiers. Each time they are built, a new R.java is created with the current identifiers so your code gets the correct values. Because of this, you must never persist resource identifiers anywhere where they can be used across different builds of your app.

Once the resources are compiled and identifiers assigned, aapt generates the R.java file for your source code and a binary file called "resources.arsc" that contains all of the resource names, identifiers, and values (for resources that come from separate file, their value is the path to that file in the .apk), in a format that can easily mmapped and parsed on the device at runtime.

You can get a summary of the resources.arsc file in an apk with the command "aapt dump resources <path-to-apk>".

The format of the binary resource table is documented in the header file for the resource data structures here:

https://github.com/android/platform_frameworks_base/blob/master/libs/androidfw/include/androidfw/ResourceTypes.h

The full implementation for reading the resource table on the device is here:

https://github.com/android/platform_frameworks_base/blob/master/libs/androidfw/ResourceTypes.cpp



Related Topics



Leave a reply



Submit