【问题标题】:What is the use of the res/values/public.xml file on Android?Android 上 res/values/public.xml 文件有什么用?
【发布时间】:2012-02-19 11:05:39
【问题描述】:

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

我还查看了官方 Android 文档,我只能找到 this page(所有可用资源)。我在此页面上找到的相关链接(到 res/values/ 目录)是:

这些页面没有提供任何关于 res/values/public.xml 文件的信息。
这是我为this type of file 找到的示例

小sn-p

<?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 提供的目录并使用单个文件来存储所有值?有人可以提供更多信息吗?

【问题讨论】:

    标签: android android-resources


    【解决方案1】:

    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 的更多细节感到好奇,请参考这个答案:How does the mapping between android resources and resources ID work?

    【讨论】:

    • 是否可以在我自己的项目中定义公共资源ID,我试过但失败了? @IgorPopov
    • 在 2015 年之前,可以在自己的项目中使用 public.xml 来修复资源 ID。2015 年,谷歌已经(像往常一样)在 gradle 中破坏了这个功能并拒绝修复它。这是相应问题的链接:link
    【解决方案2】:

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

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

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

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

    【讨论】:

      【解决方案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,因此任何更改条目的人都会破坏他们的代码,因为它不会在标准设备上运行。

      【讨论】:

      • 我不知道该文件是 Android SDK 的一部分...无论如何,我发现的示例不会更改现有值,它只是创建其他自定义值...所以我不要认为您发布的限制适用于这种情况。
      • 我想它一定是给想要开发自定义OS ROM的人使用的。如果您添加了一个新的系统资源名称/ID,那么您可以编译应用程序代码和 ROM 代码来使用它。不过,该应用程序只能在您的自定义 ROM 手机上运行。
      • 不,我在普通应用程序中找到了这个,而不是在自定义 ROM 中。
      猜你喜欢
      • 2014-08-30
      • 1970-01-01
      • 2013-06-01
      • 1970-01-01
      • 2017-08-01
      • 1970-01-01
      • 2023-04-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多