Just An Application

September 13, 2011

Android Internals: Resources – Part One: Resources And Chunks

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.


Copyright (c) 2011 By Simon Lewis. All Rights Reserved.

Advertisements

13 Comments »

  1. […] header field is a struct ResChunk_header […]

    Pingback by Android Internals: Resources – Part Three: The Table Chunk « Just An Application — September 14, 2011 @ 5:36 pm

  2. […] header field is a struct ResChunk_header […]

    Pingback by Android Internals: Resources – Part Four: The StringPool Chunk « Just An Application — September 15, 2011 @ 5:38 am

  3. […] The header.type field is always 0x0200 (RES_TABLE_PACKAGE_TYPE). […]

    Pingback by Android Internals: Resources – Part Five: The Package Chunk « Just An Application — September 16, 2011 @ 6:29 am

  4. […] header field is a struct ResChunk_header […]

    Pingback by Android Internals: Resources – Part Six: The Typespec Chunk « Just An Application — September 17, 2011 @ 5:57 am

  5. […] header field is a struct ResChunk_header […]

    Pingback by Android Internals: Resources – Part Seven: The Type Chunk « Just An Application — September 18, 2011 @ 5:07 am

  6. […] which is simply a struct ResChunk_header. […]

    Pingback by Android Internals: Binary XML – Part Two: The XML Chunk « Just An Application — September 22, 2011 @ 7:15 am

  7. […] The chunk header is an instance of struct ResChunk_header. […]

    Pingback by Android Internals: Binary XML – Part Four: The XML Resource Map Chunk « Just An Application — September 23, 2011 @ 3:17 am

  8. […] (RES_XML_START_NAMESPACE_TYPE) if the chunk represents the start of a Namespace, […]

    Pingback by Android Internals: Binary XML – Part Five: The Start And End Namespace Chunks « Just An Application — September 24, 2011 @ 5:51 am

  9. […] The header.type field is 0x0102 (RES_XML_START_ELEMENT_TYPE) […]

    Pingback by Android Internals: Binary XML – Part Six: The XML Start Element Chunk « Just An Application — September 25, 2011 @ 7:10 am

  10. […] The header.type field is 0x0103 (RES_XML_END_ELEMENT_TYPE). […]

    Pingback by Android Internals: Binary XML – Part Seven: The XML End Element Chunk « Just An Application — September 26, 2011 @ 5:46 am

  11. […] The header.type field is 0x0104 (RES_XML_CDATA_TYPE). […]

    Pingback by Android Internals: Binary XML – Part Eight: The CDATA Chunk « Just An Application — September 27, 2011 @ 3:24 am

  12. // 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;

    Should that be ‘headerSize’ instead of ‘chunkSize’ ?

    Comment by Pawel Veselov — November 18, 2011 @ 1:51 am

    • The code is verbatim from the Android source but yes ‘chunkSize’ should be ‘headerSize’ in that comment.

      Comment by Simon Lewis — November 18, 2011 @ 8:36 am


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: