Interpretation of the widget framework Class file structure of the file Dex (a)

Ali P7 mobile Internet architect, Advanced Video (Daily update) for free learning, please click: https://space.bilibili.com/474380680

Class file

Class file is a Java virtual machine definition and it recognizes the file format, generally speaking, each corresponding to a class or an interface Class file.

Class File Format

  • Class file is a set of 8-bit binary stream in units of a byte basis.
  • No delimiter between data items
  • Class file structure format using only two data structures: unsigned and tables.
    • Unsigned number: In u1, u2 like represent unsigned 2-byte, unsigned number can be used to describe the figures, reference index, number of values, string values.
    • Table: The unsigned or more tables, usually end _info, the entire Class document is a table.

Class file composed of:

 
8149969-99fc4fbc30b44e29.png
 
  • Sum: Class file begins magic number is four bytes, the file can be used to verify that the virtual machines
  • Version number: four byte magic number is the major version number and minor version Class file number
  • The constant pool: behind the version number is followed by a constant pool section. Java with different constant pool, which is a start counting, silly constant number table is the number of minus a constant. The first item 0 is reserved to indicate the current Class file does not need any references a constant pool. Constant pool constants comprise two main categories:
    • Literal: including text string is declared as final constant value, and the like.
    • Symbol references: classes and interfaces including naming authority, and a descriptor name, a method name and descriptor fields
  • Access flags: After the constant pool is part of the access flag section, which includes information: is a class or an interface, whether defined as public, whether final, whether abstract information.
  • After the access flag is an index type, parent index, the index set the interface portion, the main portion of the information: the class index information, the index information of the parent class, interface information corresponding to the index number and the implementation of the interface. (By pointing to a type of class descriptor CONSTANT_Class_info constants to find the information corresponding to the constant pool)
  • Field of the table: The following is a part of the variable information described class declaration, including variable scope, whether static, it is the final information.
  • 方法表:方法表则是用于描述类中方法的信息,与字段表类似。
  • 属性表:属性表在字段表和方法表中都会出现,主要用于描述某些场景特有的信息。比如方法是否抛出异常,被final声明的变量的值,内部类列表等信息。

Dex文件

Dex是Android平台上(Dalvik虚拟机,art虚拟机)的可执行文件,每个APK压缩包中都包含一个(或者多个MultiDex)Dex文件,Dex文件中包含了app的所有源码。

Dex文件的结构

  • Dex文件是一组以8位字节为基础单位的的二进制流。

  • Dex文件的各数据项目之间也没有任何分隔符

  • Dex文件由文件头,索引区,数据区三个部分组成

     
    8149969-88024f41212eac38.png
     
  • 其各个元素的解释如下:

    • header:dex文件头部,记录整个dex文件的相关属性
    • string_ids:字符串数据索引,记录了每个字符串在数据区的偏移量
    • type_ids:类型数据索引,记录了每个类型的字符串索引
    • proto_ids:原型数据索引,记录了方法声明的字符串,返回类型字符串,参数列表
    • field_ids:字段数据索引,记录了所属类,类型以及方法名
    • method_ids:类方法索引,记录方法所属类名,方法声明以及方法名等信息
    • class_defs:类定义数据索引,记录指定类各类信息,包括接口,超类,类数据偏移量
    • data:数据区,保存了各个类的真实数据
    • link_data:连接数据区

header

文件头记录了dex文件的一些基本信息, 以及大致的数据分布。其各字段及解释如下:

 
8149969-bb5d9f9412a298c6.png
 
 
8149969-7080fa1a1fe60730.png
 

索引区

索引区中索引了整个dex中的字符串、类型、方法声明、字段以及方法的信息, 其结构体的开始位置和个数均来自dex文件头中的记录

  • 字符串索引区:描述dex文件中所有的字符串信息
  • 类型索引区:描述dex文件中所有的类型, 如类类型、基本类型、返回值类型等
  • 方法声明索引区:描述dex文件中所有的方法声明
  • 字段索引区:描述dex文件中所有的字段声明, 这个结构中的数据全部都是索引值, 指明了字段所在的类、字段的类型以及字段名称
  • Method Index area: Dex file description of all methods, where the method indicates the class declaring the method and the method name

Data Area

The final index area data offset and the offset in the file header points to the data area are described in, further comprising the about to be resolved class_def_item

  • class_def_item: This structure is pointed to by the dex file header classDefsSize and classDefsOff, describe all class definition information Dex files, each DexClassDef contains a DexClassData structure (classDataOff), each DexClassData contains data of a Class of, Class data contains all the methods, the method includes all the instructions in the method

Class files and file comparison Dex

    • Class JVM file is executable file, and the file is Dex Android virtual machine (Dalvik, art) executable file
    • Class files corresponding to each class in Java, and contains a file Dex APK entire JAVA code in the file, the file can be greatly reduced so that the volume of Dex, reduce data redundancy Class files facilitate the operation of the mobile terminal. (Class may be several files in the SDK into the dx command Dex file)
      Original link: https://www.jianshu.com/p/a9e12b1c0079
      Ali P7 mobile Internet architect, Advanced Video (Daily update) free click to learn: https://space.bilibili.com/474380680

Guess you like

Origin www.cnblogs.com/Android-Alvin/p/11963095.html
Recommended