Just An Application

September 27, 2011

Android Internals: Binary XML – Part Eight: The CDATA Chunk

1.0 Example

There are no CDATA chunks in the main.xml layout example and by default it is unlikely that the Binary XML version of any layout will contain any unless you go out of your way to do something like this.

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        This is some character data
    
        <![CDATA[This is some more character data]]>
        <![CDATA[<tag>As is this</tag>]]>
    </LinearLayout>

which will get you this

    0000000 03 00 08 00 cc 01 00 00 01 00 1c 00 04 01 00 00
    0000010 07 00 00 00 00 00 00 00 00 01 00 00 38 00 00 00
    0000020 00 00 00 00 00 00 00 00 0f 00 00 00 1f 00 00 00
    0000030 29 00 00 00 56 00 00 00 59 00 00 00 68 00 00 00
    0000040 0c 0c 6c 61 79 6f 75 74 5f 77 69 64 74 68 00 0d
    0000050 0d 6c 61 79 6f 75 74 5f 68 65 69 67 68 74 00 07
    0000060 07 61 6e 64 72 6f 69 64 00 2a 2a 68 74 74 70 3a
    0000070 2f 2f 73 63 68 65 6d 61 73 2e 61 6e 64 72 6f 69
    0000080 64 2e 63 6f 6d 2f 61 70 6b 2f 72 65 73 2f 61 6e
    0000090 64 72 6f 69 64 00 00 00 00 0c 0c 4c 69 6e 65 61
    00000a0 72 4c 61 79 6f 75 74 00 61 61 20 54 68 69 73 20
    00000b0 69 73 20 73 6f 6d 65 20 63 68 61 72 61 63 74 65
    00000c0 72 20 64 61 74 61 0a 20 20 20 20 0a 20 20 20 20
    00000d0 54 68 69 73 20 69 73 20 73 6f 6d 65 20 6d 6f 72
    00000e0 65 20 63 68 61 72 61 63 74 65 72 20 64 61 74 61
    00000f0 0a 20 20 20 20 3c 74 61 67 3e 41 73 20 69 73 20
    0000100 74 68 69 73 3c 2f 74 61 67 3e 0a 00 80 01 08 00
    0000110 10 00 00 00 f4 00 01 01 f5 00 01 01 00 01 10 00
    0000120 18 00 00 00 02 00 00 00 ff ff ff ff 02 00 00 00
    0000130 03 00 00 00 02 01 10 00 4c 00 00 00 02 00 00 00
    0000140 ff ff ff ff ff ff ff ff 05 00 00 00 14 00 14 00
    0000150 02 00 00 00 00 00 00 00 03 00 00 00 00 00 00 00
    0000160 ff ff ff ff 08 00 00 10 ff ff ff ff 03 00 00 00
    0000170 01 00 00 00 ff ff ff ff 08 00 00 10 ff ff ff ff
    0000180 04 01 10 00 1c 00 00 00 05 00 00 00 ff ff ff ff
    0000190 06 00 00 00 08 00 00 00 00 00 00 00 03 01 10 00
    00001a0 18 00 00 00 0b 00 00 00 ff ff ff ff ff ff ff ff
    00001b0 05 00 00 00 01 01 10 00 18 00 00 00 0b 00 00 00
    00001c0 ff ff ff ff 02 00 00 00 03 00 00 00    

The bytes in blue are the chunk header and those in green the chunk body.

2.0 Chunk Header

The chunk header is an instance of struct ResXMLTree_node.

The header.type field is 0x0104 (RES_XML_CDATA_TYPE).

3.0 Chunk Body

The chunk body is an instance of this C++ struct (see frameworks/base/include/ResourceTypes.h lines 520-527)

    struct ResXMLTree_cdataExt
    {
        // The raw CDATA character data.
        struct ResStringPool_ref data;
    
        // The typed value of the character data if this is a CDATA node.
        struct Res_value typedData;
    };

3.1 data

The data field specifies the raw character data as in index into the StringPool chunk of the containing XML chunk.

3.2 typedData

The typedData field specifies the value of the character data contained in this chunk.

It is not clear in what circumstances this field is going to contain an actual value and how that value is derived from the character data and there are no clues in the source code.

4.0 The Example Annotated

This is an annotated version of full Binary XML file shown above

    00000000 03 00       // type [XML]
    00000002 08 00       // header size
    00000004 cc 01 00 00 // chunk size
    --------------------

    ++++++++++++++++++++

    00000008 01 00       // type [STRING_POOL]
    0000000a 1c 00       // header size
    0000000c 04 01 00 00 // chunk size
    --------------------

    00000010 07 00 00 00 // stringCount
    00000014 00 00 00 00 // styleCount
    00000018 00 01 00 00 // flags
    0000001c 38 00 00 00 // stringsStart (address 00000040)
    00000020 00 00 00 00 // stylesStart  (address 00000008)
    ++++++++++++++++++++

    00000024 00 00 00 00 // string[0]
    00000028 0f 00 00 00 // string[1]
    0000002c 1f 00 00 00 // string[2]
    00000030 29 00 00 00 // string[3]
    00000034 56 00 00 00 // string[4]
    00000038 59 00 00 00 // string[5]
    0000003c 68 00 00 00 // string[6]

    00000040 0c 0c 6c 61 // [0] "layout_width"
    00000044 79 6f 75 74
    00000048 5f 77 69 64
    0000004c 74 68 00 0d // [1] "layout_height"
    00000050 0d 6c 61 79
    00000054 6f 75 74 5f
    00000058 68 65 69 67
    0000005c 68 74 00 07 // [2] "android"
    00000060 07 61 6e 64
    00000064 72 6f 69 64
    00000068 00 2a 2a 68 // [3] "http://schemas.android.com/apk/res/android"
    0000006c 74 74 70 3a
    00000070 2f 2f 73 63
    00000074 68 65 6d 61
    00000078 73 2e 61 6e
    0000007c 64 72 6f 69
    00000080 64 2e 63 6f
    00000084 6d 2f 61 70
    00000088 6b 2f 72 65
    0000008c 73 2f 61 6e
    00000090 64 72 6f 69
    00000094 64 00 00 00 // [4] ""
    00000098 00 0c 0c 4c // [5] "LinearLayout"
    0000009c 69 6e 65 61
    000000a0 72 4c 61 79
    000000a4 6f 75 74 00
    000000a8 61 61 20 54 // [6] " This is some character data
    
        This is some more character data
        <tag>As is this</tag>
    "
    000000ac 68 69 73 20
    000000b0 69 73 20 73
    000000b4 6f 6d 65 20
    000000b8 63 68 61 72
    000000bc 61 63 74 65
    000000c0 72 20 64 61
    000000c4 74 61 0a 20
    000000c8 20 20 20 0a
    000000cc 20 20 20 20
    000000d0 54 68 69 73
    000000d4 20 69 73 20
    000000d8 73 6f 6d 65
    000000dc 20 6d 6f 72
    000000e0 65 20 63 68
    000000e4 61 72 61 63
    000000e8 74 65 72 20
    000000ec 64 61 74 61
    000000f0 0a 20 20 20
    000000f4 20 3c 74 61
    000000f8 67 3e 41 73
    000000fc 20 69 73 20
    00000100 74 68 69 73
    00000104 3c 2f 74 61
    00000108 67 3e 0a 00
    ==================== [End of STRING_POOL]

    0000010c 80 01       // type [XML_RESOURCE_MAP]
    0000010e 08 00       // header size
    00000110 10 00 00 00 // chunk size
    --------------------

    ++++++++++++++++++++

    00000114 f4 00 01 01 // [0]
    00000118 f5 00 01 01 // [1]
    ==================== [End of XML_RESOURCE_MAP]

    0000011c 00 01       // type [XML_START_NAMESPACE]
    0000011e 10 00       // header size
    00000120 18 00 00 00 // chunk size
    --------------------

    00000124 02 00 00 00 // lineNumber
    00000128 ff ff ff ff // comment
    ++++++++++++++++++++

    0000012c 02 00 00 00 // prefix
    00000130 03 00 00 00 // uri
    ==================== [End of XML_START_NAMESPACE]

    00000134 02 01       // type [XML_START_ELEMENT]
    00000136 10 00       // header size
    00000138 4c 00 00 00 // chunk size
    --------------------

    0000013c 02 00 00 00 // lineNumber
    00000140 ff ff ff ff // comment
    ++++++++++++++++++++

    00000144 ff ff ff ff // ns
    00000148 05 00 00 00 // name [LinearLayout]
    0000014c 14 00       // attributeStart
    0000014e 14 00       // attributeSize
    00000150 02 00       // attributeCount
    00000152 00 00       // idIndex
    00000154 00 00       // classIndex
    00000156 00 00       // styleIndex
    00000158 03 00 00 00 // attribute[0] ns
    0000015c 00 00 00 00 // attribute[0] name [layout_width]
    00000160 ff ff ff ff // attribute[0] rawValue
    00000164 08 00       // size
    00000166 00          // 0
    00000167 10          // dataType
    00000168 ff ff ff ff // data
    0000016c 03 00 00 00 // attribute[1] ns
    00000170 01 00 00 00 // attribute[1] name [layout_height]
    00000174 ff ff ff ff // attribute[1] rawValue
    00000178 08 00       // size
    0000017a 00          // 0
    0000017b 10          // dataType
    0000017c ff ff ff ff // data
    ==================== [End of XML_START_ELEMENT]

    00000180 04 01       // type [XML_CDATA]
    00000182 10 00       // header size
    00000184 1c 00 00 00 // chunk size
    --------------------

    00000188 05 00 00 00 // lineNumber
    0000018c ff ff ff ff // comment
    ++++++++++++++++++++

    00000190 06 00 00 00 // data
    00000194 08 00       // size
    00000196 00          // 0
    00000197 00          // dataType
    00000198 00 00 00 00 // data
    ==================== [End of XML_CDATA]

    0000019c 03 01       // type [XML_END_ELEMENT]
    0000019e 10 00       // header size
    000001a0 18 00 00 00 // chunk size
    --------------------

    000001a4 0b 00 00 00 // lineNumber
    000001a8 ff ff ff ff // comment
    ++++++++++++++++++++

    000001ac ff ff ff ff // ns
    000001b0 05 00 00 00 // name [LinearLayout]
    ==================== [End of XML_END_ELEMENT]

    000001b4 01 01       // type [XML_END_NAMESPACE]
    000001b6 10 00       // header size
    000001b8 18 00 00 00 // chunk size
    --------------------

    000001bc 0b 00 00 00 // lineNumber
    000001c0 ff ff ff ff // comment
    ++++++++++++++++++++

    000001c4 02 00 00 00 // prefix
    000001c8 03 00 00 00 // uri
    ==================== [End of XML_END_NAMESPACE]

    ==================== [End of XML]

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

