java动态打包_Java服务器动态打包APK安装包(添加渠道等相关信息)-程序员宅基地

技术标签: java动态打包  

1 importjava.io.IOException;2 importjava.nio.BufferUnderflowException;3 importjava.nio.ByteBuffer;4 importjava.nio.ByteOrder;5 importjava.nio.channels.FileChannel;6 importjava.util.LinkedHashMap;7 importjava.util.Map;8

9 final classApkUtil {10 privateApkUtil() {11 super();12 }13

14 /**

15 * APK Signing Block Magic Code: magic “APK Sig Block 42” (16 bytes)16 * "APK Sig Block 42" : 41 50 4B 20 53 69 67 20 42 6C 6F 63 6B 20 34 3217 */

18 public static final long APK_SIG_BLOCK_MAGIC_HI = 0x3234206b636f6c42L; //LITTLE_ENDIAN, High

19 public static final long APK_SIG_BLOCK_MAGIC_LO = 0x20676953204b5041L; //LITTLE_ENDIAN, Low

20 private static final int APK_SIG_BLOCK_MIN_SIZE = 32;21

22 /*

23 The v2 signature of the APK is stored as an ID-value pair with ID 0x7109871a24 (https://source.android.com/security/apksigning/v2.html#apk-signing-block)25 */

26 public static final int APK_SIGNATURE_SCHEME_V2_BLOCK_ID = 0x7109871a;27

28 /**

29 * The padding in APK SIG BLOCK (V3 scheme introduced)30 * Seehttps://android.googlesource.com/platform/tools/apksig/+/master/src/main/java/com/android/apksig/internal/apk/ApkSigningBlockUtils.java31 */

32 public static final int VERITY_PADDING_BLOCK_ID = 0x42726577;33

34 public static final int ANDROID_COMMON_PAGE_ALIGNMENT_BYTES = 4096;35

36

37 //Our Channel Block ID

38 public static final int APK_CHANNEL_BLOCK_ID = 0x71777777;39

40 public static final String DEFAULT_CHARSET = "UTF-8";41

42 private static final int ZIP_EOCD_REC_MIN_SIZE = 22;43 private static final int ZIP_EOCD_REC_SIG = 0x06054b50;44 private static final int UINT16_MAX_VALUE = 0xffff;45 private static final int ZIP_EOCD_COMMENT_LENGTH_FIELD_OFFSET = 20;46

47 public static long getCommentLength(final FileChannel fileChannel) throwsIOException {48 //End of central directory record (EOCD)49 //Offset Bytes Description[23]50 //0 4 End of central directory signature = 0x06054b5051 //4 2 Number of this disk52 //6 2 Disk where central directory starts53 //8 2 Number of central directory records on this disk54 //10 2 Total number of central directory records55 //12 4 Size of central directory (bytes)56 //16 4 Offset of start of central directory, relative to start of archive57 //20 2 Comment length (n)58 //22 n Comment59 //For a zip with no archive comment, the60 //end-of-central-directory record will be 22 bytes long, so61 //we expect to find the EOCD marker 22 bytes from the end.

62

63

64 final long archiveSize =fileChannel.size();65 if (archiveSize

77 final long maxCommentLength = Math.min(archiveSize -ZIP_EOCD_REC_MIN_SIZE, UINT16_MAX_VALUE);78 final long eocdWithEmptyCommentStartPosition = archiveSize -ZIP_EOCD_REC_MIN_SIZE;79 for (int expectedCommentLength = 0; expectedCommentLength <=maxCommentLength;80 expectedCommentLength++) {81 final long eocdStartPos = eocdWithEmptyCommentStartPosition -expectedCommentLength;82

83 final ByteBuffer byteBuffer = ByteBuffer.allocate(4);84 fileChannel.position(eocdStartPos);85 fileChannel.read(byteBuffer);86 byteBuffer.order(ByteOrder.LITTLE_ENDIAN);87

88 if (byteBuffer.getInt(0) ==ZIP_EOCD_REC_SIG) {89 final ByteBuffer commentLengthByteBuffer = ByteBuffer.allocate(2);90 fileChannel.position(eocdStartPos +ZIP_EOCD_COMMENT_LENGTH_FIELD_OFFSET);91 fileChannel.read(commentLengthByteBuffer);92 commentLengthByteBuffer.order(ByteOrder.LITTLE_ENDIAN);93

94 final int actualCommentLength = commentLengthByteBuffer.getShort(0);95 if (actualCommentLength ==expectedCommentLength) {96 returnactualCommentLength;97 }98 }99 }100 throw new IOException("ZIP End of Central Directory (EOCD) record not found");101 }102

103 public static long findCentralDirStartOffset(final FileChannel fileChannel) throwsIOException {104 returnfindCentralDirStartOffset(fileChannel, getCommentLength(fileChannel));105 }106

107 public static long findCentralDirStartOffset(final FileChannel fileChannel, final long commentLength) throwsIOException {108 //End of central directory record (EOCD)109 //Offset Bytes Description[23]110 //0 4 End of central directory signature = 0x06054b50111 //4 2 Number of this disk112 //6 2 Disk where central directory starts113 //8 2 Number of central directory records on this disk114 //10 2 Total number of central directory records115 //12 4 Size of central directory (bytes)116 //16 4 Offset of start of central directory, relative to start of archive117 //20 2 Comment length (n)118 //22 n Comment119 //For a zip with no archive comment, the120 //end-of-central-directory record will be 22 bytes long, so121 //we expect to find the EOCD marker 22 bytes from the end.

122

123 final ByteBuffer zipCentralDirectoryStart = ByteBuffer.allocate(4);124 zipCentralDirectoryStart.order(ByteOrder.LITTLE_ENDIAN);125 fileChannel.position(fileChannel.size() - commentLength - 6); //6 = 2 (Comment length) + 4 (Offset of start of central directory, relative to start of archive)

126 fileChannel.read(zipCentralDirectoryStart);127 final long centralDirStartOffset = zipCentralDirectoryStart.getInt(0);128 returncentralDirStartOffset;129 }130

131 public static PairfindApkSigningBlock(132 final FileChannel fileChannel) throwsIOException, SignatureNotFoundException {133 final long centralDirOffset =findCentralDirStartOffset(fileChannel);134 returnfindApkSigningBlock(fileChannel, centralDirOffset);135 }136

137 public static PairfindApkSigningBlock(138 final FileChannel fileChannel, final long centralDirOffset) throwsIOException, SignatureNotFoundException {139

140 //Find the APK Signing Block. The block immediately precedes the Central Directory.141

142 //FORMAT:143 //OFFSET DATA TYPE DESCRIPTION144 //* @+0 bytes uint64: size in bytes (excluding this field)145 //* @+8 bytes payload146 //* @-24 bytes uint64: size in bytes (same as the one above)147 //* @-16 bytes uint128: magic

148

149 if (centralDirOffset

152 +centralDirOffset);153 }154 //Read the magic and offset in file from the footer section of the block:155 //* uint64: size of block156 //* 16 bytes: magic

157 fileChannel.position(centralDirOffset - 24);158 final ByteBuffer footer = ByteBuffer.allocate(24);159 fileChannel.read(footer);160 footer.order(ByteOrder.LITTLE_ENDIAN);161 if ((footer.getLong(8) !=APK_SIG_BLOCK_MAGIC_LO)162 || (footer.getLong(16) !=APK_SIG_BLOCK_MAGIC_HI)) {163 throw newSignatureNotFoundException(164 "No APK Signing Block before ZIP Central Directory");165 }166 //Read and compare size fields

167 final long apkSigBlockSizeInFooter = footer.getLong(0);168 if ((apkSigBlockSizeInFooter Integer.MAX_VALUE - 8)) {170 throw newSignatureNotFoundException(171 "APK Signing Block size out of range: " +apkSigBlockSizeInFooter);172 }173 final int totalSize = (int) (apkSigBlockSizeInFooter + 8);174 final long apkSigBlockOffset = centralDirOffset -totalSize;175 if (apkSigBlockOffset < 0) {176 throw newSignatureNotFoundException(177 "APK Signing Block offset out of range: " +apkSigBlockOffset);178 }179 fileChannel.position(apkSigBlockOffset);180 final ByteBuffer apkSigBlock =ByteBuffer.allocate(totalSize);181 fileChannel.read(apkSigBlock);182 apkSigBlock.order(ByteOrder.LITTLE_ENDIAN);183 final long apkSigBlockSizeInHeader = apkSigBlock.getLong(0);184 if (apkSigBlockSizeInHeader !=apkSigBlockSizeInFooter) {185 throw newSignatureNotFoundException(186 "APK Signing Block sizes in header and footer do not match: "

187 + apkSigBlockSizeInHeader + " vs " +apkSigBlockSizeInFooter);188 }189 returnPair.of(apkSigBlock, apkSigBlockOffset);190 }191

192 public static Map findIdValues(final ByteBuffer apkSigningBlock) throwsSignatureNotFoundException {193 checkByteOrderLittleEndian(apkSigningBlock);194 //FORMAT:195 //OFFSET DATA TYPE DESCRIPTION196 //* @+0 bytes uint64: size in bytes (excluding this field)197 //* @+8 bytes pairs198 //* @-24 bytes uint64: size in bytes (same as the one above)199 //* @-16 bytes uint128: magic

200 final ByteBuffer pairs = sliceFromTo(apkSigningBlock, 8, apkSigningBlock.capacity() - 24);201

202 final Map idValues = new LinkedHashMap(); //keep order

203

204 int entryCount = 0;205 while(pairs.hasRemaining()) {206 entryCount++;207 if (pairs.remaining() < 8) {208 throw newSignatureNotFoundException(209 "Insufficient data to read size of APK Signing Block entry #" +entryCount);210 }211 final long lenLong =pairs.getLong();212 if ((lenLong < 4) || (lenLong >Integer.MAX_VALUE)) {213 throw newSignatureNotFoundException(214 "APK Signing Block entry #" +entryCount215 + " size out of range: " +lenLong);216 }217 final int len = (int) lenLong;218 final int nextEntryPos = pairs.position() +len;219 if (len >pairs.remaining()) {220 throw newSignatureNotFoundException(221 "APK Signing Block entry #" + entryCount + " size out of range: " +len222 + ", available: " +pairs.remaining());223 }224 final int id =pairs.getInt();225 idValues.put(id, getByteBuffer(pairs, len - 4));226

227 pairs.position(nextEntryPos);228 }229

230 returnidValues;231 }232

233 /**

234 * Returns new byte buffer whose content is a shared subsequence of this buffer's content235 * between the specified start (inclusive) and end (exclusive) positions. As opposed to236 * {@linkByteBuffer#slice()}, the returned buffer's byte order is the same as the source237 * buffer's byte order.238 */

239 private static ByteBuffer sliceFromTo(final ByteBuffer source, final int start, final intend) {240 if (start < 0) {241 throw new IllegalArgumentException("start: " +start);242 }243 if (end source.capacity()) {248 throw new IllegalArgumentException("end > capacity: " + end + " > " +capacity);249 }250 final int originalLimit =source.limit();251 final int originalPosition =source.position();252 try{253 source.position(0);254 source.limit(end);255 source.position(start);256 final ByteBuffer result =source.slice();257 result.order(source.order());258 returnresult;259 } finally{260 source.position(0);261 source.limit(originalLimit);262 source.position(originalPosition);263 }264 }265

266 /**

267 * Relative get method for reading {@codesize} number of bytes from the current268 * position of this buffer.269 *

270 *

This method reads the next {@codesize} bytes at this buffer's current position,271 * returning them as a {@codeByteBuffer} with start set to 0, limit and capacity set to272 * {@codesize}, byte order set to this buffer's byte order; and then increments the position by273 * {@codesize}.274 */

275 private static ByteBuffer getByteBuffer(final ByteBuffer source, final intsize)276 throwsBufferUnderflowException {277 if (size < 0) {278 throw new IllegalArgumentException("size: " +size);279 }280 final int originalLimit =source.limit();281 final int position =source.position();282 final int limit = position +size;283 if ((limit < position) || (limit >originalLimit)) {284 throw newBufferUnderflowException();285 }286 source.limit(limit);287 try{288 final ByteBuffer result =source.slice();289 result.order(source.order());290 source.position(limit);291 returnresult;292 } finally{293 source.limit(originalLimit);294 }295 }296

297 private static void checkByteOrderLittleEndian(finalByteBuffer buffer) {298 if (buffer.order() !=ByteOrder.LITTLE_ENDIAN) {299 throw new IllegalArgumentException("ByteBuffer byte order must be little endian");300 }301 }302

303

304 }

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_29168675/article/details/114080240

