元始天尊 发表于 2015-12-27 14:10:14

【Android】android索引文件arsc格式解析

从apktool源码可以得到arsc格式,很多软件在该文件做手脚导致apktool异常。。。

part one
1.0 Resources And Chunks

Every Android application defines a set of Resources.

These Resources are stored in a Resource Table, or in some cases, for example, layouts, as Android specific Binary XML, which is referenced from the Resource Table.

An application’s Resource Table is stored persistently in the application’s .apk file in the resources.arsc file. Binary XML data is also stored in files in the .apk.

The Resource Table and associated Binary XML data is represented both at runtime and when stored in files by Chunks.

As a consequence an application’s Resources can be loaded by simply mapping the files that contain them into memory.

2.0 Chunks

A Chunk is just a piece of memory split into two parts, a header and a body. The exact structure of the header and the body of a given Chunk is determined by its type.

2.1 Chunk Types

The possible Chunk types are defined by the following C++ enum (see frameworks/base/include/ResourceTypes.h lines 179-201)
    enum {
      RES_NULL_TYPE               = 0x0000,
      RES_STRING_POOL_TYPE      = 0x0001,
      RES_TABLE_TYPE            = 0x0002,
      RES_XML_TYPE                = 0x0003,

      // Chunk types in RES_XML_TYPE
      RES_XML_FIRST_CHUNK_TYPE    = 0x0100,
      RES_XML_START_NAMESPACE_TYPE= 0x0100,
      RES_XML_END_NAMESPACE_TYPE= 0x0101,
      RES_XML_START_ELEMENT_TYPE= 0x0102,
      RES_XML_END_ELEMENT_TYPE    = 0x0103,
      RES_XML_CDATA_TYPE          = 0x0104,
      RES_XML_LAST_CHUNK_TYPE   = 0x017f,
      // This contains a uint32_t array mapping strings in the string
      // pool back to resource identifiers.It is optional.
      RES_XML_RESOURCE_MAP_TYPE   = 0x0180,

      // Chunk types in RES_TABLE_TYPE
      RES_TABLE_PACKAGE_TYPE      = 0x0200,
      RES_TABLE_TYPE_TYPE         = 0x0201,
      RES_TABLE_TYPE_SPEC_TYPE    = 0x0202
    };


2.2 Chunk Headers

All Chunk headers irrespective of the Chunk type have an instance of the C++ struct ResChunk_header (see frameworks/base/include/ResourceTypes.h lines 160-177)
as their first field
    struct ResChunk_header
    {
      // Type identifier for this chunk.The meaning of this value depends
      // on the containing chunk.
      uint16_t type;

      // Size of the chunk header (in bytes).Adding this value to
      // the address of the chunk allows you to find its associated data
      // (if any).
      uint16_t headerSize;

      // Total size of this chunk (in bytes).This is the chunkSize plus
      // the size of any data associated with the chunk.Adding this value
      // to the chunk allows you to completely skip its contents (including
      // any child chunks).If this value is the same as chunkSize, there is
      // no data associated with the chunk.
      uint32_t size;
    };


This means that given the address, A, of any Chunk it is always possible to determine

•its type


•where the body of the Chunk starts (A + headerSize)


•where the next Chunk, if any, starts (A + size)


without knowing anything further about the structure of the given Chunk.

2.3 Byte Order

By default the data in Chunks is in little-endian byte order both at runtime and when stored in files.



1.0 The Example