Advertisements

September 26, 2011

Android Internals: Binary XML – Part Seven: The XML End Element Chunk

1.0 The Example

The example contains two EndElement chunks representing the end tags of the TextView and LinearLayout elements

    0000000 03 00 08 00 c4 02 00 00 01 00 1c 00 84 01 00 00
    0000010 0a 00 00 00 00 00 00 00 00 00 00 00 44 00 00 00
    0000020 00 00 00 00 00 00 00 00 1a 00 00 00 36 00 00 00
    0000030 54 00 00 00 60 00 00 00 72 00 00 00 ca 00 00 00
    0000040 ce 00 00 00 ea 00 00 00 fe 00 00 00 0b 00 6f 00
    0000050 72 00 69 00 65 00 6e 00 74 00 61 00 74 00 69 00
    0000060 6f 00 6e 00 00 00 0c 00 6c 00 61 00 79 00 6f 00
    0000070 75 00 74 00 5f 00 77 00 69 00 64 00 74 00 68 00
    0000080 00 00 0d 00 6c 00 61 00 79 00 6f 00 75 00 74 00
    0000090 5f 00 68 00 65 00 69 00 67 00 68 00 74 00 00 00
    00000a0 04 00 74 00 65 00 78 00 74 00 00 00 07 00 61 00
    00000b0 6e 00 64 00 72 00 6f 00 69 00 64 00 00 00 2a 00
    00000c0 68 00 74 00 74 00 70 00 3a 00 2f 00 2f 00 73 00
    00000d0 63 00 68 00 65 00 6d 00 61 00 73 00 2e 00 61 00
    00000e0 6e 00 64 00 72 00 6f 00 69 00 64 00 2e 00 63 00
    00000f0 6f 00 6d 00 2f 00 61 00 70 00 6b 00 2f 00 72 00
    0000100 65 00 73 00 2f 00 61 00 6e 00 64 00 72 00 6f 00
    0000110 69 00 64 00 00 00 00 00 00 00 0c 00 4c 00 69 00
    0000120 6e 00 65 00 61 00 72 00 4c 00 61 00 79 00 6f 00
    0000130 75 00 74 00 00 00 08 00 54 00 65 00 78 00 74 00
    0000140 56 00 69 00 65 00 77 00 00 00 1e 00 48 00 65 00
    0000150 6c 00 6c 00 6f 00 20 00 57 00 6f 00 72 00 6c 00
    0000160 64 00 2c 00 20 00 50 00 65 00 6e 00 64 00 72 00
    0000170 61 00 67 00 6f 00 6e 00 41 00 63 00 74 00 69 00
    0000180 76 00 69 00 74 00 79 00 00 00 00 00 80 01 08 00
    0000190 18 00 00 00 c4 00 01 01 f4 00 01 01 f5 00 01 01
    00001a0 4f 01 01 01 00 01 10 00 18 00 00 00 02 00 00 00
    00001b0 ff ff ff ff 04 00 00 00 05 00 00 00 02 01 10 00
    00001c0 60 00 00 00 02 00 00 00 ff ff ff ff ff ff ff ff
    00001d0 07 00 00 00 14 00 14 00 03 00 00 00 00 00 00 00
    00001e0 05 00 00 00 00 00 00 00 ff ff ff ff 08 00 00 10
    00001f0 01 00 00 00 05 00 00 00 01 00 00 00 ff ff ff ff
    0000200 08 00 00 10 ff ff ff ff 05 00 00 00 02 00 00 00
    0000210 ff ff ff ff 08 00 00 10 ff ff ff ff 02 01 10 00
    0000220 60 00 00 00 07 00 00 00 ff ff ff ff ff ff ff ff
    0000230 08 00 00 00 14 00 14 00 03 00 00 00 00 00 00 00
    0000240 05 00 00 00 01 00 00 00 ff ff ff ff 08 00 00 10
    0000250 ff ff ff ff 05 00 00 00 02 00 00 00 ff ff ff ff
    0000260 08 00 00 10 fe ff ff ff 05 00 00 00 03 00 00 00
    0000270 09 00 00 00 08 00 00 03 09 00 00 00 03 01 10 00
    0000280 18 00 00 00 0b 00 00 00 ff ff ff ff ff ff ff ff
    0000290 08 00 00 00 03 01 10 00 18 00 00 00 0c 00 00 00
    00002a0 ff ff ff ff ff ff ff ff 07 00 00 00 01 01 10 00
    00002b0 18 00 00 00 0c 00 00 00 ff ff ff ff 04 00 00 00
    00002c0 05 00 00 00                                    
    00002c4

The bytes in blue are the chunk headers and those in green the chunk bodies.

2.0 Chunk Header

The chunk header is an instance of struct ResXMLTree_node.

The header.type field is 0x0103 (RES_XML_END_ELEMENT_TYPE).

3.0 Chunk Body

The chunk body is an instance of the following C++ struct (see frameworks/base/include/ResourceTypes.h lines 546-554)

    struct ResXMLTree_endElementExt
    {
        // String of the full namespace of this element.
        struct ResStringPool_ref ns;
    
        // String name of this node if it is an ELEMENT; the raw
        // character data if this is a CDATA node.
        struct ResStringPool_ref name;
    };

3.1 ns

The ns specifies the namespace name (the Namespace URI) of this element as an index into the StringPool of the containing XML chunk.

If the element has no namespace name the value of this field is -1 (0xFFFFFFFF).

3.2 name

The name field specifies the local name of the element as an index into the StringPool of the containing XML chunk.

4.0 The Example Annotated

This is an annotated version of the EndElement chunk which represents the end tag of the LinearLayout element in the example.

    ...

    00000294 03 01       // type [XML_END_ELEMENT]
    00000296 10 00       // header size
    00000298 18 00 00 00 // chunk size
    --------------------

    0000029c 0c 00 00 00 // lineNumber
    000002a0 ff ff ff ff // comment
    ++++++++++++++++++++

    000002a4 ff ff ff ff // ns
    000002a8 07 00 00 00 // name [LinearLayout]
    ==================== [End of XML_END_ELEMENT]
	
    ...

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

September 25, 2011

Android Internals: Binary XML – Part Six: The XML Start Element Chunk

1.0 The Example

The example contains two StartElement chunks representing the start tags of the LinearLayout and TextView elements

    0000000 03 00 08 00 c4 02 00 00 01 00 1c 00 84 01 00 00
    0000010 0a 00 00 00 00 00 00 00 00 00 00 00 44 00 00 00
    0000020 00 00 00 00 00 00 00 00 1a 00 00 00 36 00 00 00
    0000030 54 00 00 00 60 00 00 00 72 00 00 00 ca 00 00 00
    0000040 ce 00 00 00 ea 00 00 00 fe 00 00 00 0b 00 6f 00
    0000050 72 00 69 00 65 00 6e 00 74 00 61 00 74 00 69 00
    0000060 6f 00 6e 00 00 00 0c 00 6c 00 61 00 79 00 6f 00
    0000070 75 00 74 00 5f 00 77 00 69 00 64 00 74 00 68 00
    0000080 00 00 0d 00 6c 00 61 00 79 00 6f 00 75 00 74 00
    0000090 5f 00 68 00 65 00 69 00 67 00 68 00 74 00 00 00
    00000a0 04 00 74 00 65 00 78 00 74 00 00 00 07 00 61 00
    00000b0 6e 00 64 00 72 00 6f 00 69 00 64 00 00 00 2a 00
    00000c0 68 00 74 00 74 00 70 00 3a 00 2f 00 2f 00 73 00
    00000d0 63 00 68 00 65 00 6d 00 61 00 73 00 2e 00 61 00
    00000e0 6e 00 64 00 72 00 6f 00 69 00 64 00 2e 00 63 00
    00000f0 6f 00 6d 00 2f 00 61 00 70 00 6b 00 2f 00 72 00
    0000100 65 00 73 00 2f 00 61 00 6e 00 64 00 72 00 6f 00
    0000110 69 00 64 00 00 00 00 00 00 00 0c 00 4c 00 69 00
    0000120 6e 00 65 00 61 00 72 00 4c 00 61 00 79 00 6f 00
    0000130 75 00 74 00 00 00 08 00 54 00 65 00 78 00 74 00
    0000140 56 00 69 00 65 00 77 00 00 00 1e 00 48 00 65 00
    0000150 6c 00 6c 00 6f 00 20 00 57 00 6f 00 72 00 6c 00
    0000160 64 00 2c 00 20 00 50 00 65 00 6e 00 64 00 72 00
    0000170 61 00 67 00 6f 00 6e 00 41 00 63 00 74 00 69 00
    0000180 76 00 69 00 74 00 79 00 00 00 00 00 80 01 08 00
    0000190 18 00 00 00 c4 00 01 01 f4 00 01 01 f5 00 01 01
    00001a0 4f 01 01 01 00 01 10 00 18 00 00 00 02 00 00 00
    00001b0 ff ff ff ff 04 00 00 00 05 00 00 00 02 01 10 00
    00001c0 60 00 00 00 02 00 00 00 ff ff ff ff ff ff ff ff
    00001d0 07 00 00 00 14 00 14 00 03 00 00 00 00 00 00 00
    00001e0 05 00 00 00 00 00 00 00 ff ff ff ff 08 00 00 10
    00001f0 01 00 00 00 05 00 00 00 01 00 00 00 ff ff ff ff
    0000200 08 00 00 10 ff ff ff ff 05 00 00 00 02 00 00 00
    0000210 ff ff ff ff 08 00 00 10 ff ff ff ff 02 01 10 00
    0000220 60 00 00 00 07 00 00 00 ff ff ff ff ff ff ff ff
    0000230 08 00 00 00 14 00 14 00 03 00 00 00 00 00 00 00
    0000240 05 00 00 00 01 00 00 00 ff ff ff ff 08 00 00 10
    0000250 ff ff ff ff 05 00 00 00 02 00 00 00 ff ff ff ff
    0000260 08 00 00 10 fe ff ff ff 05 00 00 00 03 00 00 00
    0000270 09 00 00 00 08 00 00 03 09 00 00 00 03 01 10 00
    0000280 18 00 00 00 0b 00 00 00 ff ff ff ff ff ff ff ff
    0000290 08 00 00 00 03 01 10 00 18 00 00 00 0c 00 00 00
    00002a0 ff ff ff ff ff ff ff ff 07 00 00 00 01 01 10 00
    00002b0 18 00 00 00 0c 00 00 00 ff ff ff ff 04 00 00 00
    00002c0 05 00 00 00                                    
    00002c4

