Android 解析resources.arsc文件

简介: 1). 文件格式resources.arsc文件格式.png2). 头部信息/** * Resource.arsc文件格式是由一系列的chunk构成,每一个chunk均包含ResChunk_header * * struct ...
1). 文件格式
img_c8eeb5781e65aca64973aa7a38eee7ce.png
resources.arsc文件格式.png
2). 头部信息
/**
 * Resource.arsc文件格式是由一系列的chunk构成,每一个chunk均包含ResChunk_header
 * 
 * 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;
};
 * 
 * @author mazaiting
 */
public class ResChunkHeader {
    /**
     * 当前这个chunk的类型
     */
    public short type;
    /**
     * 当前chunk的头部大小
     */
    public short headerSize;
    /**
     * 当前chunk的大小
     */
    public int size;
    
    /**
     * 获取Chunk Header所占字节数
     * @return
     */
    public int getHeaderSize() {
        return 2 + 2 + 4;
    }
    
    @Override
    public String toString(){
        return "type: " + Util.bytesToHexString(Util.int2Byte(type)) + ",headerSize: " + headerSize + ",size: " + size;
    }
    
}
3). 资源索引表的头部信息
/**
 * Resources.arsc文件的第一个结构是资源索引表头部
 * 描述Resources.arsc文件的大小和资源包数量
 * 
 * struct ResTable_header
{
    struct ResChunk_header header;

    // The number of ResTable_package structures.
    uint32_t packageCount;
};
 * 
 * @author mazaiting
 */
public class ResTableHeader {
    /**
     * 标准的Chunk头部信息格式
     */
    public ResChunkHeader header;
    /**
     * 被编译的资源包个数
     * Android 中一个apk可能包含多个资源包,默认情况下都只有一个就是应用的包名所在的资源包
     */
    public int packageCount;
    
    public ResTableHeader() {
        header = new ResChunkHeader();
    }
    
    /**
     * 获取当前Table Header所占字节数
     * @return
     */
    public int getHeaderSize() {
        return header.getHeaderSize() + 4;
    }
    
    @Override
    public String toString(){
        return "header:" + header.toString() + "\n" + "packageCount:"+packageCount;
    }
}
4). 资源项的值字符串资源池
/**
 * 资源索引表头部的是资源项的值字符串资源池,这个字符串资源池包含了所有的在资源包里面所定义的资源项的值字符串
 * 一个字符串可以对应多个ResStringPool_span和一个ResStringPool_ref
 * struct ResStringPool_header
{
    struct ResChunk_header header;

    // Number of strings in this pool (number of uint32_t indices that follow
    // in the data).
    uint32_t stringCount;

    // Number of style span arrays in the pool (number of uint32_t indices
    // follow the string indices).
    uint32_t styleCount;

    // Flags.
    enum {
        // If set, the string index is sorted by the string values (based
        // on strcmp16()).
        SORTED_FLAG = 1<<0,

        // String pool is encoded in UTF-8
        UTF8_FLAG = 1<<8
    };
    uint32_t flags;

    // Index from header of the string data.
    uint32_t stringsStart;

    // Index from header of the style data.
    uint32_t stylesStart;
};
 * 
 * @author mazaiting
 */
public class ResStringPoolHeader {
    /**
     * 排序标记
     */
    public final static int SORTED_FLAG = 1;
    /**
     * UTF-8编码标识
     */
    public final static int UTF8_FLAG = (1 << 8);
    /**
     * 标准的Chunk头部信息结构
     */
    public ResChunkHeader header;
    /**
     * 字符串的个数
     */
    public int stringCount;
    /**
     * 字符串样式的个数
     */
    public int styleCount;
    /**
     * 字符串的属性,可取值包括0x000(UTF-16),0x001(字符串经过排序),0x100(UTF-8)和他们的组合值
     */
    public int flags;
    /**
     * 字符串内容块相对于其头部的距离
     */
    public int stringsStart;
    /**
     * 字符串样式块相对于其头部的距离
     */
    public int stylesStart;
    
