62

我在 Google 上搜索过,但找不到任何相关结果。

我还检查了官方的 Android 文档这个页面(所有可用资源)是我能找到的。我在此页面上找到的相关链接(指向目录)是:res/values/

这些页面不告诉任何有关该res/values/public.xml文件的信息。
这是我为此类文件找到的示例

小片段

<?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>

type属性中可以看出,它几乎包含了您通常放在目录下的单独目录中的所有标准资源类型......res


为什么要滥用 Android 提供的目录并使用单个文件来存储所有值?有人可以提供更多有关此的信息吗?

4

3 回答 3

94

文件res/values/public.xml用于为 Android 资源分配固定的资源 ID。

考虑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>

编译应用时,Android 资产打包工具 (aapt) 可能会为这些资源分配以下资源 ID:

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

现在,将字符串资源集更改为

<?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>

您会注意到资源 ID@string/string3已更改:

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
    }
}

为了防止这种情况,您可以使用res/values/public.xml

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

这将导致资源 ID 被分配如下:

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
    }
}

应用程序很少使用res/values/public.xml,因为分配给资源的资源 ID 无关紧要。当它们发生变化时,无论如何都会重新构建整个应用程序,因此 Java 代码中通过资源 ID 对资源的任何引用都将被更新。

res/values/public.xml最重要的用户是 Android 平台本身。针对旧版本 Android 构建的应用程序假定某些资源具有某个资源 ID。例如,Android 资源@android:style/Theme必须始终具有资源 ID 0x01030005,以便平台向后兼容针对旧版本平台构建的应用程序。

如果您对资源ID如何分配的更多细节感到好奇,请参考这个答案:android资源和资源ID之间的映射如何工作?

于 2012-02-20T07:57:03.887 回答
9

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

public.xml 通常对图书馆项目很有用。如果您包含 public.xml,则假定您的其余资源是私有的。

尽管私有资源仍可供其他项目使用,但 linter 会警告使用它们,并且它们不会包含在 AndroidStudio 的自动完成中。

这是自动生成 public.xml 的要点 https://gist.github.com/HannahMitt/99922d4183bdb03356fd339413ba6303

于 2017-01-10T22:14:30.370 回答
3

它不只是供操作系统代码的使用作者定义符号名称和系统资源ID之间的映射的文件吗?

您可以在您的 SDK 中的 YOUR_SDK_LOCATION\platforms\android-??\data\res\values 中找到它。

它的头

该文件定义了平台导出的基础公共资源,必须始终存在

并有一个警告:

修改此文件的任何人的重要说明 在您进行任何更改之前阅读此文件

此文件定义资源的二进制兼容性。因此,在此处进行更改时必须非常小心,否则将完全破坏与旧应用程序的向后兼容性

它有诸如

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

在其中 - 所有系统资源 ID,因此任何更改条目的人都会破坏他们的代码,因为它不会在标准设备上运行。

于 2012-02-19T11:32:32.233 回答