The bytes in blue are the chunk headers and those in green the chunk bodies.

2.0 Chunk Header

The chunk header is an instance of struct ResXMLTree_node.

The header.type field is 0x0102 (RES_XML_START_ELEMENT_TYPE)

3.0 Chunk Body

The chunk body comprises an instance of the C++ struct ResXMLTree_attrExt followed by zero or more Attributes.

3.1 struct ResXMLTree_attrExt

The struct ResXMLTree_attrExt is defined as follows (see frameworks/base/include/ResourceTypes.h lines 561-589)

    struct ResXMLTree_attrExt
    {
        // String of the full namespace of this element.
        struct ResStringPool_ref ns;
    
        // String name of this node if it is an ELEMENT; the raw
        // character data if this is a CDATA node.
        struct ResStringPool_ref name;
    
        // Byte offset from the start of this structure where the attributes start.
        uint16_t attributeStart;
    
        // Size of the ResXMLTree_attribute structures that follow.
        uint16_t attributeSize;
    
        // Number of attributes associated with an ELEMENT.  These are
        // available as an array of ResXMLTree_attribute structures
        // immediately following this node.
        uint16_t attributeCount;
    
        // Index (1-based) of the "id" attribute. 0 if none.
        uint16_t idIndex;
    
        // Index (1-based) of the "class" attribute. 0 if none.
        uint16_t classIndex;
    
        // Index (1-based) of the "style" attribute. 0 if none.
        uint16_t styleIndex;
    };

3.1.1 ns

The ns specifies the namespace name (the Namespace URI) of this element as an index into the StringPool of the containing XML chunk.

If the element has no namespace name the value of this field is -1.

3.1.2 name

The name field specifies the local name of this element as an index into the StringPool of the containing XML chunk.

3.1.3 attributeStart

The attributeStart field specifies the start of the attribute data within the chunk body as an offset from the start of the chunk body.

Since this structure is at the start of the chunk body and is of fixed-size the value of this field is fixed at 20 (0x0014).

3.1.4 attributeSize

The attributeSize field specifies the size in bytes of the structure used to represent an Attribute.

This value is fixed and coincidentally is the same as that of the attributeStart field preceding it, that is, 20 (0x0014).

3.1.5 attributeCount

The attributeCount field specifies the number of Attributes that follow.

3.1.6 idIndex

The idIndex field specifies the Attribute if any which represents the id attribute in the original XML.

3.1.7 classIndex

The classIndex field specifies the Attribute if any which represents the class attribute in the original XML.

3.1.8 styleIndex

The styleIndex field specifies the Attribute if any which represents the style attribute in the original XML.

3.2 Attributes

An Attribute is represented by an instance of this C++ struct (see frameworks/base/include/ResourceTypes.h lines 591-604)

    struct ResXMLTree_attribute
    {
        // Namespace of this attribute.
        struct ResStringPool_ref ns;
    
        // Name of this attribute.
        struct ResStringPool_ref name;

        // The original raw string value of this attribute.
        struct ResStringPool_ref rawValue;
    
        // Processesd typed value of this attribute.
        struct Res_value typedValue;
    };

3.2.1 ns

The ns field specifies namespace name (the Namespace URI) of the attribute as an index into the StringPool of the containing XML chunk.

3.2.2 name

The name field specifies the local name of this Attribute as an index into the StringPool of the containing XML chunk.

Note that the value of this field also functions as an index into the Resource Id array of the XMLResourceMap chunk if it is present.

3.2.3 rawValue

The rawValue field specifies the value of this Attribute as it appeared in the original XML as an index into the StringPool of the containing XML chunk.

If the original value is not available it has the value -1.

3.2.4 typedValue

The typedValue is an instance of struct Res_value which specifies the value of the Attribute.

4.0 The Example Annotated

This is an annotated version of the StartElement chunk which represents the start tag of the LinearLayout element in the example.

    ...

    000001bc 02 01       // type [XML_START_ELEMENT]
    000001be 10 00       // header size
    000001c0 60 00 00 00 // chunk size
    --------------------

    000001c4 02 00 00 00 // lineNumber
    000001c8 ff ff ff ff // comment
    ++++++++++++++++++++

    000001cc ff ff ff ff // ns
    000001d0 07 00 00 00 // name [LinearLayout]
    000001d4 14 00       // attributeStart
    000001d6 14 00       // attributeSize
    000001d8 03 00       // attributeCount
    000001da 00 00       // idIndex
    000001dc 00 00       // classIndex
    000001de 00 00       // styleIndex
    000001e0 05 00 00 00 // attribute[0] ns
    000001e4 00 00 00 00 // attribute[0] name [orientation]
    000001e8 ff ff ff ff // attribute[0] rawValue
    000001ec 08 00       // size
    000001ee 00          // 0
    000001ef 10          // dataType
    000001f0 01 00 00 00 // data
    000001f4 05 00 00 00 // attribute[1] ns
    000001f8 01 00 00 00 // attribute[1] name [layout_width]
    000001fc ff ff ff ff // attribute[1] rawValue
    00000200 08 00       // size
    00000202 00          // 0
    00000203 10          // dataType
    00000204 ff ff ff ff // data
    00000208 05 00 00 00 // attribute[2] ns
    0000020c 02 00 00 00 // attribute[2] name [layout_height]
    00000210 ff ff ff ff // attribute[2] rawValue
    00000214 08 00       // size
    00000216 00          // 0
    00000217 10          // dataType
    00000218 ff ff ff ff // data
    ==================== [End of XML_START_ELEMENT]
	
    ...

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

September 24, 2011

Android Internals: Binary XML – Part Five: The XML Start And End Namespace Chunks

1.0 The Example

The example contains a single pair of Start/End Namespace chunks

    0000000 03 00 08 00 c4 02 00 00 01 00 1c 00 84 01 00 00
    0000010 0a 00 00 00 00 00 00 00 00 00 00 00 44 00 00 00
    0000020 00 00 00 00 00 00 00 00 1a 00 00 00 36 00 00 00
    0000030 54 00 00 00 60 00 00 00 72 00 00 00 ca 00 00 00
    0000040 ce 00 00 00 ea 00 00 00 fe 00 00 00 0b 00 6f 00
    0000050 72 00 69 00 65 00 6e 00 74 00 61 00 74 00 69 00
    0000060 6f 00 6e 00 00 00 0c 00 6c 00 61 00 79 00 6f 00
    0000070 75 00 74 00 5f 00 77 00 69 00 64 00 74 00 68 00
    0000080 00 00 0d 00 6c 00 61 00 79 00 6f 00 75 00 74 00
    0000090 5f 00 68 00 65 00 69 00 67 00 68 00 74 00 00 00
    00000a0 04 00 74 00 65 00 78 00 74 00 00 00 07 00 61 00
    00000b0 6e 00 64 00 72 00 6f 00 69 00 64 00 00 00 2a 00
    00000c0 68 00 74 00 74 00 70 00 3a 00 2f 00 2f 00 73 00
    00000d0 63 00 68 00 65 00 6d 00 61 00 73 00 2e 00 61 00
    00000e0 6e 00 64 00 72 00 6f 00 69 00 64 00 2e 00 63 00
    00000f0 6f 00 6d 00 2f 00 61 00 70 00 6b 00 2f 00 72 00
    0000100 65 00 73 00 2f 00 61 00 6e 00 64 00 72 00 6f 00
    0000110 69 00 64 00 00 00 00 00 00 00 0c 00 4c 00 69 00
    0000120 6e 00 65 00 61 00 72 00 4c 00 61 00 79 00 6f 00
    0000130 75 00 74 00 00 00 08 00 54 00 65 00 78 00 74 00
    0000140 56 00 69 00 65 00 77 00 00 00 1e 00 48 00 65 00
    0000150 6c 00 6c 00 6f 00 20 00 57 00 6f 00 72 00 6c 00
    0000160 64 00 2c 00 20 00 50 00 65 00 6e 00 64 00 72 00
    0000170 61 00 67 00 6f 00 6e 00 41 00 63 00 74 00 69 00
    0000180 76 00 69 00 74 00 79 00 00 00 00 00 80 01 08 00
    0000190 18 00 00 00 c4 00 01 01 f4 00 01 01 f5 00 01 01
    00001a0 4f 01 01 01 00 01 10 00 18 00 00 00 02 00 00 00
    00001b0 ff ff ff ff 04 00 00 00 05 00 00 00 02 01 10 00
    00001c0 60 00 00 00 02 00 00 00 ff ff ff ff ff ff ff ff
    00001d0 07 00 00 00 14 00 14 00 03 00 00 00 00 00 00 00
    00001e0 05 00 00 00 00 00 00 00 ff ff ff ff 08 00 00 10
    00001f0 01 00 00 00 05 00 00 00 01 00 00 00 ff ff ff ff
    0000200 08 00 00 10 ff ff ff ff 05 00 00 00 02 00 00 00
    0000210 ff ff ff ff 08 00 00 10 ff ff ff ff 02 01 10 00
    0000220 60 00 00 00 07 00 00 00 ff ff ff ff ff ff ff ff
    0000230 08 00 00 00 14 00 14 00 03 00 00 00 00 00 00 00
    0000240 05 00 00 00 01 00 00 00 ff ff ff ff 08 00 00 10
    0000250 ff ff ff ff 05 00 00 00 02 00 00 00 ff ff ff ff
    0000260 08 00 00 10 fe ff ff ff 05 00 00 00 03 00 00 00
    0000270 09 00 00 00 08 00 00 03 09 00 00 00 03 01 10 00
    0000280 18 00 00 00 0b 00 00 00 ff ff ff ff ff ff ff ff
    0000290 08 00 00 00 03 01 10 00 18 00 00 00 0c 00 00 00
    00002a0 ff ff ff ff ff ff ff ff 07 00 00 00 01 01 10 00
    00002b0 18 00 00 00 0c 00 00 00 ff ff ff ff 04 00 00 00
    00002c0 05 00 00 00                                    
    00002c4

