通过aapt查看apk资源

通过aapt dump可以查看apk的各种资源,包括名称,类型,以及对应的值。下面看看如何查看资源类型,值等信息
aapt d resources your.apk
每一种资源都有三个参数:
type: 资源类型
configCount: 支持的配置个数
entryCount: 一共有多少个资源

以下是几个具体例子:

type 0 configCount=1 entryCount=280
  spec resource 0x7f010000 com.example.demo:attr/drawerArrowStyle: flags=0x00000000
config (default): //0x7f010000是资源id, attr指定的资源类型
  resource 0x7f010000 com.example.demo:attr/drawerArrowStyle: <bag>

type 1 configCount=12 entryCount=373
  spec resource 0x7f020000 com.example.demo:drawable/abc_ab_share_pack_mtrl_alpha: flags=0x00000100
config (default): //默认配置下的值类型和值,t=0x03是值类型, d=0x00000005是值
  resource 0x7f020002 com.example.demo:drawable/abc_btn_borderless_material: t=0x03 d=0x00000005 (s=0x0008 r=0x00)
config v21: //另一种配置下的取值,资源类型和名称相同,但是取值可以不同
  resource 0x7f020001 com.example.demo:drawable/abc_action_bar_item_background_material: t=0x03 d=0x000001b5 (s=0x0008 r=0x00)

type 2 configCount=2 entryCount=252
  spec resource 0x7f030000 com.example.demo:layout/abc_action_bar_title_item: flags=0x00000000
config (default):
  resource 0x7f030000 com.example.demo:layout/abc_action_bar_title_item: t=0x03 d=0x00000189 (s=0x0008 r=0x00)

值类型:

前面的给出了几个例子,其中都有t参数,t代表的是取值类型,确定了限制值d的具体意义。t共有如下类型:

字符串类型

字符串类型的值也是一个整数,其值是字符串资源池中字符串id,通过此id就能从字符串资源池中找到具体的字符串值. 如:

/** The <var>string</var> field holds string data.  In addition, if
     *  <var>data</var> is non-zero then it is the string block
     *  index of the string and <var>assetCookie</var> is the set of
     *  assets the string came from.
 */
public static final int TYPE_STRING = 0x03;
config v23:
resource 0x7f0e007a com.example.demo:color/abc_btn_colored_borderless_text_material: 
t=0x03 d=0x000001b7 (s=0x0008 r=0x00)

d=0x00000167转换为十进制就是439,对应的字符串资源可以通过
aapt d strings your.apk查看

String #439: res/color-v23/abc_btn_colored_borderless_text_material.xml

可以看到这是一个文件的相对路径,说明这个color资源是用xml文件定义的,系统在读取资源的时候就根据这个路径找到对应的文件

引用类型

引用类型的值d指向的一定是另外一个资源的id

/** The <var>data</var> field holds a resource identifier. */
public static final int TYPE_REFERENCE = 0x01;
resource 0x7f020172 com.example.demo:drawable/arrow: t=0x01 
d=0x10020068 (s=0x0008 r=0x00)

d=0x010020068, 0x10说明这是一个android sdk带的资源,读取这个资源时要递归找到另外一个资源

属性类型

属性类型的值,表示这个资源是有一个attr的取值确定的,通常attr的取值会在不同的theme中具有不同的值,在代码中,用如下方式定义一个值由某个属性决定:

<item name="arrow" type="drawable" format="reference">@android:drawable/arrow_right</item>
/** The <var>data</var> field holds an attribute resource
 *  identifier (referencing an attribute in the current theme
 *  style, not a resource entry).
 */
public static final int TYPE_ATTRIBUTE = 0x02;
resource 0x7f020172 com.example.demo:drawable/arrow: t=0x01
d=0x010020068 (s=0x0008 r=0x00)
Float类型

float类型的值就是对应的IEEE 754的float值

/** The <var>data</var> field holds an IEEE 754 floating point number. */
public static final int TYPE_FLOAT = 0x04;
resource 0x7f090061 com.example.demo:dimen/abc_disabled_alpha_material_dark:
t=0x04
d=0x3e99999a (s=0x0008 r=0x00)
颜色值类型

颜色类型直接描述了颜色的整数取值,根据不同的颜色表示方法,分为以下几类:

/** Identifies the start of integer values that were specified as
     *  color constants (starting with '#'). */
    public static final int TYPE_FIRST_COLOR_INT = 0x1c;

    /** The <var>data</var> field holds a color that was originally
     *  specified as #aarrggbb. */
    public static final int TYPE_INT_COLOR_ARGB8 = 0x1c;
    /** The <var>data</var> field holds a color that was originally
     *  specified as #rrggbb. */
    public static final int TYPE_INT_COLOR_RGB8 = 0x1d;
    /** The <var>data</var> field holds a color that was originally
     *  specified as #argb. */
    public static final int TYPE_INT_COLOR_ARGB4 = 0x1e;
    /** The <var>data</var> field holds a color that was originally
     *  specified as #rgb. */
    public static final int TYPE_INT_COLOR_RGB4 = 0x1f;

    /** Identifies the end of integer values that were specified as color
     *  constants. */
    public static final int TYPE_LAST_COLOR_INT = 0x1f;
resource 0x7f0e0006 com.example.demo:color/action_bar_sub_title_dark:
t=0x1c d=0x66ffffff (s=0x0008 r=0x00)
整数值类型

整数值类型同样直接描述了资源的取值,根据不同的表现形式分为几类:

/** Identifies the start of plain integer values.  Any type value
     *  from this to {@link #TYPE_LAST_INT} means the
     *  <var>data</var> field holds a generic integer value. */
    public static final int TYPE_FIRST_INT = 0x10;

    /** The <var>data</var> field holds a number that was
     *  originally specified in decimal. */
    public static final int TYPE_INT_DEC = 0x10;
    /** The <var>data</var> field holds a number that was
     *  originally specified in hexadecimal (0xn). */
    public static final int TYPE_INT_HEX = 0x11;
    /** The <var>data</var> field holds 0 or 1 that was originally
     *  specified as "false" or "true". */
    public static final int TYPE_INT_BOOLEAN = 0x12;
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容