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?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

http://stackoverflow.com/questions/9348614/what-is-the-use-of-the-res-values-public-xml-file-on-android


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

I also checkedthe official Android docsandthis page(for all the available resources) was all I could find. Therelevant links(to theres/values/directory) I found on this page were:

These pages don't tell anything about theres/values/public.xmlfile.
Here is anexampleI found forthis 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 thetypeattribute,it contains pretty muchall the standard resource typesthat you normally put inseparate directoriesunder theresdirectory...


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?

share | improve this question
add comment

2 Answers

up vote 37 down vote accepted

The fileres/values/public.xmlis used to assign fixed resource IDs to Android resources.

Consider these set of string resources inres/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/string3has 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 useres/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 forres/values/public.xmlsince 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 ofres/values/public.xmlis 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/Thememust 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?

| improve this answer
+1'd && accepted. Thank you very much for explaining me in such detail what I asked :).Igor Popov Feb 20 '12 at 18:53
add comment

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.

| improve this answer
I didn't know about the file being part of the Android SDK... Anyway,the example I found doesn't change existing values,it just creates other,custom ones... so I don't think the restriction you posted applies in this case.Feb 19 '12 at 11:52
I guess that it must be for the use of people who want to develop custom OS ROMs. If you added a new system resource name/id,then you could compile the app code and ROM code to make use of it. The app would only run on your custom ROM phone though.NickT Feb 19 '12 at 12:01
No,I found this in a normal app,not in a custom ROM.Feb 19 '12 at 12:02
原文链接:https://www.f2er.com/xml/298977.html

猜你在找的XML相关文章