Android 上的 res/values/public.xml 文件有什么用?

发布于 2025-01-07 17:51:49 字数 1936 浏览 0 评论 0原文

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

我还检查了官方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 提供的目录并使用单个文件来存储所有值在?有人可以提供更多相关信息吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

巷子口的你 2025-01-14 17:51:49

文件 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>

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

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之间的映射是如何工作的?

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?

辞慾 2025-01-14 17:51:49

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

public.xml 对于一般的库项目很有用。如果您包含 public.xml,则假定您的其余资源是私有的。

尽管私有资源仍然可以被其他项目使用,但 linter 会在使用它们时发出警告,并且它们不会包含在 AndroidStudio 的自动补全中。

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

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

迷雾森÷林ヴ 2025-01-14 17:51:49

难道它不是一个仅供操作系统代码的使用作者定义符号名称和系统资源 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 之类的条目,因此任何更改条目的人都会破坏他们的代码,因为它不会在标准设备上运行。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文