The contents of the example resources.arsc file comprise a single Table chunk.
    0000000 02 00 0c 00 64 04 00 00 01 00 00 00 01 00 1c 00
    0000010 d0 00 00 00 06 00 00 00 00 00 00 00 00 01 00 00
    0000020 34 00 00 00 00 00 00 00 00 00 00 00 1d 00 00 00
    0000030 3a 00 00 00 57 00 00 00 6d 00 00 00 8f 00 00 00
    0000040 1a 1a 72 65 73 2f 64 72 61 77 61 62 6c 65 2d 6c
    0000050 64 70 69 2f 69 63 6f 6e 2e 70 6e 67 00 1a 1a 72
    0000060 65 73 2f 64 72 61 77 61 62 6c 65 2d 6d 64 70 69
    0000070 2f 69 63 6f 6e 2e 70 6e 67 00 1a 1a 72 65 73 2f
    0000080 64 72 61 77 61 62 6c 65 2d 68 64 70 69 2f 69 63
    0000090 6f 6e 2e 70 6e 67 00 13 13 72 65 73 2f 6c 61 79
    00000a0 6f 75 74 2f 6d 61 69 6e 2e 78 6d 6c 00 1f 1f 48
    00000b0 65 6c 6c 6f 20 57 6f 72 6c 64 2c 20 50 65 6e 64
    00000c0 72 61 67 6f 6e 41 63 74 69 76 69 74 79 21 00 09
    00000d0 09 50 65 6e 64 72 61 67 6f 6e 00 00 00 02 1c 01
    00000e0 88 03 00 00 7f 00 00 00 78 00 70 00 65 00 72 00
    00000f0 2e 00 72 00 65 00 73 00 6f 00 75 00 72 00 63 00
    0000100 65 00 73 00 2e 00 70 00 65 00 6e 00 64 00 72 00
    0000110 61 00 67 00 6f 00 6e 00 00 00 00 00 00 00 00 00
    0000120 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    *
    00001e0 00 00 00 00 00 00 00 00 1c 01 00 00 04 00 00 00
    00001f0 6c 01 00 00 04 00 00 00 01 00 1c 00 50 00 00 00
    0000200 04 00 00 00 00 00 00 00 00 01 00 00 2c 00 00 00
    0000210 00 00 00 00 00 00 00 00 07 00 00 00 12 00 00 00
    0000220 1b 00 00 00 04 04 61 74 74 72 00 08 08 64 72 61
    0000230 77 61 62 6c 65 00 06 06 6c 61 79 6f 75 74 00 06
    0000240 06 73 74 72 69 6e 67 00 01 00 1c 00 50 00 00 00
    0000250 04 00 00 00 00 00 00 00 00 01 00 00 2c 00 00 00
    0000260 00 00 00 00 00 00 00 00 07 00 00 00 0e 00 00 00
    0000270 16 00 00 00 04 04 69 63 6f 6e 00 04 04 6d 61 69
    0000280 6e 00 05 05 68 65 6c 6c 6f 00 08 08 61 70 70 5f
    0000290 6e 61 6d 65 00 00 00 00 02 02 10 00 10 00 00 00
    00002a0 01 00 00 00 00 00 00 00 02 02 10 00 14 00 00 00
    00002b0 02 00 00 00 01 00 00 00 00 01 00 00 01 02 34 00
    00002c0 48 00 00 00 02 00 00 00 01 00 00 00 38 00 00 00
    00002d0 20 00 00 00 00 00 00 00 00 00 00 00 00 00 78 00
    00002e0 00 00 00 00 00 00 00 00 04 00 00 00 00 00 00 00
    00002f0 00 00 00 00 08 00 00 00 00 00 00 00 08 00 00 03
    0000300 00 00 00 00 01 02 34 00 48 00 00 00 02 00 00 00
    0000310 01 00 00 00 38 00 00 00 20 00 00 00 00 00 00 00
    0000320 00 00 00 00 00 00 a0 00 00 00 00 00 00 00 00 00
    0000330 04 00 00 00 00 00 00 00 00 00 00 00 08 00 00 00
    0000340 00 00 00 00 08 00 00 03 01 00 00 00 01 02 34 00
    0000350 48 00 00 00 02 00 00 00 01 00 00 00 38 00 00 00
    0000360 20 00 00 00 00 00 00 00 00 00 00 00 00 00 f0 00
    0000370 00 00 00 00 00 00 00 00 04 00 00 00 00 00 00 00
    0000380 00 00 00 00 08 00 00 00 00 00 00 00 08 00 00 03
    0000390 02 00 00 00 02 02 10 00 14 00 00 00 03 00 00 00
    00003a0 01 00 00 00 00 00 00 00 01 02 34 00 48 00 00 00
    00003b0 03 00 00 00 01 00 00 00 38 00 00 00 20 00 00 00
    00003c0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    00003d0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    00003e0 08 00 00 00 01 00 00 00 08 00 00 03 03 00 00 00
    00003f0 02 02 10 00 18 00 00 00 04 00 00 00 02 00 00 00
    0000400 00 00 00 00 00 00 00 00 01 02 34 00 5c 00 00 00
    0000410 04 00 00 00 02 00 00 00 3c 00 00 00 20 00 00 00
    0000420 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    0000430 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    0000440 10 00 00 00 08 00 00 00 02 00 00 00 08 00 00 03
    0000450 04 00 00 00 08 00 00 00 03 00 00 00 08 00 00 03
    0000460 05 00 00 00                                    
    0000464


The bytes in blue are the Table chunk header and those in green, that is, everything else, the Table chunk body.

2.0 The Table Chunk Header

The format of a Table chunk header is defined by the following C++ struct (see frameworks/base/include/ResourceTypes.h lines 755-761)
    struct ResTable_header
    {
      struct ResChunk_header header;

      // The number of ResTable_package structures.
      uint32_t packageCount;
    };


2.1 header

The header field is a struct ResChunk_header instance.

The header.type field is always 0x0002. (RES_TABLE_TYPE)

The header.headerSize field is always 0x000c.

2.2 packageCount

The packageCount field specifies the number of Packages contained in the Table.

2.3 The Example Annotated

This is an annotated version of the Table chunk header from the example.

    00000000 02 00       // type
    00000002 0c 00       // header size
    00000004 64 04 00 00 // chunk size
    --------------------

    00000008 01 00 00 00 // package count
    ++++++++++++++++++++

    ...


3.0 The Table Chunk Body

A Table chunk contains

•a set of Packages, where a Package is a collection of Resources


•a set of strings used by the Resources contained in those Packages


The set of strings are contained in a StringPool chunk. Each Package is contained in a corresponding Package chunk.

The StringPool chunk immediately follows the Table chunk header. The Package chunks follow the StringPool chunk.


页: [1]
查看完整版本: 【Android】android索引文件arsc格式解析