智能推荐

CentOS离线安装最新版本Docker_centos离线安装docker 18.09.6-程序员宅基地

文章浏览阅读1.4w次,点赞5次,收藏32次。一、背景由于公司内部服务器只能连内网,即使建立局域网yum源,也无法使用yum在线安装Docker CE。支持在线安装的朋友们,直接参考CentOS在线安装Docker官方文档即可:https://docs.docker.com/install/linux/docker-ce/centos/目前最新版本是v18.09.6。二、准备工作1、根据官方文档对CentOS的要求:需要是..._centos离线安装docker 18.09.6

关于pycharm上opencv的安装和cv2 ‘cv2.’无代码提示问题的解决_pycharm用terminal下载opencv-程序员宅基地

文章浏览阅读1.9k次,点赞6次,收藏10次。一.pycharm下opencv的安装在pycharm下的Terminal输入pip install opencv-python也可以下载功能加强版pip install opencv-contrib-python添加了国内pip源的朋友们记得在命令行后面加上(以下是清华pip源)-i https://pypi.tuna.tsinghua.edu.cn/simple..._pycharm用terminal下载opencv

linux内核编译过程中出现两个错误的解决方法~!-程序员宅基地

文章浏览阅读639次。  /*************************** error 1 ******************************/ 在编译内核的过程中出现了如下的编译显示代码:   drivers/video/console/vgacon.c: In function 'vgacon_startup':   drivers/video/console/vgacon.c:510..._编译内核 efi_secure_boot undeclared 错误