    public ResStringPoolHeader() {
        header = new ResChunkHeader();
    }
    
    /**
     * 获取当前String Pool Header所占字节数
     * @return
     */
    public int getHeaderSize() {
        return header.getHeaderSize() + 4 + 4 + 4 + 4 + 4;
    }
    
    @Override
    public String toString(){
        return "header: " + header.toString() + "\n" + "stringCount: " + stringCount + ",styleCount: " + styleCount 
                + ",flags: " + flags + ",stringStart: " + stringsStart + ",stylesStart: " + stylesStart;
    }
}
5). Package数据块
/**
 * Package数据块记录编译包的元数据
 * 
 * struct ResTable_package
{
    struct ResChunk_header header;

    // If this is a base package, its ID.  Package IDs start
    // at 1 (corresponding to the value of the package bits in a
    // resource identifier).  0 means this is not a base package.
    uint32_t id;

    // Actual name of this package, \0-terminated.
    uint16_t name[128];

    // Offset to a ResStringPool_header defining the resource
    // type symbol table.  If zero, this package is inheriting from
    // another base package (overriding specific values in it).
    uint32_t typeStrings;

    // Last index into typeStrings that is for public use by others.
    uint32_t lastPublicType;

    // Offset to a ResStringPool_header defining the resource
    // key symbol table.  If zero, this package is inheriting from
    // another base package (overriding specific values in it).
    uint32_t keyStrings;

    // Last index into keyStrings that is for public use by others.
    uint32_t lastPublicKey;

    uint32_t typeIdOffset;
};
 * 
 * Package数据块的整体结构
 * String Pool
 * Type String Pool
 * Key String Pool
 * Type Specification
 * Type Info
 * 
 * @author mazaiting
 */
public class ResTablePackage {
    /**
     * Chunk的头部信息数据结构
     */
    public ResChunkHeader header;
    /**
     * 包的ID,等于package id,一般用户包的值Package Id为0X7F,系统资源包Pacage Id为0X01
     * 这个值会在构建public.xml中的id值时用到
     */
    public int id;
    /**
     * 包名
     */
    public char[] name = new char[128];
    /**
     * 类型字符串资源池相对头部的偏移
     */
    public int typeStrings;
    /**
     * 最后一个到处的public类型字符串在类型字符串资源池中的索引,目前这个值设置为类型字符串资源池的元素格尔书
     */
    public int lastPublicType;
    /**
     * 资源项名称字符串相对头部的偏移
     */
    public int keyStrings;
    /**
     * 最后一个导出的Public资源项名称字符串在资源项名称字符串资源池中的索引,目前这个值设置为资源项名称字符串资源池的元素个数
     */
    public int lastPublicKey;
    
    public ResTablePackage() {
        header = new ResChunkHeader();
    }
    
    @Override
    public String toString(){
        return "header: " + header.toString() + "\n" + ",id= " + id + ",name: " + name.toString() + 
                ",typeStrings:" + typeStrings + ",lastPublicType: " + lastPublicType + ",keyStrings: " + keyStrings 
                + ",lastPublicKey: " + lastPublicKey;
    }
}
6). 类型规范数据块
/**
 * 类型规范数据块用来描述资源项的配置差异性。通过这个差异性,我们可以知道每个资源项的配置状况。
 * 知道了一个资源项的配置状况之后,Android资源管理框架在检测到设备的配置信息发生变化之后,就
 * 可以知道是否需要重新加载该资源项。类型规范数据块是按照类型来组织的,即每一种类型都对应有一个
 * 类型规范数据块。
 * 
 * struct ResTable_typeSpec
{
    struct ResChunk_header header;

    // The type identifier this chunk is holding.  Type IDs start
    // at 1 (corresponding to the value of the type bits in a
    // resource identifier).  0 is invalid.
    uint8_t id;
    
    // Must be 0.
    uint8_t res0;
    // Must be 0.
    uint16_t res1;
    
    // Number of uint32_t entry configuration masks that follow.
    uint32_t entryCount;

    enum {
        // Additional flag indicating an entry is public.
        SPEC_PUBLIC = 0x40000000
    };
};
 * 
 * @author mazaiting
 */
