如何Android的资源和资源ID工作之间的映射? [英] How does the mapping between android resources and resources ID work?

查看:473
本文介绍了如何Android的资源和资源ID工作之间的映射?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是不可思议的为Android刚刚通过的 R.id.XXX 找到合适的资源。

It is magical for Android to locate the proper resource just through the R.id.XXX.

AFAIK,资源被编译为二进制格式,那么,如何在引擎盖下这个映射逻辑的工作?

AFAIK, the resources are compiled to binary format, so how does this mapping logic work under the hood?

也许它的工作原理是这样的:

Maybe it works like this:

有关,例如,在 layout1.xml ,我们得到了:

For e.g., in the layout1.xml, we got:

<Button android:id="@+id/button1" >

和AAPT会产生这种在R.java:

and AAPT will generate this in the R.java:

public static final int button1=0x7f05000b;

*。APK 是genrated,在 @ + ID /按钮1 与被substituded0x7f05000b。

When the *.apk is genrated, the @+id/button1 with be substituded with "0x7f05000b".

因此​​,当我们调用:

Thus, when we call:

findViewById(R.id.button1);

我们基本上仍然执行搜索基于ID,虽然ID是一个数字像0x7f05000b

we are essentially still do the search based on the ID, though the ID is a number like 0x7f05000b.

谢谢!

我真正想知道的,是资源的id整数是如何解析成资源内容?换句话说,如何在Android运行时查找资源,内容资源ID作为唯一的线索?

例如,如何是绘制图片发现了一个资源ID?要不怎么是一个字符串值,发现有一个资源ID?

For example, how is a drawable picture found with a resource id? Or how is a string value is found with a resource id?

推荐答案

在构建时,该AAPT工具收集所有已定义的资源(虽然单独的文件或文件中明确定义)并分配资源ID给他们。

At build time, the aapt tool collects all of the resources you have defined (though separate files or explicit definitions in files) and assigns resource IDs to them.

一个资源ID的形式是一个32位的数字:PPTTNNNN。聚丙烯是包的资源为; TT是资源的类型; NNNN是在该类型的资源的名称。对于应用程序资源,PP总是0x7f的。

A resource ID is a 32 bit number of the form: PPTTNNNN. PP is the package the resource is for; TT is the type of the resource; NNNN is the name of the resource in that type. For applications resources, PP is always 0x7f.

在TT和NNNN值由AAPT分配任意 - 基本上每一个新类型的下一个可用的号码被分配和使用(从1开始);同样地用于在类型中的每个新的名称,下一个可用的号码被分配和使用(从1开始)。

The TT and NNNN values are assigned by aapt arbitrarily -- basically for each new type the next available number is assigned and used (starting with 1); likewise for each new name in a type, the next available number is assigned and used (starting with 1).

因此​​,如果我们通过AAPT按此顺序处理这些资源文件:

So if we have these resource files handled by aapt in this order:

layout/main.xml
drawable/icon.xml
layout/listitem.xml

我们看到的第一个类型是布局,以便给予TT == 1,在该类型下的第一个名字是主,使被赋予NNNN == 1,最终资源ID为0x7f010001。

The first type we see is "layout" so that is given TT == 1. The first name under that type is "main" so that is given NNNN == 1. The final resource ID is 0x7f010001.

接下来,我们看到绘制,使被赋予TT == 2.该类型的第一个名字是图标,这样得到NNNN == 1,最终资源ID为0x7f020001。

Next we see "drawable" so that is given TT == 2. The first name for that type is "icon" so that gets NNNN == 1. The final resource ID is 0x7f020001.

最后,我们看到另外一个布局,它有TT == 1和以前一样。这有一个新的名称列表项,以便获取下一个值NNNN == 2,最终资源ID为0x7f010002。

Last we see another "layout" which has TT == 1 as before. This has a new name "listitem" so that gets the next value NNNN == 2. The final resource ID is 0x7f010002.

需要注意的是AAPT在默认情况下并没有试图保留这些标识符生成的一样。每次资源发生变化,他们都可以得到新的标识符。每次他们都建立一个新的R.java创建与当前标识符使您code得到正确的价值观。正因为如此,你绝不能持久资源标识符的任何地方,他们可以在不同的使用建立你的应用程序中。

Note that aapt by default makes no attempt to keep these identifiers the same between builds. Each time the resources change, they can all get new identifiers. Each time they are built, a new R.java is created with the current identifiers so your code gets the correct values. Because of this, you must never persistent resource identifiers anywhere where they can be used across different builds of your app.

一旦这些资源被编译并分配的标识,AAPT生成R.java文件源$ C ​​$ c和所谓的resources.arsc的二进制文件,其中包含的所有资源名称,标识,和值(为即来自不同的文件资源,其价值是路径中的.apk该文件),在可以轻松mmapped和解析的设备在运行时的格式。

Once the resources are compiled and identifiers assigned, aapt generates the R.java file for your source code and a binary file called "resources.arsc" that contains all of the resource names, identifiers, and values (for resources that come from separate file, their value is the path to that file in the .apk), in a format that can easily mmapped and parsed on the device at runtime.

您可以得到resources.arsc文件的摘要与命令的APKAAPT转储资源&LT;路径对APK&gt;中。

You can get a summary of the resources.arsc file in an apk with the command "aapt dump resources <path-to-apk>".

二进制资源表的格式被记录为资源数据结构这里在头文件:

The format of the binary resource table is documented in the header file for the resource data structures here:

<一个href="https://github.com/android/platform_frameworks_base/blob/master/include/androidfw/ResourceTypes.h">https://github.com/android/platform_frameworks_base/blob/master/include/androidfw/ResourceTypes.h

全面落实读取资源表中的设备是在这里:

The full implementation for reading the resource table on the device is here:

<一个href="https://github.com/android/platform_frameworks_base/blob/master/libs/androidfw/ResourceTypes.cpp">https://github.com/android/platform_frameworks_base/blob/master/libs/androidfw/ResourceTypes.cpp

这篇关于如何Android的资源和资源ID工作之间的映射?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