SqlServer事务1.0_php 原生sqlserver事务-程序员宅基地

文章浏览阅读156次。事务事务的定义事务的使用场景事务的特性事务的语法实例讲解事务的定义事务就是被绑定在一起作为一个逻辑工作单元的SQL语句组,如果任何一个语句操作失败那么整个操作就被失败,进而回滚到操作前状态,或者是上个节点。为了确保要么执行,要么不执行,就可以使用事务。要将一组语句作为事务考虑,就需要通过ACID测试,即原子性,一致性,隔离性和持久性。事务的使用场景举个例子,我们经常会使用转账功能,转账的时候,是先减去转出自己账户的金额,然后再在指定转入账户的金额加上转出的金额。如果刚好这个时候转出的操作已经执行完成_php 原生sqlserver事务

jstack分析java应用线程阻塞实战_jstack 分析阻塞进程-程序员宅基地

文章浏览阅读1.1k次。问题描述:生产环境,有个查询交易提交后,一直转圈未响应。问题分析:1、其他操作都比较流畅,初步怀疑是有同步锁导致的线程阻塞2、使用jstack命令收集堆栈信息。进程号为7689,命令如下:jstack -l 7689 &gt; aa.tdump参考http://www.cnblogs.com/nexiyi/p/java_thread_jstack.html中的操作,查找..._jstack 分析阻塞进程