public class ResTableTypeSpec {
    /**
     * SPEC公共常量
     */
    public final static int SPEC_PUBLIC = 0x40000000;
    /**
     * Chunk的头部信息结构
     */
    public ResChunkHeader header;
    /**
     * 标识资源的Type ID,Type ID是指资源的类型ID。资源的类型有animator、anim、color、drawable、layout、menu、raw、string和xml等等若干种,每一种都会被赋予一个ID。
     */
    public byte id;
    /**
     * 保留,始终为0
     */
    public byte res0;
    /**
     * 保留,始终为0
     */
    public short res1;
    /**
     * 本类型资源项个数,即名称相同的资源项的个数
     */
    public int entryCount;
    
    public ResTableTypeSpec() {
        header = new ResChunkHeader();
    }
    
    @Override
    public String toString(){
        return "header: " + header.toString() + ",id: " + id + ",res0: " + res0 + 
                ",res1: " + res1 + ",entryCount: " + entryCount;
    }

}
7). 资源类型项数据块
/**
 * 类型资源项数据块用来描述资源项的具体信息,可以知道每一个资源项的名称、值和配置等信息。
 * 类型资源项数据同样是按照类型和配置来组织的,即一个具有n个配置的类型一共对应有n个类型
 * 资源项数据块。
 * 
 * struct ResTable_type
{
    struct ResChunk_header header;

    enum {
        NO_ENTRY = 0xFFFFFFFF
    };
    
    // The type identifier this chunk is holding.  Type IDs start
    // at 1 (corresponding to the value of the type bits in a
    // resource identifier).  0 is invalid.
    uint8_t id;
    
    // Must be 0.
    uint8_t res0;
    // Must be 0.
    uint16_t res1;
    
    // Number of uint32_t entry indices that follow.
    uint32_t entryCount;

    // Offset from header where ResTable_entry data starts.
    uint32_t entriesStart;
    
    // Configuration this collection of entries is designed for.
    ResTable_config config;
};
 * 
 * @author mazaiting
 */
public class ResTableType {
    /**
     * NO_ENTRY常量
     */
    public final static int NO_ENTRY = 0xFFFFFFFF;
    /**
     * Chunk的头部信息结构
     */
    public ResChunkHeader header;
    /**
     * 标识资源的Type ID
     */
    public byte id;
    /**
     * 保留,始终为0
     */
    public byte res0;
    /**
     * 保留,始终为0
     */
    public short res1;
    /**
     * 本类型资源项个数,指名称相同的资源项的个数
     */
    public int entryCount;
    /**
     * 资源项数组块相对头部的偏移值
     */
    public int entriesStart;
    /**
     * 指向一个ResTable_config,用来描述配置信息,地区,语言,分辨率等
     */
    public ResTableConfig resConfig;
    
    public ResTableType() {
        header = new ResChunkHeader();
        resConfig = new ResTableConfig();
    }
    
    /**
     * 获取当前资源类型所占的字节数
     * @return
     */
    public int getSize() {
        return header.getHeaderSize() + 1 + 1 + 2 + 4 + 4;
    }
    
