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

AndroidAndroid Resources

Android Problem Overview


I've searched on Google, but couldn't find any relevant results.

I also checked the official Android docs and this page (for all the available resources) was all I could find. The relevant links (to the res/values/ directory) I found on this page were:

These pages don't tell anything about the res/values/public.xml file.
Here is an example I found for this type of file.

Small snippet

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <public type="attr" name="commentTextColor" id="0xAA010007" />
    <public type="drawable" name="add_icon_bl" id="0xAA020000" />
    <public type="layout" name="act_date_picker" id="0xAA030001" />
    <public type="anim" name="activity_slide_from_bottom" id="0xAA040000" />
    <public type="xml" name="pref_menu" id="0xAA050000" />
    <public type="raw" name="alert_bell_animation_bl" id="0xAA060000" />
    <public type="array" name="taskRepeat" id="0xAA070000" />
    <public type="color" name="theme_main_color_bl" id="0xAA080000" />
    <public type="string" name="no_internet" id="0xAA0a0001" />
    <public type="id" name="page1" id="0xAA0d0015" />
</resources>

As you can see from the type attribute, it contains pretty much all the standard resource types that you normally put in separate directories under the res directory...


Why would one want to misuse the directories that Android provides and use a single file to store all the values in? Can someone give more information about this?

Android Solutions


Solution 1 - 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: https://stackoverflow.com/questions/6517151/how-does-the-mapping-between-android-resources-and-resources-id-work/6646113#6646113

Solution 2 - Android

https://developer.android.com/studio/projects/android-library.html#PrivateResources

public.xml is useful for library projects in general. If you include a public.xml, it's assumed that the rest of your resources are meant to be private.

Although private resources will still be usable by other projects, the linter will warn about using them, and they won't be included in AndroidStudio's autocompletion.

Here's a gist for autogenerating public.xml https://gist.github.com/HannahMitt/99922d4183bdb03356fd339413ba6303

Solution 3 - Android

Is it not a file just for the use authors of the OS code to define a mapping between symbolic names and system resource ids?

You'll find it in your SDK at YOUR_SDK_LOCATION\platforms\android-??\data\res\values.

It's headed

> This file defines the base public resources exported by the platform, > which must always exist

and has the caveat:

> IMPORTANT NOTE FOR ANYONE MODIFYING THIS FILE READ THIS BEFORE YOU > MAKE ANY CHANGES > > This file defines the binary compatibility for resources. As such, > you must be very careful when making changes here, or you will > completely break backwards compatibility with old applications

It has entries such as

<public type="attr" name="smallScreens" id="0x01010284" />
<public type="attr" name="normalScreens" id="0x01010285" />
<public type="attr" name="largeScreens" id="0x01010286" />

in it - all system resurce ids, so anyone changing entries will break their code, insomuch as it won't run on a standard device.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionIgor PopovView Question on Stackoverflow
Solution 1 - AndroidMartin NordholtsView Answer on Stackoverflow
Solution 2 - AndroidHannahMittView Answer on Stackoverflow
Solution 3 - AndroidNickTView Answer on Stackoverflow