mysql timestamp 差值_MySQL用 TIMESTAMPDIFF() 函数计算时间差-程序员宅基地

文章浏览阅读1.1k次。TIMESTAMPDIFF() 函数将计算两个日期或日期时间表达式之间的整数时间差。其中,我们可以通过参数指定时间差的单位,如:秒、分钟、小时等。语法:TIMESTAMPDIFF(interval,datetime1,datetime2)参数说明:interval:日期比较返回的时间差单位。可以使如下值:FRAC_SECOND:表示间隔是毫秒SECOND:秒MINUTE:分钟HOUR:小时DAY:..._mysql timestamp 作差

随便推点

移植opencv+opencv_contrib_apps/annotation/cmakefiles/opencv_annotation.dir/b-程序员宅基地

文章浏览阅读1.4k次,点赞4次,收藏8次。交叉编译opencv 3.4.2opencv_contribute 3.4.2ubuntu 16cmake 3.12.2cmake 进行编译配置勾选 ENABLE_CXX11设置安装路径 CMAKE_INSTALL_PREFIX /usr/local问题一:opencv-3.4.1/3rdparty/libpng/pngstruct.h:30:18: fatal error: zlib.h: No such file or directory~/op..._apps/annotation/cmakefiles/opencv_annotation.dir/build.make:99: recipe for t

