Android学习心得(18) --- 对APK包动态写入信息
来源:互联网 发布:哈工大人工智能产业园 编辑:程序博客网 时间:2024/05/19 15:21
我在博客上发表一些我的Android学习心得,希望对大家能有帮助。
这一篇我们讲述一下在APK包的comment中动态写入信息。
1、介绍
APK包的压缩方式是zip,所有和zip有着相同的文件结构,zip文件主要由三部分组成:压缩的文件内容源数据、压缩的目录源数据、目录结束标识结构。
目录结束标识结构存在在整个归档包的结尾,用来标记压缩的目录数据的结束,其结构如下:
我们可以在注释中添加一些我们自己的信息,添加后并不会影响APK的运行,而且可以实现在运行的时候动态的取出其中的信息。
2、实现
首先,我们使用一个未添加的APK,在010 Editor中进行打开查看,运行ZIP格式的模板
可以看到,当前这个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,查看是否插入成功
发现,插入成功。
插入的是qiuyu93422和000A是长度。
在app中测试读取comment信息:
我们将读取的字符串显示到TextView中:
成功!
4、源代码
最后我把源代码放出来,供大家参考一下。
JAVA插入APK源码:https://github.com/QyMars/InjectCommentInAPK
APK显示源码:https://github.com/QyMars/ShowCommentAPK
- Android学习心得(18) --- 对APK包动态写入信息
- Apk动态写入信息
- Android学习心得(12) --- APK签名keystore
- Android -- 一种动态为apk写入信息的方案
- Android解析apk包信息
- Android 减包 - 减少APK大小
- Android 减包 - 减少APK大小
- 一种动态为apk写入信息的方案
- 一种动态为apk写入信息的方案
- 一种动态为apk写入信息的方案
- 一种动态为apk写入信息的方案
- 获取Android的APK包签名信息
- Android 获取APK包的信息
- win/mac下反编译Android安装包-APK文件
- 查看apk包信息
- Android动态部署三:如何从插件apk中启动Activity(-)
- 自学Android历程->对Android应用APK包进行签名
- Android读取APK Package包的图标安装信息
- Ubuntu14.04桥接网络设置与SSH登陆
- 苹果自带的json解析NSJSONSerialization(序列化)
- 小白笔记------------------最小m段和问题
- <<金刚经>> 读书小记
- 显式锁ReentrantLock
- Android学习心得(18) --- 对APK包动态写入信息
- 数组操作
- C/C++输出格式运用
- CentOS7+JDK1.7
- 欢迎使用CSDN-markdown编辑器
- uva1608 non-boring sequences
- matlab中lsim的用法
- 至今互联网上收集最全的精品教育资源【转载+补充】
- Win7 Python2.7 安装教程