    @Override
    public String toString(){
        return "header: " + header.toString() + ",id: " + id + ",res0: " + res0 + ",res1: " + res1 + 
                ",entryCount: " + entryCount + ",entriesStart: " + entriesStart;
    }
 
}
8). 代码解析
public class ParseResourceMain {
//  private final static String FILE_PATH = "res/source.apk";
    private final static String FILE_PATH = "res/resources.arsc";
    public static void main(String[] args) {
        
//      byte[] arscArray = getArscFromApk(FILE_PATH);
        byte[] arscArray = getArscFromFile(FILE_PATH); 
        
        System.out.println("parse restable header ...");
        ParseResourceUtil.parseResTableHeaderChunk(arscArray);
        System.out.println("===================================");
        System.out.println();

        System.out.println("parse resstring pool chunk  ...");
        ParseResourceUtil.parseResStringPoolChunk(arscArray);
        System.out.println("===================================");
        System.out.println();
        
        System.out.println("parse package chunk ...");
        ParseResourceUtil.parsePackage(arscArray);
        System.out.println("===================================");
        System.out.println();
        
        System.out.println("parse typestring pool chunk ...");
        ParseResourceUtil.parseTypeStringPoolChunk(arscArray);
        System.out.println("===================================");
        System.out.println();
        
        System.out.println("parse keystring pool chunk ...");
        ParseResourceUtil.parseKeyStringPoolChunk(arscArray);
        System.out.println("===================================");
        System.out.println();
        
        /**
         * 解析正文内容
         * 正文内容就是ResValue值,也就是开始构建public.xml中的条目信息,和类型的分离不同的xml文件
         */
        int resCount = 0;
        while (!ParseResourceUtil.isEnd(arscArray.length)) {
            resCount++;
            boolean isSpec = ParseResourceUtil.isTypeSpec(arscArray);
            if (isSpec) {
                System.out.println("parse restype spec chunk ...");
                ParseResourceUtil.parseResTypeSpec(arscArray);
                System.out.println("===================================");
                System.out.println();
            } else {                
                System.out.println("parse restype info chunk ...");
                ParseResourceUtil.parseResTypeInfo(arscArray);
                System.out.println("===================================");
                System.out.println();
            }
        }
        System.out.println("res count: " + resCount);
        
    }
    