The bytes in blue are the chunk headers and those in green the chunk bodies.

The Start Namespace chunk starts at 00001a4 and the End Namespace chunk at 00002ac.

2.0 Start And End Namespace Chunks

Start and end namespace chunks represent the scope of a namespace declaration which occurs in the start tag of an element.

If there are multiple declarations in the same start tag then the start namespace chunks for the namespaces appear in the order of the declarations and the end namespace chunks in reverse order.

The chunks which represent the start and end of namespace scopes have the same structure.

3.0 Chunk Header

The chunk header is an instance of the struct ResXMLTree_node.

The header.type field is

The header.headerSize is always 16 (0x0010).

The header.size is always 24 (0x0018).

4.0 Chunk Body

The chunk body is an instance of the following C++ struct (see frameworks/base/include/ResourceTypes.h lines 533-540)

    struct ResXMLTree_namespaceExt
    {
        // The prefix of the namespace.
        struct ResStringPool_ref prefix;
    
        // The URI of the namespace.
        struct ResStringPool_ref uri;
    };

4.1 prefix

The prefix field specifies the XML Namespace prefix as an index into the StringPool of the containing XML chunk.

4.1 uri

The uri field specifies the XML Namespace URI as an index into the StringPool of the containing XML chunk.

5.0 The Example Annotated

5.1 The Start Namespace Chunk

This is an annotated version of the Start Namespace chunk from the example.

    ...

    000001a4 00 01       // type [XML_START_NAMESPACE]
    000001a6 10 00       // header size
    000001a8 18 00 00 00 // chunk size
    --------------------

    000001ac 02 00 00 00 // lineNumber
    000001b0 ff ff ff ff // comment
    ++++++++++++++++++++

    000001b4 04 00 00 00 // prefix
    000001b8 05 00 00 00 // uri
    ==================== [End of XML_START_NAMESPACE]
	
    ...

5.2 The End Namespace Chunk

This is an annotated version of the End Namespace chunk from the example.

    ...

    000002ac 01 01       // type [XML_END_NAMESPACE]
    000002ae 10 00       // header size
    000002b0 18 00 00 00 // chunk size
    --------------------

    000002b4 0c 00 00 00 // lineNumber
    000002b8 ff ff ff ff // comment
    ++++++++++++++++++++

    000002bc 04 00 00 00 // prefix
    000002c0 05 00 00 00 // uri
    ==================== [End of XML_END_NAMESPACE]
	
    ...

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

September 23, 2011

Android Internals: Binary XML – Part Four: The XML Resource Map Chunk

1.0 The Example

The example contains a single XMLResourceMap chunk.

    0000000 03 00 08 00 c4 02 00 00 01 00 1c 00 84 01 00 00
    0000010 0a 00 00 00 00 00 00 00 00 00 00 00 44 00 00 00
    0000020 00 00 00 00 00 00 00 00 1a 00 00 00 36 00 00 00
    0000030 54 00 00 00 60 00 00 00 72 00 00 00 ca 00 00 00
    0000040 ce 00 00 00 ea 00 00 00 fe 00 00 00 0b 00 6f 00
    0000050 72 00 69 00 65 00 6e 00 74 00 61 00 74 00 69 00
    0000060 6f 00 6e 00 00 00 0c 00 6c 00 61 00 79 00 6f 00
    0000070 75 00 74 00 5f 00 77 00 69 00 64 00 74 00 68 00
    0000080 00 00 0d 00 6c 00 61 00 79 00 6f 00 75 00 74 00
    0000090 5f 00 68 00 65 00 69 00 67 00 68 00 74 00 00 00
    00000a0 04 00 74 00 65 00 78 00 74 00 00 00 07 00 61 00
    00000b0 6e 00 64 00 72 00 6f 00 69 00 64 00 00 00 2a 00
    00000c0 68 00 74 00 74 00 70 00 3a 00 2f 00 2f 00 73 00
    00000d0 63 00 68 00 65 00 6d 00 61 00 73 00 2e 00 61 00
    00000e0 6e 00 64 00 72 00 6f 00 69 00 64 00 2e 00 63 00
    00000f0 6f 00 6d 00 2f 00 61 00 70 00 6b 00 2f 00 72 00
    0000100 65 00 73 00 2f 00 61 00 6e 00 64 00 72 00 6f 00
    0000110 69 00 64 00 00 00 00 00 00 00 0c 00 4c 00 69 00
    0000120 6e 00 65 00 61 00 72 00 4c 00 61 00 79 00 6f 00
    0000130 75 00 74 00 00 00 08 00 54 00 65 00 78 00 74 00
    0000140 56 00 69 00 65 00 77 00 00 00 1e 00 48 00 65 00
    0000150 6c 00 6c 00 6f 00 20 00 57 00 6f 00 72 00 6c 00
    0000160 64 00 2c 00 20 00 50 00 65 00 6e 00 64 00 72 00
    0000170 61 00 67 00 6f 00 6e 00 41 00 63 00 74 00 69 00
    0000180 76 00 69 00 74 00 79 00 00 00 00 00 80 01 08 00
    0000190 18 00 00 00 c4 00 01 01 f4 00 01 01 f5 00 01 01
    00001a0 4f 01 01 01 00 01 10 00 18 00 00 00 02 00 00 00
    00001b0 ff ff ff ff 04 00 00 00 05 00 00 00 02 01 10 00
    00001c0 60 00 00 00 02 00 00 00 ff ff ff ff ff ff ff ff
    00001d0 07 00 00 00 14 00 14 00 03 00 00 00 00 00 00 00
    00001e0 05 00 00 00 00 00 00 00 ff ff ff ff 08 00 00 10
    00001f0 01 00 00 00 05 00 00 00 01 00 00 00 ff ff ff ff
    0000200 08 00 00 10 ff ff ff ff 05 00 00 00 02 00 00 00
    0000210 ff ff ff ff 08 00 00 10 ff ff ff ff 02 01 10 00
    0000220 60 00 00 00 07 00 00 00 ff ff ff ff ff ff ff ff
    0000230 08 00 00 00 14 00 14 00 03 00 00 00 00 00 00 00
    0000240 05 00 00 00 01 00 00 00 ff ff ff ff 08 00 00 10
    0000250 ff ff ff ff 05 00 00 00 02 00 00 00 ff ff ff ff
    0000260 08 00 00 10 fe ff ff ff 05 00 00 00 03 00 00 00
    0000270 09 00 00 00 08 00 00 03 09 00 00 00 03 01 10 00
    0000280 18 00 00 00 0b 00 00 00 ff ff ff ff ff ff ff ff
    0000290 08 00 00 00 03 01 10 00 18 00 00 00 0c 00 00 00
    00002a0 ff ff ff ff ff ff ff ff 07 00 00 00 01 01 10 00
    00002b0 18 00 00 00 0c 00 00 00 ff ff ff ff 04 00 00 00
    00002c0 05 00 00 00                                    
    00002c4

The bytes in blue are the chunk header and those in green the chunk body.

2.0 Chunk Header

The chunk header is an instance of struct ResChunk_header.

The header.type field is 0x0180 (RES_XML_RESOURCE_MAP_TYPE).

3.0 Chunk Body

The chunk body is an array of Resource Ids.

The element at index i is the Resource Id of the Attribute named by the string at index i in the StringPool chunk of the containing XML chunk.

4.0 The Example Annotated

This is an annotated version of the XMLResourceMap chunk from the example.

    ...
    
    0000018c 80 01       // type [XML_RESOURCE_MAP]
    0000018e 08 00       // header size
    00000190 18 00 00 00 // chunk size
    --------------------

    ++++++++++++++++++++

    00000194 c4 00 01 01 // [0]
    00000198 f4 00 01 01 // [1]
    0000019c f5 00 01 01 // [2]
    000001a0 4f 01 01 01 // [3]
    ==================== [End of XML_RESOURCE_MAP]
	
    ...

The specified Resource Ids are

  • 0x010100c4

  • 0x010100f4

  • 0x010100f5

  • 0x0101014f

The first four strings in the StringPool are

  • orientation

  • layout_width

  • layout_height

  • text

Checking the documentation shows that the Resource Ids of the attributes

  • R.attr.orientation

  • R.attr.layout_width

  • R.attr.layout_height

  • R.attr.text

in the android package are indeed those specified in the chunk body.


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

September 22, 2011

Android Internals: Binary XML – Part Three: XML Node

The headers of the chunks representing the start and end of namespace scopes, the start and end of elements, and CDATA are all instances of the following
C++ struct (see frameworks/base/include/ResourceTypes.h lines 505-514)

    struct ResXMLTree_node
    {
        struct ResChunk_header header;

        // Line number in original source file at which this element appeared.
        uint32_t lineNumber;

        // Optional XML comment that was associated with this element; -1 if none.
        struct ResStringPool_ref comment;
    };

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

Android Internals: Binary XML – Part Two: The XML Chunk

1.0 The Example