图像分割最全综述_图像分割最优问题综述-程序员宅基地

文章浏览阅读2.6k次,点赞2次,收藏26次。转载 https://www.cnblogs.com/CV-life/p/11160796.html 图像分割是计算机视觉研究中的一个经典难题,已经成为图像理解领域关注的一个热点,图像分割是图像分析的第一步,是计算机视觉的基础,是图像理解的重要组成部分,同时也是图像处理中最困难的问题之一。所谓图像分割是指根据灰度、彩色、空间纹理、几何形状等特征把图像划分成若干个互不相交的区域,使得这些特征在同一区域内表现出一致性或相似性,而在不同区域间表现出明显的不同。简单的说就是在一副图像中,把目标从背景..._图像分割最优问题综述

终于弄明白 i = i++和 i = ++i 了_i=i++-程序员宅基地

文章浏览阅读3.4w次,点赞156次,收藏618次。写在前面:前些天看完了JVM的内存结构,自以为自己是懂了,心里想想不就是分线程共享和线程私有嘛,然后又怎么怎么分怎么怎么的嘛…直到遇到了这道题目。说句实话,曾经自己做这种运算题目,完全是靠脑子空想,然后拿上笔颤抖的写下一个自己都不知道正不正确的答案。不过过了今天,我终于能确定它的答案了。为此,我也专门写一篇博客,记录我的学习!!!文章目录1、题目2、分析2.1、第一步2.2、第二步2.3、第三步2.4、第四步2.5、结果3、i = ++i1、题目package pers.mobian.._i=i++

Beginning ARC in iOS 5 Tutorial Part 2_warning[440]: the section alignment is less than 2-程序员宅基地

文章浏览阅读779次。Note from Ray: This is the twelfth iOS 5 tutorial in the iOS 5 Feast! This tutorial is a free preview chapter from our new bookiOS 5 By Tutorials. Matthijs Hollemans wrote this chapter – the same_warning[440]: the section alignment is less than 2^2. data in literal pool m

面试题--乱2-程序员宅基地

文章浏览阅读889次。windows服务器使用的远程连接端口默认 3389 查询max 记录: nslookup dig postfix的配置文件main,cf中以下哪个参数是设置邮件大小的,message_size_limit打开网站,出现以下错误Fatal error: Unable to read **** bytes in或者是Fatal error: Corrupte..._让一个使用者bobby能够进行cp/dirl/file/dir2的指令时,请说明dirl、file、dir

c++期末上机oj题目汇总二(2018北邮信通版)纯干货_北邮c++期末考试-程序员宅基地

文章浏览阅读2.4k次,点赞9次,收藏73次。学长学姐回忆版本6-11今天一气呵成上传系列,就是脖子有点酸题组六1.大小写转换2.计算有多少个盈数3.输出最大值,平均数和及格人数4.统计字母个数5.学生类..._北邮c++期末考试

推荐文章

热门文章

相关标签