    /**
     * 从文件中获取resouces.arsc
     * @param filePath 文件路径
     * @return
     */
    private static byte[] getArscFromFile(String filePath) {
        byte[] srcByte = null;
        InputStream is = null;
        ByteArrayOutputStream baos = null;
        try {
            is = new FileInputStream(filePath);
            baos = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int len = 0;
            while ((len = is.read(buffer)) != -1) {
                baos.write(buffer, 0, len);             
            }
            srcByte = baos.toByteArray();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
                baos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return srcByte;
    }

    /**
     * 从APK中获取resources.arsc文件
     * @param filePath 文件路径
     * @return resources.arsc文件二进制数据
     */
    private static byte[] getArscFromApk(String filePath) {
        byte[] srcByte = null;
        ZipFile zipFile = null;
        InputStream is = null;
        ByteArrayOutputStream baos = null;
        try {
            zipFile = new ZipFile(filePath);
            ZipEntry entry = zipFile.getEntry("resources.arsc");
            is = zipFile.getInputStream(entry);
            baos = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int len = 0;
            while ((len = is.read(buffer)) != -1) {
                baos.write(buffer, 0, len);             
            }
            srcByte = baos.toByteArray();
            zipFile.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
                baos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return srcByte;
    }

}

参考文章

Android逆向之旅---解析编译之后的Resource.arsc文件格式

代码下载

目录
相关文章
|
5月前
|
数据采集 监控 API
告别手动埋点!Android 无侵入式数据采集方案深度解析
传统的Android应用监控方案需要开发者在代码中手动添加埋点,不仅侵入性强、工作量大,还难以维护。本文深入探讨了基于字节码插桩技术的无侵入式数据采集方案,通过Gradle插件 + AGP API + ASM的技术组合,实现对应用性能、用户行为、网络请求等全方位监控,真正做到零侵入、易集成、高稳定。
695 63
|
10月前
|
Android开发 开发者
Android自定义View之不得不知道的文件attrs.xml(自定义属性)
本文详细介绍了如何通过自定义 `attrs.xml` 文件实现 Android 自定义 View 的属性配置。以一个包含 TextView 和 ImageView 的 DemoView 为例,讲解了如何使用自定义属性动态改变文字内容和控制图片显示隐藏。同时,通过设置布尔值和点击事件,实现了图片状态的切换功能。代码中展示了如何在构造函数中解析自定义属性,并通过方法 `setSetting0n` 和 `setbackeguang` 实现功能逻辑的优化与封装。此示例帮助开发者更好地理解自定义 View 的开发流程与 attrs.xml 的实际应用。
284 2
Android自定义View之不得不知道的文件attrs.xml(自定义属性)
|
10月前
|
Java Android开发
Android studio中build.gradle文件简单介绍
本文解析了Android项目中build.gradle文件的作用,包括jcenter仓库配置、模块类型定义、包名设置及依赖管理,涵盖本地、库和远程依赖的区别。
855 19
|
移动开发 安全 Java
Android历史版本与APK文件结构
通过以上内容,您可以全面了解Android的历史版本及其主要特性,同时掌握APK文件的结构和各部分的作用。这些知识对于理解Android应用的开发和发布过程非常重要,也有助于在实际开发中进行高效的应用管理和优化。希望这些内容对您的学习和工作有所帮助。
1434 83
|
9月前
|
安全 Java Android开发
为什么大厂要求安卓开发者掌握Kotlin和Jetpack?深度解析现代Android开发生态优雅草卓伊凡
为什么大厂要求安卓开发者掌握Kotlin和Jetpack?深度解析现代Android开发生态优雅草卓伊凡
405 0
为什么大厂要求安卓开发者掌握Kotlin和Jetpack?深度解析现代Android开发生态优雅草卓伊凡
|
10月前
|
存储 XML Java
Android 文件数据储存之内部储存 + 外部储存
简介:本文详细介绍了Android内部存储与外部存储的使用方法及核心原理。内部存储位于手机内存中,默认私有,适合存储SharedPreferences、SQLite数据库等重要数据,应用卸载后数据会被清除。外部存储包括公共文件和私有文件,支持SD卡或内部不可移除存储,需申请权限访问。文章通过代码示例展示了如何保存、读取、追加、删除文件以及将图片保存到系统相册的操作,帮助开发者理解存储机制并实现相关功能。
2475 2
|
12月前
|
XML JavaScript Android开发
【Android】网络技术知识总结之WebView,HttpURLConnection,OKHttp,XML的pull解析方式
本文总结了Android中几种常用的网络技术,包括WebView、HttpURLConnection、OKHttp和XML的Pull解析方式。每种技术都有其独特的特点和适用场景。理解并熟练运用这些技术,可以帮助开发者构建高效、可靠的网络应用程序。通过示例代码和详细解释,本文为开发者提供了实用的参考和指导。
462 15
|
12月前
|
监控 Shell Linux
Android调试终极指南:ADB安装+多设备连接+ANR日志抓取全流程解析,覆盖环境变量配置/多设备调试/ANR日志分析全流程,附Win/Mac/Linux三平台解决方案
ADB(Android Debug Bridge)是安卓开发中的重要工具,用于连接电脑与安卓设备,实现文件传输、应用管理、日志抓取等功能。本文介绍了 ADB 的基本概念、安装配置及常用命令。包括:1) 基本命令如 `adb version` 和 `adb devices`;2) 权限操作如 `adb root` 和 `adb shell`;3) APK 操作如安装、卸载应用;4) 文件传输如 `adb push` 和 `adb pull`;5) 日志记录如 `adb logcat`;6) 系统信息获取如屏幕截图和录屏。通过这些功能,用户可高效调试和管理安卓设备。
|
Java API 数据处理
深潜数据海洋:Java文件读写全面解析与实战指南
通过本文的详细解析与实战示例,您可以系统地掌握Java中各种文件读写操作,从基本的读写到高效的NIO操作,再到文件复制、移动和删除。希望这些内容能够帮助您在实际项目中处理文件数据,提高开发效率和代码质量。
478 4
|
Serverless 对象存储 人工智能
智能文件解析:体验阿里云多模态信息提取解决方案
在当今数据驱动的时代,信息的获取和处理效率直接影响着企业决策的速度和质量。然而,面对日益多样化的文件格式(文本、图像、音频、视频),传统的处理方法显然已经无法满足需求。
503 4
智能文件解析:体验阿里云多模态信息提取解决方案

热门文章

最新文章

推荐镜像

更多