Android学习心得(18) --- 对APK包动态写入信息

来源:互联网 发布:哈工大人工智能产业园 编辑:程序博客网 时间:2024/05/19 15:21

我在博客上发表一些我的Android学习心得,希望对大家能有帮助。
这一篇我们讲述一下在APK包的comment中动态写入信息。


1、介绍

APK包的压缩方式是zip,所有和zip有着相同的文件结构,zip文件主要由三部分组成:压缩的文件内容源数据、压缩的目录源数据、目录结束标识结构。
目录结束标识结构存在在整个归档包的结尾,用来标记压缩的目录数据的结束,其结构如下:
123
我们可以在注释中添加一些我们自己的信息,添加后并不会影响APK的运行,而且可以实现在运行的时候动态的取出其中的信息。


2、实现

首先,我们使用一个未添加的APK,在010 Editor中进行打开查看,运行ZIP格式的模板
1
可以看到,当前这个APK中注释信息长度为0,则没有注释信息。

下面,我们编写java程序,将注释信息添加到其中
注意:注释信息添加的格式是:(注释信息,注释信息长度)

首先,我们将需要写入的注释信息写入到APK文件尾部的comment中:
这是写入方法:将comment信息和comment长度写入APK尾部

private static void writeAPK(File file, String comment) {    // TODO Auto-generated method stub    ZipFile zipFile = null;    ByteArrayOutputStream outputStream = null;    RandomAccessFile accessFile = null;    try {        zipFile = new ZipFile(file);        String zipComment = zipFile.getComment();        // 判断是否包含comment信息        if(zipComment != null) {            // 如果有 则返回            return ;        }        byte[] bytecomment = comment.getBytes();        outputStream = new ByteArrayOutputStream();        // 写入comment和长度        outputStream.write(bytecomment);        outputStream.write(Util.short2Byte((short) bytecomment.length));        byte[] commentdata = outputStream.toByteArray();        accessFile = new RandomAccessFile(file, "rw");        accessFile.seek(file.length() - 2);         // comment长度是short类型        // 重新写入comment长度,注意Android apk文件使用的是ByteOrder.LITTLE_ENDIAN(小端序);        accessFile.write(commentdata);        accessFile.close();        System.out.println("插入完成!");    } catch (Exception e) {        // TODO: handle exception    } finally {        try {            if(zipFile != null) {                zipFile.close();            }            if(outputStream != null) {                outputStream.close();            }            if(accessFile != null) {                accessFile.close();            }        } catch(Exception e) {        }    }}

既然已经插入成功,下面在运行该APK时动态读取comment信息,然后显示出来
这个是apk中读取代码:

public String readAPK(File file) {        byte[] bytes = null;        try {            RandomAccessFile accessFile = new RandomAccessFile(file, "r");            long index = accessFile.length();            bytes = new byte[2];            index = index - bytes.length;            accessFile.seek(index);            accessFile.readFully(bytes);            int contentLength = stream2Short(bytes, 0);            bytes = new byte[contentLength];            index = index - bytes.length;            accessFile.seek(index);            accessFile.readFully(bytes);            String a = new String(bytes, "utf-8");            Log.d("Hello", a);            return a;        } catch (FileNotFoundException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        }        return null;    }

3、测试

测试插入程序:
在这次运行例子中,我写入的信息是qiuyu93422,查看是否插入成功
21
发现,插入成功。
222
插入的是qiuyu93422和000A是长度。


在app中测试读取comment信息:

我们将读取的字符串显示到TextView中:
123123123
成功!


4、源代码

最后我把源代码放出来,供大家参考一下。
JAVA插入APK源码:https://github.com/QyMars/InjectCommentInAPK
APK显示源码:https://github.com/QyMars/ShowCommentAPK

0 0
原创粉丝点击