The example contains a single XML chunk which represents the original XML document.

    0000000 03 00 08 00 c4 02 00 00 01 00 1c 00 84 01 00 00
    0000010 0a 00 00 00 00 00 00 00 00 00 00 00 44 00 00 00
    0000020 00 00 00 00 00 00 00 00 1a 00 00 00 36 00 00 00
    0000030 54 00 00 00 60 00 00 00 72 00 00 00 ca 00 00 00
    0000040 ce 00 00 00 ea 00 00 00 fe 00 00 00 0b 00 6f 00
    0000050 72 00 69 00 65 00 6e 00 74 00 61 00 74 00 69 00
    0000060 6f 00 6e 00 00 00 0c 00 6c 00 61 00 79 00 6f 00
    0000070 75 00 74 00 5f 00 77 00 69 00 64 00 74 00 68 00
    0000080 00 00 0d 00 6c 00 61 00 79 00 6f 00 75 00 74 00
    0000090 5f 00 68 00 65 00 69 00 67 00 68 00 74 00 00 00
    00000a0 04 00 74 00 65 00 78 00 74 00 00 00 07 00 61 00
    00000b0 6e 00 64 00 72 00 6f 00 69 00 64 00 00 00 2a 00
    00000c0 68 00 74 00 74 00 70 00 3a 00 2f 00 2f 00 73 00
    00000d0 63 00 68 00 65 00 6d 00 61 00 73 00 2e 00 61 00
    00000e0 6e 00 64 00 72 00 6f 00 69 00 64 00 2e 00 63 00
    00000f0 6f 00 6d 00 2f 00 61 00 70 00 6b 00 2f 00 72 00
    0000100 65 00 73 00 2f 00 61 00 6e 00 64 00 72 00 6f 00
    0000110 69 00 64 00 00 00 00 00 00 00 0c 00 4c 00 69 00
    0000120 6e 00 65 00 61 00 72 00 4c 00 61 00 79 00 6f 00
    0000130 75 00 74 00 00 00 08 00 54 00 65 00 78 00 74 00
    0000140 56 00 69 00 65 00 77 00 00 00 1e 00 48 00 65 00
    0000150 6c 00 6c 00 6f 00 20 00 57 00 6f 00 72 00 6c 00
    0000160 64 00 2c 00 20 00 50 00 65 00 6e 00 64 00 72 00
    0000170 61 00 67 00 6f 00 6e 00 41 00 63 00 74 00 69 00
    0000180 76 00 69 00 74 00 79 00 00 00 00 00 80 01 08 00
    0000190 18 00 00 00 c4 00 01 01 f4 00 01 01 f5 00 01 01
    00001a0 4f 01 01 01 00 01 10 00 18 00 00 00 02 00 00 00
    00001b0 ff ff ff ff 04 00 00 00 05 00 00 00 02 01 10 00
    00001c0 60 00 00 00 02 00 00 00 ff ff ff ff ff ff ff ff
    00001d0 07 00 00 00 14 00 14 00 03 00 00 00 00 00 00 00
    00001e0 05 00 00 00 00 00 00 00 ff ff ff ff 08 00 00 10
    00001f0 01 00 00 00 05 00 00 00 01 00 00 00 ff ff ff ff
    0000200 08 00 00 10 ff ff ff ff 05 00 00 00 02 00 00 00
    0000210 ff ff ff ff 08 00 00 10 ff ff ff ff 02 01 10 00
    0000220 60 00 00 00 07 00 00 00 ff ff ff ff ff ff ff ff
    0000230 08 00 00 00 14 00 14 00 03 00 00 00 00 00 00 00
    0000240 05 00 00 00 01 00 00 00 ff ff ff ff 08 00 00 10
    0000250 ff ff ff ff 05 00 00 00 02 00 00 00 ff ff ff ff
    0000260 08 00 00 10 fe ff ff ff 05 00 00 00 03 00 00 00
    0000270 09 00 00 00 08 00 00 03 09 00 00 00 03 01 10 00
    0000280 18 00 00 00 0b 00 00 00 ff ff ff ff ff ff ff ff
    0000290 08 00 00 00 03 01 10 00 18 00 00 00 0c 00 00 00
    00002a0 ff ff ff ff ff ff ff ff 07 00 00 00 01 01 10 00
    00002b0 18 00 00 00 0c 00 00 00 ff ff ff ff 04 00 00 00
    00002c0 05 00 00 00                                    
    00002c4

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

2.0 Chunk Header

The chunk header is an instance of the following C++ struct (see frameworks/base/include/ResourceTypes.h lines 496-499)

    struct ResXMLTree_header
    {
        struct ResChunk_header header;
    };

which is simply a struct ResChunk_header.

The header.type is always 0x0003 (RES_XML_TYPE).

3.0 Chunk Body

The chunk body comprises

  • a StringPool chunk

  • an optional XMLResourceMap chunk

  • the chunks representing the elements of the original XML document

The XML document is typically represented as follows

  • a StartNamespace chunk

  • a StartElement chunk representing the start tag of the root element

  • zero or more chunks representing the body of the root element

  • an EndElement chunk representing the end tag of the root element

  • an EndNamespace chunk

4.0 The Example Annotated

This is an annotated version of the chunk header and the following StringPool chunk from the example.


    00000000 03 00       // type [XML]
    00000002 08 00       // header size
    00000004 c4 02 00 00 // chunk size
    --------------------

    ++++++++++++++++++++

    00000008 01 00       // type [STRING_POOL]
    0000000a 1c 00       // header size
    0000000c 84 01 00 00 // chunk size
    --------------------

    00000010 0a 00 00 00 // stringCount
    00000014 00 00 00 00 // styleCount
    00000018 00 00 00 00 // flags
    0000001c 44 00 00 00 // stringsStart (address 0000004c)
    00000020 00 00 00 00 // stylesStart  (address 00000008)
    ++++++++++++++++++++

    00000024 00 00 00 00 // string[0]
    00000028 1a 00 00 00 // string[1]
    0000002c 36 00 00 00 // string[2]
    00000030 54 00 00 00 // string[3]
    00000034 60 00 00 00 // string[4]
    00000038 72 00 00 00 // string[5]
    0000003c ca 00 00 00 // string[6]
    00000040 ce 00 00 00 // string[7]
    00000044 ea 00 00 00 // string[8]
    00000048 fe 00 00 00 // string[9]

    0000004c 0b 00 6f 00 // [0] "orientation"
    00000050 72 00 69 00
    00000054 65 00 6e 00
    00000058 74 00 61 00
    0000005c 74 00 69 00
    00000060 6f 00 6e 00
    00000064 00 00 0c 00 // [1] "layout_width"
    00000068 6c 00 61 00
    0000006c 79 00 6f 00
    00000070 75 00 74 00
    00000074 5f 00 77 00
    00000078 69 00 64 00
    0000007c 74 00 68 00
    00000080 00 00 0d 00 // [2] "layout_height"
    00000084 6c 00 61 00
    00000088 79 00 6f 00
    0000008c 75 00 74 00
    00000090 5f 00 68 00
    00000094 65 00 69 00
    00000098 67 00 68 00
    0000009c 74 00 00 00
    000000a0 04 00 74 00 // [3] "text"
    000000a4 65 00 78 00
    000000a8 74 00 00 00
    000000ac 07 00 61 00 // [4] "android"
    000000b0 6e 00 64 00
    000000b4 72 00 6f 00
    000000b8 69 00 64 00
    000000bc 00 00 2a 00 // [5] "http://schemas.android.com/apk/res/android"
    000000c0 68 00 74 00
    000000c4 74 00 70 00
    000000c8 3a 00 2f 00
    000000cc 2f 00 73 00
    000000d0 63 00 68 00
    000000d4 65 00 6d 00
    000000d8 61 00 73 00
    000000dc 2e 00 61 00
    000000e0 6e 00 64 00
    000000e4 72 00 6f 00
    000000e8 69 00 64 00
    000000ec 2e 00 63 00
    000000f0 6f 00 6d 00
    000000f4 2f 00 61 00
    000000f8 70 00 6b 00
    000000fc 2f 00 72 00
    00000100 65 00 73 00
    00000104 2f 00 61 00
    00000108 6e 00 64 00
    0000010c 72 00 6f 00
    00000110 69 00 64 00
    00000114 00 00 00 00 // [6] ""
    00000118 00 00 0c 00 // [7] "LinearLayout"
    0000011c 4c 00 69 00
    00000120 6e 00 65 00
    00000124 61 00 72 00
    00000128 4c 00 61 00
    0000012c 79 00 6f 00
    00000130 75 00 74 00
    00000134 00 00 08 00 // [8] "TextView"
    00000138 54 00 65 00
    0000013c 78 00 74 00
    00000140 56 00 69 00
    00000144 65 00 77 00
    00000148 00 00 1e 00 // [9] "Hello World, PendragonActivity"
    0000014c 48 00 65 00
    00000150 6c 00 6c 00
    00000154 6f 00 20 00
    00000158 57 00 6f 00
    0000015c 72 00 6c 00
    00000160 64 00 2c 00
    00000164 20 00 50 00
    00000168 65 00 6e 00
    0000016c 64 00 72 00
    00000170 61 00 67 00
    00000174 6f 00 6e 00
    00000178 41 00 63 00
    0000017c 74 00 69 00
    00000180 76 00 69 00
    00000184 74 00 79 00
    00000188 00 00 00 00
    ==================== [End of STRING_POOL]

    ...

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

Android Internals: Binary XML – Part One Example

The examples which follow are taken from the binary XML version of the main.xml layout file that was automatically generated as part of the same project the Resource table example was taken from.

1.0 main.xml

The contents of the generated main.xml layout file.

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >
    <TextView  
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="@string/hello"
        />
    </LinearLayout>

1.1 Binary main.xml

