Android编译的资源-resources.arsc [英] Android compiled resources - resources.arsc

查看:191
本文介绍了Android编译的资源-resources.arsc的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图弄清楚编译资源"的含义.

I am trying to figure out what it mean to "compile resources".

我阅读了许多有关该主题的文章,但没有找到简单的答案. 我读过的最好的书是:如何.

I have read many articles about the subject but didn't find a simple answer. The best one I have read was this: How does the mapping between android resources and resources ID work?.

据我了解,当我们通过ANT(Eclipse)或Gradle(AS)编译项目时. 我们使用名为 aapt 的工具-Android资产打包工具,该工具: 用于为我们的每个资源(例如我们的布局,样式等)生成唯一的ID,并将其存储在查找表中.然后,它通过生成两个文件来持久化此查找表:

From my understanding, when we compile our project either by ANT (Eclipse) or Gradle (AS). We use a tool called aapt - Android Asset Packaging Tool which: Is used to generate unique IDs for each of our resources, such as our layouts, our styles and more and store them in a lookup table. Then it persists this lookup table by generating two files:

  1. 它生成具有这些唯一ID的R.java文件,因此我们将能够在编译期间使用来自Java代码的资源.
  2. 它将生成resources.arsc文件,该文件可以在resources * .ap_文件中找到. 此resources.arsc文件稍后将由apktool打包到apk.
    这种arcc文件格式是一种易于在运行时由设备映射和解析的格式.
  1. It Generates the R.java file with these unique IDs so we will be able to use our resources from our java code during compilation.
  2. It generate the resources.arsc file which can be found in resources*.ap_ file. This resources.arsc file will later be packed by the apktool to the apk.
    This arsc file format is a format that will be easily mapped and parsed by the device at runtime.

一个例子:

因此简单起见:可以说我在activity_main.xml中有此内容:

An Example:

So to make it simple: lets say I have this in my activity_main.xml:

    <TextView android:id="@+id/my_textView"
        android:text="@string/hello_world" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

然后我从onCreate调用它:

And I call it from my onCreate using:

findViewById(R.id.my_textView)

在我的R.java文件中,我将看到:

In my R.java file I will see:

public static final int my_textView=0x7f08003f;

使用:在生成的apk上转储aapt资源,我可以看到它与my_textView包含两行: ec资源0x7f08003f com.example.lizi.liortest2:id/my_textView:标志= 0x00000000 资源0x7f08003f com.example.lizi.liortest2:id/my_textView:t = 0x12 d = 0x00000000(s = 0x0008 r = 0x00)

Using: aapt dump resources on the generated apk I can see it contains two lines with my_textView: ec resource 0x7f08003f com.example.lizi.liortest2:id/my_textView: flags=0x00000000 resource 0x7f08003f com.example.lizi.liortest2:id/my_textView: t=0x12 d=0x00000000 (s=0x0008 r=0x00)

我本以为这个resources.arsc文件不仅会包含资源ID,而且还会包含我为视图定义的所有属性,例如android:layout_width ="wrap_content".

I would have thought that this resources.arsc file will not just contain the resource ID but also all the properties I have defined for the view, such as android:layout_width="wrap_content".

所以现在在运行时虚拟机尝试运行findViewById(R.id.my_textView) 它如何知道要获取哪个视图/要创建其属性?

So now during runtime when the VM tries to run findViewById(R.id.my_textView) How does it know which view to get / its properties to create?

我简直不明白它是如何工作的……此查找表是否还应包含属性数据? 0x7f08003f这个数字是什么? (它代表一个值,该值以后将被映射到将存储对象的物理内存中吗?)

I simply can't understand how it works... Shouldn't this lookup table contain also the properties data? And what is this 0x7f08003f number? (Should it represent a value that will later be mapped to physical memory in which the object will be stored?)

推荐答案

TL; DR :借助android编译器(aapt),xml节点被转换为Java类,并且相应的属性将转换为数字ID. Android运行时使用这些数字ID实例化类并创建视图

TL;DR: With the help of android compiler(aapt), xml nodes get translated to Java classes and the corresponding attributes get translated to numerical Ids. Android run-time works with these numeric ids to instantiate classes and create the views

运行此命令以转储二进制xml
aapt d xmltree apk_file_name res/layout/activity_main.xml( aapt可以在 android-sdk-dir/build-tools/23.0.2/aapt.exe 中找到)

Run this command to dump the binary xml
aapt d xmltree apk_file_name res/layout/activity_main.xml(aapt can be found in android-sdk-dir/build-tools/23.0.2/aapt.exe)

这将显示xml节点(例如LinearLayoutRelativeLayout等)及其属性(例如android:layout_width, android:layout_height)及其值.请注意,可以在其中看到常量match_parent(数值0xffffffff-1)或wrap_content(数值0xfffffffe-2).

This will show the xml nodes (e.g. LinearLayout, RelativeLayout, etc) with their attributes(e.g. android:layout_width, android:layout_height) and their values. Note that, the constants match_parent(numeric value 0xffffffff or -1) or wrap_content(numeric value 0xfffffffe or -2) can be seen there.

事实上,您可以在apk中的任何其他xml文件上使用此命令,例如AndroidManifest.xml or layout files

As a matter of fact, you can use this command on any other xml files in the apk e.g. AndroidManifest.xml or layout files

.apk文件只是一个zip存档,其中包含所有Java类文件(classes.dex),所有已编译的资源文件以及一个名为resources.arsc的文件. 此resource.arsc文件包含有关资源的所有元信息.其中一些是...

The apk file is just a zip archive containing all the java class files(classes.dex), all the compiled resource files and a file named resources.arsc. This resource.arsc file contains all the meta-information about the resources. Some of those are...

  • xml节点(例如LinearLayoutRelativeLayout等),
  • 属性(例如android:layout_width)
  • 资源id.
  • the xml nodes(e.g. LinearLayout, RelativeLayout, etc),
  • the attributes(e.g. android:layout_width),
  • the resource id's.

资源id是指apk文件中的实际资源.这些属性在运行时解析为一个值.解析过程对于任何重定向(与4dp相对的@dimen/...或与"#FFaabbcc"相对的@color/...)来说都是明智的,并且返回可用的值(与color不同地解析dimen值)值).

The resource id's refer to the real resources in the apk-file. The attributes are resolved to a value at runtime. The resolution process is smart about any re-direction (@dimen/... as opposed to 4dp or @color/... as opposed to "#FFaabbcc") and returns a usable value(a dimen value is resolved differently than a color value).

什么是已编译的XML文件: 编译的XML文件就是将资源引用更改为其相应的ids的同一XML文件.例如,引用@string/ok将替换为0x7f000001.此外,android命名空间中的属性更改为它们各自的整数值(例如wrap_content更改为0xfffffffe-2)

Whats a compiled XML file: A compiled XML file is just the same XML file with the resource references changed to their corresponding ids. For example, a reference @string/ok will be replaced by 0x7f000001. Moreover, the attributes from android namespace is changed to their respective integer values(e.g. wrap_contentis changed to 0xfffffffe or -2)

Android如何在运行时解析资源: 方法 inflater.inflate() 解析已编译的xml文件,并通过实例化xml节点来创建视图层次结构.每个xml节点均由一个Java类(例如LinearLayout.java,RelativeLayout.java)实例化.为了实例化,inflater解析编译的xml文件,收集节点的所有属性,并

How Android resolves resources at runtime: The methodinflater.inflate() parses a compiled xml file and creates a view hierarchy by instantiating the xml nodes. Each of the xml nodes is instantiated by a java class(e.g. LinearLayout.java, RelativeLayout.java). To instantiate, the inflater parses the compiled xml file, collects all the attributes of a node and creates a packed structure of type AttributeSet. This AttributeSet is passed to the class constructor. The class constructor has the responsibility of walking the AttributeSet and resolving each of the attribute values.

例如,对于包含RelativeLayout的布局,inflater会将layout_widthlayout_height打包到AttributeSet中,并将其传递给构造函数

For example, for a layout containing RelativeLayout, the inflater will pack layout_width and layout_height into a AttributeSet and pass it to the constructor

RelativeLayout(上下文上下文, AttributeSet attrs,int defStyleAttr,int defStyleRes).

在这种情况下,某些属性由 RelativeLayout.initFromAttributes().其余属性由父实例化后在该视图上调用setId(id)

In this case, some of the attributes are resolved by RelativeLayout.initFromAttributes(). The rest of the attributes are resolved by the parent ViewGroup.initFromAttributes(). The attribute android:id of a view is just another attribute. After inflating, the inflater stores the id of each view by calling setId(id) on that view after instantiation

现在要回答您的问题 R.id是一个Java数组,而R.id.my_textview是该数组中的整数.视图my_textviewid是此整数(以0x7f开头).方法 findViewById() 在该视图层次结构上进行深度优先搜索,以找到相应的视图.

Now to answer your question R.id is a java array and R.id.my_textview is an integer in that array. The id of the view my_textview is this integer(starts with 0x7f). The method findViewById() does a depth-first search on that view hierarchy to find the respective view.

希望这会有所帮助.您在问题中提供的链接已经回答了aapt如何生成ID.

Hope this helps. The link you provided in your question already answers how the ids are generated by aapt.

这是一个很棒的系统,可以管理具有多种变体的设备的资源.而且,实现的速度非常快!以此为基础,它可以实现更高级别的功能(例如运行时资源叠加层)

Its a wonderful system of managing resources for devices with multiple dimensions of variations. Moreover, the implementation is really fast !! With this as the foundation, it allows to implement higher level functionality(e.g. Runtime Resource Overlay)

这篇关于Android编译的资源-resources.arsc的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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