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

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

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

  • string资源
  • 风格的资源
  • 更多资源

这些页面不会告诉res/values/public.xml文件。
这是我为这种types的文件find的一个例子

小片段

 <?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属性中可以看到,它几乎包含了所有标准资源types ,通常放在res目录下的不同目录中。


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

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

考虑res / values / strings.xml中的这些string资源集合:

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

现在,将string资源的集合更改为

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

应用程序很less使用res / values / public.xml,因为分配给资源的资源ID无关紧要。 当它们改变的时候,整个应用程序都会被重build,所以Java代码中的任何资源引用都会被更新。

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

如果您对如何分配资源ID有更多的了解,请参考以下答案: Android资源和资源ID之间的映射是如何工作的?

它是不是一个文件仅用于操作系统代码的作者来定义符号名称和系统资源ID之间的映射?

您可以在YOUR_SDK_LOCATION \ platforms \ android – ?? \ data \ res \ values的SDK中find它。

它正在走向

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

并有警告:

任何人修改此文件的重要注意事项在您做出任何更改之前请仔细阅读

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

它有条目,如

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

在这个系统中,所有的系统都会重启ID,所以任何改变条目的人都会破坏他们的代码,因为它不会在标准的设备上运行。

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

一般而言,public.xml对图书馆项目很有用。 如果你包含一个public.xml,那么假定你的其他资源是私有的。

虽然私人资源仍然可以被其他项目使用,但是Linter会警告使用它们,而不会被包含在AndroidStudio的自动完成中。

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