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

智能推荐

while循环&CPU占用率高问题深入分析与解决方案_main函数使用while(1)循环cpu占用99-程序员宅基地

文章浏览阅读3.8k次,点赞9次,收藏28次。直接上一个工作中碰到的问题,另外一个系统开启多线程调用我这边的接口,然后我这边会开启多线程批量查询第三方接口并且返回给调用方。使用的是两三年前别人遗留下来的方法,放到线上后发现确实是可以正常取到结果,但是一旦调用,CPU占用就直接100%(部署环境是win server服务器)。因此查看了下相关的老代码并使用JProfiler查看发现是在某个while循环的时候有问题。具体项目代码就不贴了,类似于下面这段代码。​​​​​​while(flag) {//your code;}这里的flag._main函数使用while(1)循环cpu占用99

【无标题】jetbrains idea shift f6不生效_idea shift +f6快捷键不生效-程序员宅基地

文章浏览阅读347次。idea shift f6 快捷键无效_idea shift +f6快捷键不生效

node.js学习笔记之Node中的核心模块_node模块中有很多核心模块,以下不属于核心模块,使用时需下载的是-程序员宅基地

文章浏览阅读135次。Ecmacript 中没有DOM 和 BOM核心模块Node为JavaScript提供了很多服务器级别,这些API绝大多数都被包装到了一个具名和核心模块中了,例如文件操作的 fs 核心模块 ,http服务构建的http 模块 path 路径操作模块 os 操作系统信息模块// 用来获取机器信息的var os = require('os')// 用来操作路径的var path = require('path')// 获取当前机器的 CPU 信息console.log(os.cpus._node模块中有很多核心模块,以下不属于核心模块,使用时需下载的是

数学建模【SPSS 下载-安装、方差分析与回归分析的SPSS实现(软件概述、方差分析、回归分析)】_化工数学模型数据回归软件-程序员宅基地

文章浏览阅读10w+次,点赞435次,收藏3.4k次。SPSS 22 下载安装过程7.6 方差分析与回归分析的SPSS实现7.6.1 SPSS软件概述1 SPSS版本与安装2 SPSS界面3 SPSS特点4 SPSS数据7.6.2 SPSS与方差分析1 单因素方差分析2 双因素方差分析7.6.3 SPSS与回归分析SPSS回归分析过程牙膏价格问题的回归分析_化工数学模型数据回归软件

利用hutool实现邮件发送功能_hutool发送邮件-程序员宅基地

文章浏览阅读7.5k次。如何利用hutool工具包实现邮件发送功能呢?1、首先引入hutool依赖<dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>5.7.19</version></dependency>2、编写邮件发送工具类package com.pc.c..._hutool发送邮件

docker安装elasticsearch,elasticsearch-head,kibana,ik分词器_docker安装kibana连接elasticsearch并且elasticsearch有密码-程序员宅基地

文章浏览阅读867次,点赞2次,收藏2次。docker安装elasticsearch,elasticsearch-head,kibana,ik分词器安装方式基本有两种,一种是pull的方式,一种是Dockerfile的方式,由于pull的方式pull下来后还需配置许多东西且不便于复用,个人比较喜欢使用Dockerfile的方式所有docker支持的镜像基本都在https://hub.docker.com/docker的官网上能找到合..._docker安装kibana连接elasticsearch并且elasticsearch有密码

随便推点

Python 攻克移动开发失败!_beeware-程序员宅基地

文章浏览阅读1.3w次,点赞57次,收藏92次。整理 | 郑丽媛出品 | CSDN(ID:CSDNnews)近年来,随着机器学习的兴起,有一门编程语言逐渐变得火热——Python。得益于其针对机器学习提供了大量开源框架和第三方模块,内置..._beeware

Swift4.0_Timer 的基本使用_swift timer 暂停-程序员宅基地

文章浏览阅读7.9k次。//// ViewController.swift// Day_10_Timer//// Created by dongqiangfei on 2018/10/15.// Copyright 2018年 飞飞. All rights reserved.//import UIKitclass ViewController: UIViewController { ..._swift timer 暂停

元素三大等待-程序员宅基地

文章浏览阅读986次,点赞2次,收藏2次。1.硬性等待让当前线程暂停执行,应用场景:代码执行速度太快了,但是UI元素没有立马加载出来,造成两者不同步,这时候就可以让代码等待一下,再去执行找元素的动作线程休眠,强制等待 Thread.sleep(long mills)package com.example.demo;import org.junit.jupiter.api.Test;import org.openqa.selenium.By;import org.openqa.selenium.firefox.Firefox.._元素三大等待

Java软件工程师职位分析_java岗位分析-程序员宅基地

文章浏览阅读3k次,点赞4次,收藏14次。Java软件工程师职位分析_java岗位分析

Java:Unreachable code的解决方法_java unreachable code-程序员宅基地

文章浏览阅读2k次。Java:Unreachable code的解决方法_java unreachable code

标签data-*自定义属性值和根据data属性值查找对应标签_如何根据data-*属性获取对应的标签对象-程序员宅基地

文章浏览阅读1w次。1、html中设置标签data-*的值 标题 11111 222222、点击获取当前标签的data-url的值$('dd').on('click', function() { var urlVal = $(this).data('ur_如何根据data-*属性获取对应的标签对象

推荐文章

热门文章

相关标签