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

这篇博客分享了如何在Android APK包的comment中动态写入信息并进行读取,详细介绍了实现过程和测试结果。通过在APK尾部写入注释信息,并在运行时动态读取,展示了在不改变APK功能的前提下,添加自定义信息的方法。附有源代码链接供读者参考。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我在博客上发表一些我的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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值