The result of running hexdump on the binary version main.xml layout file extracted from the application .apk file.


    0000000 03 00 08 00 c4 02 00 00 01 00 1c 00 84 01 00 00
    0000010 0a 00 00 00 00 00 00 00 00 00 00 00 44 00 00 00
    0000020 00 00 00 00 00 00 00 00 1a 00 00 00 36 00 00 00
    0000030 54 00 00 00 60 00 00 00 72 00 00 00 ca 00 00 00
    0000040 ce 00 00 00 ea 00 00 00 fe 00 00 00 0b 00 6f 00
    0000050 72 00 69 00 65 00 6e 00 74 00 61 00 74 00 69 00
    0000060 6f 00 6e 00 00 00 0c 00 6c 00 61 00 79 00 6f 00
    0000070 75 00 74 00 5f 00 77 00 69 00 64 00 74 00 68 00
    0000080 00 00 0d 00 6c 00 61 00 79 00 6f 00 75 00 74 00
    0000090 5f 00 68 00 65 00 69 00 67 00 68 00 74 00 00 00
    00000a0 04 00 74 00 65 00 78 00 74 00 00 00 07 00 61 00
    00000b0 6e 00 64 00 72 00 6f 00 69 00 64 00 00 00 2a 00
    00000c0 68 00 74 00 74 00 70 00 3a 00 2f 00 2f 00 73 00
    00000d0 63 00 68 00 65 00 6d 00 61 00 73 00 2e 00 61 00
    00000e0 6e 00 64 00 72 00 6f 00 69 00 64 00 2e 00 63 00
    00000f0 6f 00 6d 00 2f 00 61 00 70 00 6b 00 2f 00 72 00
    0000100 65 00 73 00 2f 00 61 00 6e 00 64 00 72 00 6f 00
    0000110 69 00 64 00 00 00 00 00 00 00 0c 00 4c 00 69 00
    0000120 6e 00 65 00 61 00 72 00 4c 00 61 00 79 00 6f 00
    0000130 75 00 74 00 00 00 08 00 54 00 65 00 78 00 74 00
    0000140 56 00 69 00 65 00 77 00 00 00 1e 00 48 00 65 00
    0000150 6c 00 6c 00 6f 00 20 00 57 00 6f 00 72 00 6c 00
    0000160 64 00 2c 00 20 00 50 00 65 00 6e 00 64 00 72 00
    0000170 61 00 67 00 6f 00 6e 00 41 00 63 00 74 00 69 00
    0000180 76 00 69 00 74 00 79 00 00 00 00 00 80 01 08 00
    0000190 18 00 00 00 c4 00 01 01 f4 00 01 01 f5 00 01 01
    00001a0 4f 01 01 01 00 01 10 00 18 00 00 00 02 00 00 00
    00001b0 ff ff ff ff 04 00 00 00 05 00 00 00 02 01 10 00
    00001c0 60 00 00 00 02 00 00 00 ff ff ff ff ff ff ff ff
    00001d0 07 00 00 00 14 00 14 00 03 00 00 00 00 00 00 00
    00001e0 05 00 00 00 00 00 00 00 ff ff ff ff 08 00 00 10
    00001f0 01 00 00 00 05 00 00 00 01 00 00 00 ff ff ff ff
    0000200 08 00 00 10 ff ff ff ff 05 00 00 00 02 00 00 00
    0000210 ff ff ff ff 08 00 00 10 ff ff ff ff 02 01 10 00
    0000220 60 00 00 00 07 00 00 00 ff ff ff ff ff ff ff ff
    0000230 08 00 00 00 14 00 14 00 03 00 00 00 00 00 00 00
    0000240 05 00 00 00 01 00 00 00 ff ff ff ff 08 00 00 10
    0000250 ff ff ff ff 05 00 00 00 02 00 00 00 ff ff ff ff
    0000260 08 00 00 10 fe ff ff ff 05 00 00 00 03 00 00 00
    0000270 09 00 00 00 08 00 00 03 09 00 00 00 03 01 10 00
    0000280 18 00 00 00 0b 00 00 00 ff ff ff ff ff ff ff ff
    0000290 08 00 00 00 03 01 10 00 18 00 00 00 0c 00 00 00
    00002a0 ff ff ff ff ff ff ff ff 07 00 00 00 01 01 10 00
    00002b0 18 00 00 00 0c 00 00 00 ff ff ff ff 04 00 00 00
    00002c0 05 00 00 00                                    
    00002c4

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

Android Internals: Resources – Part Ten: Complex Resource Entry Examples

Filed under: Android, Android Internals, Android Resources — Tags: , , — Simon Lewis @ 3:51 am

Complex Resource entries are used to represent array, attribute, plurals, and style Resources.

Terminology

To try and reduce the confusion resulting from the naming issues described here in what follows I am simply going to refer to instances of
struct ResTable_map as members.

1.0 Arrays

As the following examples show the distinction between array, integer-array, and string-array Resources only exists at the XML source level. Their binary
representations are identical which is why they are all stored in a single Type chunk.

1.1 Array

This array Resource definition

    <array name="example_array">
        <item>@array/example_integer_array</item>
        <item>@array/example_string_array</item>
    </array>

results in this complex Resource entry

    ...

    00000a10 10 00       // entry[0] size
    00000a12 01 00       // entry[0] flags
    00000a14 09 00 00 00 // entry[0] key [example_array]
    00000a18 00 00 00 00 // parent
    00000a1c 02 00 00 00 // count
    
    00000a20 00 00 00 02 // name
    00000a24 08 00       // size
    00000a26 00          // 0
    00000a27 01          // dataType
    00000a28 01 00 04 7f // data
    00000a2c 01 00 00 02 // name
    00000a30 08 00       // size
    00000a32 00          // 0
    00000a33 01          // dataType
    00000a34 02 00 04 7f // data

    ...

Each member represents an element of the array.

The name field of each member is the result of applying the macro Res_MAKEARRAY to the index of the element that it represents.

Res_MAKEARRAY() is defined as follows

    #define Res_MAKEARRAY(entry) (0x02000000 | (entry&0xFFFF))

(see frameworks/base/include/ResourceTypes.h line 216)

Each member has a data type of Res_value.TYPE_REFERENCE and the data is a Resource id.

1.2 Integer Array

This integer array Resource definition

    <integer-array name="example_integer_array">
        <item>1</item>
        <item>3</item>
        <item>5</item>
        <item>7</item>
        <item>11</item>
        <item>13</item>
        <item>17</item>
    </integer-array>

results in this complex Resource entry

    ...

    00000a38 10 00       // entry[1] size
    00000a3a 01 00       // entry[1] flags
    00000a3c 0a 00 00 00 // entry[1] key [example_integer_array]
    00000a40 00 00 00 00 // parent
    00000a44 07 00 00 00 // count

    00000a48 00 00 00 02 // name
    00000a4c 08 00       // size
    00000a4e 00          // 0
    00000a4f 10          // dataType
    00000a50 01 00 00 00 // data
    00000a54 01 00 00 02 // name
    00000a58 08 00       // size
    00000a5a 00          // 0
    00000a5b 10          // dataType
    00000a5c 03 00 00 00 // data
    00000a60 02 00 00 02 // name
    00000a64 08 00       // size
    00000a66 00          // 0
    00000a67 10          // dataType
    00000a68 05 00 00 00 // data
    00000a6c 03 00 00 02 // name
    00000a70 08 00       // size
    00000a72 00          // 0
    00000a73 10          // dataType
    00000a74 07 00 00 00 // data
    00000a78 04 00 00 02 // name
    00000a7c 08 00       // size
    00000a7e 00          // 0
    00000a7f 10          // dataType
    00000a80 0b 00 00 00 // data
    00000a84 05 00 00 02 // name
    00000a88 08 00       // size
    00000a8a 00          // 0
    00000a8b 10          // dataType
    00000a8c 0d 00 00 00 // data
    00000a90 06 00 00 02 // name
    00000a94 08 00       // size
    00000a96 00          // 0
    00000a97 10          // dataType
    00000a98 11 00 00 00 // data

    ...

The only difference between this example and the preceding one is that the data type of each member is Res_value.TYPE_INTEGER and the data is an integer.

1.3 String Array

This string array Resource definition

    <string-array name="example_string_array">
        <item>Viking</item>
        <item>North Utsire</item>
        <item>South Utsire</item>
        <item>Forties</item>
        <item>Cromarty</item>
        <item>Forth</item>
        <item>Tyne</item>
    </string-array>

results in this complex Resource entry

    ...

    00000a9c 10 00       // entry[2] size
    00000a9e 01 00       // entry[2] flags
    00000aa0 0b 00 00 00 // entry[2] key [example_string_array]
    00000aa4 00 00 00 00 // parent
    00000aa8 07 00 00 00 // count

    00000aac 00 00 00 02 // name
    00000ab0 08 00       // size
    00000ab2 00          // 0
    00000ab3 03          // dataType
    00000ab4 07 00 00 00 // data
    00000ab8 01 00 00 02 // name
    00000abc 08 00       // size
    00000abe 00          // 0
    00000abf 03          // dataType
    00000ac0 08 00 00 00 // data
    00000ac4 02 00 00 02 // name
    00000ac8 08 00       // size
    00000aca 00          // 0
    00000acb 03          // dataType
    00000acc 09 00 00 00 // data
    00000ad0 03 00 00 02 // name
    00000ad4 08 00       // size
    00000ad6 00          // 0
    00000ad7 03          // dataType
    00000ad8 0a 00 00 00 // data
    00000adc 04 00 00 02 // name
    00000ae0 08 00       // size
    00000ae2 00          // 0
    00000ae3 03          // dataType
    00000ae4 0b 00 00 00 // data
    00000ae8 05 00 00 02 // name
    00000aec 08 00       // size
    00000aee 00          // 0
    00000aef 03          // dataType
    00000af0 0c 00 00 00 // data
    00000af4 06 00 00 02 // name
    00000af8 08 00       // size
    00000afa 00          // 0
    00000afb 03          // dataType
    00000afc 0d 00 00 00 // data

    ...

The only difference between this example and the preceding one is that the data type of each member is Res_value.TYPE_STRING and the data is an index
into the StringPool chunk of the containing Table chunk.

2.0 Attributes

2.1 Attribute Types

Attribute types are defined by the following anonymous enum which nested in the definition of struct ResTable_map (see frameworks/base/include/ResourceTypes.h lines 1675-1714)

    enum {
        // No type has been defined for this attribute, use generic
        // type handling.  The low 16 bits are for types that can be
        // handled generically; the upper 16 require additional information
        // in the bag so can not be handled generically for TYPE_ANY.
        TYPE_ANY = 0x0000FFFF,

        // Attribute holds a references to another resource.
        TYPE_REFERENCE = 1<<0,

        // Attribute holds a generic string.
        TYPE_STRING = 1<<1,

        // Attribute holds an integer value.  ATTR_MIN and ATTR_MIN can
        // optionally specify a constrained range of possible integer values.
        TYPE_INTEGER = 1<<2,

        // Attribute holds a boolean integer.
        TYPE_BOOLEAN = 1<<3,

        // Attribute holds a color value.
        TYPE_COLOR = 1<<4,

        // Attribute holds a floating point value.
        TYPE_FLOAT = 1<<5,

        // Attribute holds a dimension value, such as "20px".
        TYPE_DIMENSION = 1<<6,

        // Attribute holds a fraction value, such as "20%".
        TYPE_FRACTION = 1<<7,

        // Attribute holds an enumeration.  The enumeration values are
        // supplied as additional entries in the map.
        TYPE_ENUM = 1<<16,

        // Attribute holds a bitmaks of flags.  The flag bit values are
        // supplied as additional entries in the map.
        TYPE_FLAGS = 1<<17
    };

2.2 Boolean Attribute

This attribute Resource definition

    <attr name="example_boolean_attribute" format="boolean"/>

