一种动态为apk写入信息的方案

移动开发
如果用户在当前页面下载了应用,安装之后直接跳转到刚才浏览的界面,不仅可以将这一部分流量引回客户端,还可以让用户获得完整的用户体验。下面提出一种方案来满足这个业务需求。

背景

我们在日常使用应用可能会遇到以下场景。

场景1: 用户浏览h5页面时看到一个页面,下载安装app后启动会来到首页而不是用户之前浏览的页面,造成使用场景的割裂。

场景2: 用户通过二维码把一个页面分享出去,没有装猫客的用户如果直接安装启动之后无法回到分享的页面。

如果用户在当前页面下载了应用,安装之后直接跳转到刚才浏览的界面,不仅可以将这一部分流量引回客户端,还可以让用户获得完整的用户体验。下面提出一种方案来满足这个业务需求。
原理

android使用的apk包的压缩方式是zip,与zip有相同的文件结构,在zip的Central directory file header中包含一个File comment区域,可以存放一些数据。File comment是zip文件如果可以正确的修改这个部分,就可以在不破坏压缩包、不用重新打包的的前提下快速的给apk文件写入自己想要的数据。
comment是在Central directory file header末尾储存的,可以将数据直接写在这里,下表是header末尾的结构。

 

由于数据是不确定的,我们无法知道comment的长度,从表中可以看到zip定义comment的长度的位置在comment之前,所以无法从zip中直接获取comment的长度。这里我们需要自定义comment的长度,在自定义comment内容的后面添加一个区域储存comment的长度,结构如下图。

 

这里可以将一个固定的结构写在comment中,然后根据自定义的长度分区获取每个部分的内容,还可以添加其它数据,如校验码、版本等。
实现
1.将数据写入comment

这一部分可以在本地进行,需要定义一个长度为2的byte[]来储存comment的长度,直接使用Java的api就可以把comment和comment的长度写到apk的末尾,代码如下。

  1. public static void writeApk(File file, String comment) {  
  2.     ZipFile zipFile = null;  
  3.     ByteArrayOutputStream outputStream = null;  
  4.     RandomAccessFile accessFile = null;  
  5.     try {  
  6.         zipFile = new ZipFile(file);  
  7.         String zipComment = zipFile.getComment();  
  8.         if (zipComment != null) {  
  9.             return;  
  10.         }  
  11.  
  12.         byte[] byteComment = comment.getBytes();  
  13.         outputStream = new ByteArrayOutputStream();  
  14.  
  15.         outputStream.write(byteComment);  
  16.         outputStream.write(short2Stream((short) byteComment.length));  
  17.  
  18.         byte[] data = outputStream.toByteArray();  
  19.  
  20.         accessFile = new RandomAccessFile(file, "rw");  
  21.         accessFile.seek(file.length() - 2);  
  22.         accessFile.write(short2Stream((short) data.length));  
  23.         accessFile.write(data);  
  24.     } catch (IOException e) {  
  25.         e.printStackTrace();  
  26.     } finally {  
  27.         try {  
  28.             if (zipFile != null) {  
  29.                 zipFile.close();  
  30.             }  
  31.             if (outputStream != null) {  
  32.                 outputStream.close();  
  33.             }  
  34.             if (accessFile != null) {  
  35.                 accessFile.close();  
  36.             }  
  37.         } catch (Exception e) {  
  38.  
  39.         }  
  40.  
  41.     }  
  42. }  
  43.  

2.读取apk包中的comment数据

首先获取apk的路径,通过context中的getPackageCodePath()方法就可以获取,代码如下。

  1. public static String getPackagePath(Context context) {  
  2.     if (context != null) {  
  3.         return context.getPackageCodePath();  
  4.     }  
  5.     return null;  
  6. }  
  7.  

获取路径之后就可以读取comment的内容了,这里不能直接使用ZipFile中的getComment()方法直接获取comment,因为这个方法是Java7中的方法,在android4.4之前是不支持Java7的,所以我们需要自己去读取apk文件中的comment。首先根据之前自定义的结构,先读取写在***的comment的长度,根据这个长度,才可以获取真正comment的内容,代码如下。

  1. public static String readApk(File file) {  
  2.     byte[] bytes = null;  
  3.     try {  
  4.         RandomAccessFile accessFile = new RandomAccessFile(file, "r");  
  5.         long index = accessFile.length();  
  6.  
  7.         bytes = new byte[2];  
  8.         index = index - bytes.length;  
  9.         accessFile.seek(index);  
  10.         accessFile.readFully(bytes);  
  11.  
  12.         int contentLength = stream2Short(bytes, 0);  
  13.  
  14.         bytes = new byte[contentLength];  
  15.         index = index - bytes.length;  
  16.         accessFile.seek(index);  
  17.         accessFile.readFully(bytes);  
  18.  
  19.         return new String(bytes, "utf-8");  
  20.     } catch (FileNotFoundException e) {  
  21.         e.printStackTrace();  
  22.     } catch (IOException e) {  
  23.         e.printStackTrace();  
  24.     }  
  25.     return null;  
  26. }  
  27.  

这里的stream2Short()和short2Stream()参考了MultiChannelPackageTool中的方法。
测试

在生成apk后,调用下面的代码写入我们想要的数据,

  1. File file = new File("/Users/zhaolin/app-debug.apk");  
  2. writeApk(file, "test comment"); 

安装这个apk之后运行,让comment显示在屏幕上,运行结果如下。

运行结果符合预期,安装包也没有被破坏,可以正常安装。
结论

通过修改comment将数据传递给APP的方案是可行的,由于是修改apk自有的数据,并不会对apk造成破坏,修改后可以正常安装。
这种方案不用重新打包apk,并且在服务端只是写文件的操作,效率很高,可以适用于动态生成apk的场景。
可以通过这个方案进行h5到APP的引流,用户操作不会产生割裂感,保证用户体验的统一。
 

责任编辑:陈琳 来源: 苹果核
相关推荐

2020-09-09 14:22:48

数据中心

2022-07-13 11:49:18

接口迁移方案

2023-07-18 07:23:11

方案payloadrequest

2020-12-23 10:10:23

Pythonweb代码

2022-07-07 10:33:27

Python姿势代码

2022-06-22 09:44:41

Python文件代码

2014-12-01 09:54:40

JavaScript

2020-12-09 10:15:34

Pythonweb代码

2018-06-07 10:29:34

SDN服务器负载均衡

2017-08-01 18:06:56

2016-09-20 12:49:29

2022-09-27 08:04:37

Adapter​设计模式

2022-09-29 12:09:40

MySQLTiDB数据库

2024-03-29 11:13:17

云计算人工智能

2017-08-11 19:02:21

Android全屏幕适配

2018-05-04 15:26:10

Android开发全屏幕

2012-01-17 11:02:39

2017-01-22 16:35:02

iOSBlockCallback

2024-04-12 10:15:24

点赞
收藏

51CTO技术栈公众号