results in this complex Resource entry

    ...
 
    0000073c 10 00       // entry[0] size
    0000073e 01 00       // entry[0] flags
    00000740 00 00 00 00 // entry[0] key [example_boolean_attribute]
    00000744 00 00 00 00 // parent
    00000748 01 00 00 00 // count

    0000074c 00 00 00 01 // name
    00000750 08 00       // size
    00000752 00          // 0
    00000753 10          // dataType
    00000754 08 00 00 00 // data
	
    ...

The entry has a single member which specifies the type of the attribute.

2.3 Enum Attribute

This attribute Resource definition

    <attr name="example_enum_attribute">
        <enum name="Humber" value="0"/>
        <enum name="Thames" value="1"/>
        <enum name="Dover"  value="2"/>
        <enum name="Wight"  value="3"/>
    </attr>

results in this complex Resource entry

    ...
 
    00000758 10 00       // entry[1] size
    0000075a 01 00       // entry[1] flags
    0000075c 01 00 00 00 // entry[1] key [example_enum_attribute]
    00000760 00 00 00 00 // parent
    00000764 05 00 00 00 // count

    00000768 00 00 00 01 // name
    0000076c 08 00       // size
    0000076e 00          // 0
    0000076f 10          // dataType
    00000770 00 00 01 00 // data
    00000774 00 00 05 7f // name
    00000778 08 00       // size
    0000077a 00          // 0
    0000077b 10          // dataType
    0000077c 00 00 00 00 // data
    00000780 01 00 05 7f // name
    00000784 08 00       // size
    00000786 00          // 0
    00000787 10          // dataType
    00000788 01 00 00 00 // data
    0000078c 02 00 05 7f // name
    00000790 08 00       // size
    00000792 00          // 0
    00000793 10          // dataType
    00000794 02 00 00 00 // data
    00000798 03 00 05 7f // name
    0000079c 08 00       // size
    0000079e 00          // 0
    0000079f 10          // dataType
    000007a0 03 00 00 00 // data

    ...

There are five members.

The first specifies the attribute type

The other four specify the enum members.

In each case

  • the name is the Resource id of the id Resource corresponding to the name attribute in the original XML, for example, Humber == 0x7f050000,

  • the data type is 0x10 (Res_value.TYPE_INTEGER) and

  • the data value is the value defined by the value attribute in the original XML.

2.4 Flags Attribute

This attribute Resource definition

    <attr name="exmple_flag_attribute">
        <flag name="Up"    value="0"/>
        <flag name="Down"  value="1"/>
        <flag name="Left"  value="2"/>
        <flag name="Right" value="3"/>
    </attr>

results in this complex Resource entry

    ...
 
    000007a4 10 00       // entry[2] size
    000007a6 01 00       // entry[2] flags
    000007a8 02 00 00 00 // entry[2] key [exmple_flag_attribute]
    000007ac 00 00 00 00 // parent
    000007b0 05 00 00 00 // count

    000007b4 00 00 00 01 // name
    000007b8 08 00       // size
    000007ba 00          // 0
    000007bb 10          // dataType
    000007bc 00 00 02 00 // data
    000007c0 04 00 05 7f // name
    000007c4 08 00       // size
    000007c6 00          // 0
    000007c7 10          // dataType
    000007c8 00 00 00 00 // data
    000007cc 05 00 05 7f // name
    000007d0 08 00       // size
    000007d2 00          // 0
    000007d3 10          // dataType
    000007d4 01 00 00 00 // data
    000007d8 06 00 05 7f // name
    000007dc 08 00       // size
    000007de 00          // 0
    000007df 10          // dataType
    000007e0 02 00 00 00 // data
    000007e4 07 00 05 7f // name
    000007e8 08 00       // size
    000007ea 00          // 0
    000007eb 10          // dataType
    000007ec 03 00 00 00 // data

    ...

There are five members.

The first specifies the attribute type

The other four specify the flags.

In each case

  • the name is the Resource id corresponding to the name attribute in the original XML, for example, Up == 0x7f050004,

  • the data type is 0x10 (Res_value.TYPE_INTEGER) and

  • the data value is the value defined by the value attribute in the original XML.

2.5 Integer Attribute

    <attr 
        name   = "example_integer_attribute" 
        format = "integer" 
        min    = "2" 
        max    = "7"/>
    ...
 
    000007f0 10 00       // entry[3] size
    000007f2 01 00       // entry[3] flags
    000007f4 03 00 00 00 // entry[3] key [example_integer_attribute]
    000007f8 00 00 00 00 // parent
    000007fc 03 00 00 00 // count

    00000800 00 00 00 01 // name
    00000804 08 00       // size
    00000806 00          // 0
    00000807 10          // dataType
    00000808 04 00 00 00 // data
    0000080c 01 00 00 01 // name
    00000810 08 00       // size
    00000812 00          // 0
    00000813 10          // dataType
    00000814 02 00 00 00 // data
    00000818 02 00 00 01 // name
    0000081c 08 00       // size
    0000081e 00          // 0
    0000081f 10          // dataType
    00000820 07 00 00 00 // data
	
    ...

The first specifies the attribute type

The second specifies the mimimum value

The second specifies the maximum value

3.0 Plurals

This plurals Resource definition

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <plurals name="plurals_example">
            <item quantity="one">once</item>
            <item quantity="two">twice</item>
            <item quantity="other">lots</item>
        </plurals>
    </resources>

results in this complex Resource entry

    ...

    00000f24 10 00       // entry[0] size
    00000f26 01 00       // entry[0] flags
    00000f28 24 00 00 00 // entry[0] key [plurals_example]
    00000f2c 00 00 00 00 // parent
    00000f30 03 00 00 00 // count

    00000f34 04 00 00 01 // name
    00000f38 08 00       // size
    00000f3a 00          // 0
    00000f3b 03          // dataType
    00000f3c 0f 00 00 00 // data
    00000f40 06 00 00 01 // name
    00000f44 08 00       // size
    00000f46 00          // 0
    00000f47 03          // dataType
    00000f48 0e 00 00 00 // data
    00000f4c 07 00 00 01 // name
    00000f50 08 00       // size
    00000f52 00          // 0
    00000f53 03          // dataType
    00000f54 10 00 00 00 // data
	
    ...

The three members correspond to the three items.

The names

  • 0x01000004

  • 0x01000006

  • 0x01000007

are the three constants

The data type of each member is Res_value.TYPE_STRING and the value is an index into the containing Table chunk’s StringPool chunk.

4.0 Styles

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <style name = "Text">
        </style>
        <style name="CustomText" parent="Text">
            <item name="android:textSize">20sp</item>
            <item name="android:textColor">#008</item>
        </style>
    </resources>
    ...

    00001018 00 00 00 00 // entry_index[0]
    0000101c 10 00 00 00 // entry_index[1]
    00001020 10 00       // entry[0] size
    00001022 01 00       // entry[0] flags
    00001024 27 00 00 00 // entry[0] key [Text]
    00001028 00 00 00 00 // parent
    0000102c 00 00 00 00 // count

    00001030 10 00       // entry[1] size
    00001032 01 00       // entry[1] flags
    00001034 28 00 00 00 // entry[1] key [CustomText]
    00001038 00 00 0d 7f // parent
    0000103c 02 00 00 00 // count

    00001040 95 00 01 01 // name
    00001044 08 00       // size
    00001046 00          // 0
    00001047 05          // dataType
    00001048 02 14 00 00 // data
    0000104c 98 00 01 01 // name
    00001050 08 00       // size
    00001052 00          // 0
    00001053 1f          // dataType
    00001054 88 00 00 ff // data

    ...

The first style which is defined simply to act as a parent for the second is empty and is represented by an empty Complex Resource entry.

The second style which inherits from the first is represented by a Complex Resource entry with two members.

The parent field specifies the Resource id of the parent style.

The name of each member is the Resource id specified by the name attribute in the original XML definition.

The value of each member represents the value specified by the corresponding item in the original XML definition.

5.0 Ids

Complex Resource entries are also used to represent id Resources in some circumstances.

Defining the enum and flags attributes above results in an additional id Typespec/Type chunk pair.

The Type chunk looks like this

    ...

    00000b38 05          // id
    00000b39 00          // 0
    00000b3a 00 00       // 0
    00000b3c 08 00 00 00 // entryCount
    00000b40 54 00 00 00 // entriesStart (address 00000b84)
    00000b44 20 00 00 00 // config: size
    00000b48 00 00 00 00 // config: imsi
    00000b4c 00 00 00 00 // config: locale
    00000b50 00 00 00 00 // config: screenType
    00000b54 00 00 00 00 // config: input
    00000b58 00 00 00 00 // config: screenSize
    00000b5c 00 00 00 00 // config: version
    00000b60 00 00 00 00 // config: screenConfig
    ++++++++++++++++++++

    00000b64 00 00 00 00 // entry_index[0]
    00000b68 10 00 00 00 // entry_index[1]
    00000b6c 20 00 00 00 // entry_index[2]
    00000b70 30 00 00 00 // entry_index[3]
    00000b74 40 00 00 00 // entry_index[4]
    00000b78 50 00 00 00 // entry_index[5]
    00000b7c 60 00 00 00 // entry_index[6]
    00000b80 70 00 00 00 // entry_index[7]
    00000b84 10 00       // entry[0] size
    00000b86 01 00       // entry[0] flags
    00000b88 0c 00 00 00 // entry[0] key [Humber]
    00000b8c 00 00 00 00 // parent
    00000b90 00 00 00 00 // count
    00000b94 10 00       // entry[1] size
    00000b96 01 00       // entry[1] flags
    00000b98 0d 00 00 00 // entry[1] key [Thames]
    00000b9c 00 00 00 00 // parent
    00000ba0 00 00 00 00 // count
    00000ba4 10 00       // entry[2] size
    00000ba6 01 00       // entry[2] flags
    00000ba8 0e 00 00 00 // entry[2] key [Dover]
    00000bac 00 00 00 00 // parent
    00000bb0 00 00 00 00 // count
    00000bb4 10 00       // entry[3] size
    00000bb6 01 00       // entry[3] flags
    00000bb8 0f 00 00 00 // entry[3] key [Wight]
    00000bbc 00 00 00 00 // parent
    00000bc0 00 00 00 00 // count
    00000bc4 10 00       // entry[4] size
    00000bc6 01 00       // entry[4] flags
    00000bc8 10 00 00 00 // entry[4] key [Up]
    00000bcc 00 00 00 00 // parent
    00000bd0 00 00 00 00 // count
    00000bd4 10 00       // entry[5] size
    00000bd6 01 00       // entry[5] flags
    00000bd8 11 00 00 00 // entry[5] key [Down]
    00000bdc 00 00 00 00 // parent
    00000be0 00 00 00 00 // count
    00000be4 10 00       // entry[6] size
    00000be6 01 00       // entry[6] flags
    00000be8 12 00 00 00 // entry[6] key [Left]
    00000bec 00 00 00 00 // parent
    00000bf0 00 00 00 00 // count
    00000bf4 10 00       // entry[7] size
    00000bf6 01 00       // entry[7] flags
    00000bf8 13 00 00 00 // entry[7] key [Right]
    00000bfc 00 00 00 00 // parent
    00000c00 00 00 00 00 // count
    ==================== [End of TYPE]

    ...

As you can see there are eight empty Complex Resource entries, one for each defined enum member and flag.

Note that this is not the only way that id Resources are represented. They can also be represented as Simple Resource entries with a boolean value of false.

September 20, 2011

Android Internals: Resources – Part Nine: Simple Resource Entry Examples

Filed under: Android, Android Internals, Android Resources — Tags: , , — Simon Lewis @ 4:34 am

1.0 Booleans

These boolean Resource definitions

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <bool name="example_false">false</bool>
        <bool name="example_true">true</bool>
    </resources>

result in these entries

    ...    

    00000c58 08 00       // entry[0] size
    00000c5a 00 00       // entry[0] flags
    00000c5c 14 00 00 00 // entry[0] key [example_false]
    00000c60 08 00       // size
    00000c62 00          // 0
    00000c63 12          // dataType [TYPE_BOOLEAN]
    00000c64 00 00 00 00 // data
    00000c68 08 00       // entry[1] size
    00000c6a 00 00       // entry[1] flags
    00000c6c 15 00 00 00 // entry[1] key [example_true]
    00000c70 08 00       // size
    00000c72 00          // 0
    00000c73 12          // dataType [TYPE_BOOLEAN]
    00000c74 ff ff ff ff // data
	
    ...

The entry values both have a value type of TYPE_BOOLEAN as you would expect, but true is represented by the value 0xFFFFFFFF rather than 1 as specified by the comment here.

2.0 Colours

Colour resources can be specified using four different formats and each format has a corresponding value type.

These color Resource definitions

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <color name="colour_rgb">#ABC</color>
        <color name="colour_argb">#ABCD</color>
        <color name="colour_rrggbb">#AABBCC</color>
        <color name="colour_aarrggbb">#AABBCCDD</color>
    </resources>

result in these entries

    ...

    00000cdc 08 00       // entry[0] size
    00000cde 00 00       // entry[0] flags
    00000ce0 16 00 00 00 // entry[0] key [colour_rgb]
    00000ce4 08 00       // size
    00000ce6 00          // 0
    00000ce7 1f          // dataType [TYPE_INT_COLOR_RGB4]
    00000ce8 cc bb aa ff // data
    00000cec 08 00       // entry[1] size
    00000cee 00 00       // entry[1] flags
    00000cf0 17 00 00 00 // entry[1] key [colour_argb]
    00000cf4 08 00       // size
    00000cf6 00          // 0
    00000cf7 1e          // dataType [TYPE_INT_COLOR_ARGB4]
    00000cf8 dd cc bb aa // data
    00000cfc 08 00       // entry[2] size
    00000cfe 00 00       // entry[2] flags
    00000d00 18 00 00 00 // entry[2] key [colour_rrggbb]
    00000d04 08 00       // size
    00000d06 00          // 0
    00000d07 1d          // dataType [TYPE_INT_COLOR_RGB8]
    00000d08 cc bb aa ff // data
    00000d0c 08 00       // entry[3] size
    00000d0e 00 00       // entry[3] flags
    00000d10 19 00 00 00 // entry[3] key [colour_aarrggbb]
    00000d14 08 00       // size
    00000d16 00          // 0
    00000d17 1c          // dataType [TYPE_INT_COLOR_ARGB8]
    00000d18 dd cc bb aa // data

    ...

Each entry value has a different type but all the colour values are encoded in the same way as standard 32-bit integers in ARGB format.

Other than making it possible to recover the textual representation of the Resource value as defined it is not clear why it is necessary to have four different colour value types

3.0 Dimensions

Dimensions can be specified in six different units.

These dimension Resource definitions

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <dimen name="dimension_mm">27mm</dimen>
        <dimen name="dimension_px">27px</dimen>
        <dimen name="dimension_in">27in</dimen>
        <dimen name="dimension_pt">27pt</dimen>
        <dimen name="dimension_dp">27dp</dimen>
        <dimen name="dimension_sp">27sp</dimen>
    </resources>

result in these entries

    ...

    00000d90 08 00       // entry[0] size
    00000d92 00 00       // entry[0] flags
    00000d94 1a 00 00 00 // entry[0] key [dimension_mm]
    00000d98 08 00       // size
    00000d9a 00          // 0
    00000d9b 05          // dataType [TYPE_DIMENSION]
    00000d9c 05 1b 00 00 // data
    00000da0 08 00       // entry[1] size
    00000da2 00 00       // entry[1] flags
    00000da4 1b 00 00 00 // entry[1] key [dimension_px]
    00000da8 08 00       // size
    00000daa 00          // 0
    00000dab 05          // dataType [TYPE_DIMENSION]
    00000dac 00 1b 00 00 // data
    00000db0 08 00       // entry[2] size
    00000db2 00 00       // entry[2] flags
    00000db4 1c 00 00 00 // entry[2] key [dimension_in]
    00000db8 08 00       // size
    00000dba 00          // 0
    00000dbb 05          // dataType [TYPE_DIMENSION]
    00000dbc 04 1b 00 00 // data
    00000dc0 08 00       // entry[3] size
    00000dc2 00 00       // entry[3] flags
    00000dc4 1d 00 00 00 // entry[3] key [dimension_pt]
    00000dc8 08 00       // size
    00000dca 00          // 0
    00000dcb 05          // dataType [TYPE_DIMENSION]
    00000dcc 03 1b 00 00 // data
    00000dd0 08 00       // entry[4] size
    00000dd2 00 00       // entry[4] flags
    00000dd4 1e 00 00 00 // entry[4] key [dimension_dp]
    00000dd8 08 00       // size
    00000dda 00          // 0
    00000ddb 05          // dataType [TYPE_DIMENSION]
    00000ddc 01 1b 00 00 // data
    00000de0 08 00       // entry[5] size
    00000de2 00 00       // entry[5] flags
    00000de4 1f 00 00 00 // entry[5] key [dimension_sp]
    00000de8 08 00       // size
    00000dea 00          // 0
    00000deb 05          // dataType [TYPE_DIMENSION]
    00000dec 02 1b 00 00 // data

    ...

Dimensions are the reverse of colours. All values have the same type (TYPE_DIMENSION) and the type of each dimension’s unit is encoded in the data.

The low-order 8 bits of the data value specify the units of the dimension as defined by the anonymous enum members

  • COMPLEX_UNIT_PX

  • COMPLEX_UNIT_DIP

  • COMPLEX_UNIT_SP

  • COMPLEX_UNIT_PT

  • COMPLEX_UNIT_IN

  • COMPLEX_UNIT_MM

The remaining 24 bits specify the value.

4.0 Fractions

Fractions can be specified using two different formats.

These resource definitions

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <fraction name="example_fraction_percent">14%</fraction>
        <fraction name="example_fraction_percent_p">28%p</fraction>
    </resources>

result in these entries

    ...

    00000e3c 00 00 00 00 // entry_index[0]
    00000e40 10 00 00 00 // entry_index[1]
    00000e44 08 00       // entry[0] size
    00000e46 00 00       // entry[0] flags
    00000e48 20 00 00 00 // entry[0] key [example_fraction_percent]
    00000e4c 08 00       // size
    00000e4e 00          // 0
    00000e4f 06          // dataType [TYPE_FRACTION]
    00000e50 30 85 eb 11 // data
    00000e54 08 00       // entry[1] size
    00000e56 00 00       // entry[1] flags
    00000e58 21 00 00 00 // entry[1] key [example_fraction_percent_p]
    00000e5c 08 00       // size
    00000e5e 00          // 0
    00000e5f 06          // dataType [TYPE_FRACTION]
    00000e60 31 0a d7 23 // data

    ...

Fractions are like dimensions. All values have the same type (TYPE_FRACTION) and the format is encoded in the data.

The low-order 4 bits of the data value specify the type of the fraction as defined by the anonymous enum members

  • COMPLEX_UNIT_FRACTION

  • COMPLEX_UNIT_FRACTION_PARENT

The remaining 28 bits specify the value as a floating point number.

5.0 Integers

Integer Resources can be specified as a decimal or hexidecimal value and there are corresponding value types.

These resource definitions

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
	    <integer name="example_decimal">1234</integer>
	    <integer name="example_hex">0x1234</integer>
    </resources>

result in these entries

    ...

    00000eb0 00 00 00 00 // entry_index[0]
    00000eb4 10 00 00 00 // entry_index[1]
    00000eb8 08 00       // entry[0] size
    00000eba 00 00       // entry[0] flags
    00000ebc 22 00 00 00 // entry[0] key [example_decimal]
    00000ec0 08 00       // size
    00000ec2 00          // 0
    00000ec3 10          // dataType [TYPE_INT_DEC]
    00000ec4 d2 04 00 00 // data
    00000ec8 08 00       // entry[1] size
    00000eca 00 00       // entry[1] flags
    00000ecc 23 00 00 00 // entry[1] key [example_hex]
    00000ed0 08 00       // size
    00000ed2 00          // 0
    00000ed3 11          // dataType [TYPE_INT_HEX]
    00000ed4 34 12 00 00 // data

    ...

The entry values have different types but unsurprisingly both integer values are encoded as standard 32-bit integers.

Other than making it possible to recover the textual representation of the Resource value as defined it is not clear why it is necessary to have two different integer value types

Older Posts »

Blog at WordPress.com.

%d bloggers like this: