Java服务打包动态Android渠道

Z前言

由于我单位正在做游戏推广,每次要做渠道号得去三方上传应用 ,过程时间长还能麻烦,所以就打算自己做一个服务来打包渠道信息,于是就有了下面的经历

历程

1、一开始我是打算在apk中做多渠道打包,需要什么渠道信息就打一个安装包,这样效率低还很麻烦,不方便业务随时弄
2、APK本身就是个ZIP压缩包嘛,我把他解压缩,在里面中放一个配置文件,然后下载的时候,WEB服务器根据用户信息的不同动态替换这个配置文件,最后重新压缩成apk,然后APP运行的时候,读取这个配置文件就可以了。代码写好了,生成的安装包放手机一安装,安装包解析失败!
3、于是在GitHub还有百度和谷歌各种找资料,很多博客也都写了Java动态添加apk配置文件,修改配置,可是代码都是很老的了,现在系统最高都12了,估计到时候会去适配改代码很麻烦,于是就找到了一个忘zip中的commont区中添加信息的博客,这也是一种思路,于是就开启了复制代码之旅,往comments区添加洗洗,读取信息都可以,但是只是勾选了V1签名
4、最后高版本对安装包解析很严格,每个厂商对apk检测很严,最终在GitHub上找到美团的项目https://github.com/Meituan-Dianping/walle,在美团的多渠道打包工具提供了一个walle-cli-all.jar包,经历多个挫折最终找到他的核心代码和另外一个博主的博客https://www.cnblogs.com/plumsq/p/11589776.html

代码

package com.zw.apk.channel.apk;
import java.io.DataOutput;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.ArrayList;
import java.util.List;

/**
 * https://source.android.com/security/apksigning/v2.html
 * https://en.wikipedia.org/wiki/Zip_(file_format)
 */
class ApkSigningBlock {

    private final List<ApkSigningPayload> payloads;

    ApkSigningBlock() {
        super();

        payloads = new ArrayList<ApkSigningPayload>();
    }

    public final List<ApkSigningPayload> getPayloads() {
        return payloads;
    }

    public void addPayload(final ApkSigningPayload payload) {
        payloads.add(payload);
    }

    public long writeApkSigningBlock(final DataOutput dataOutput) throws IOException {
        long length = 24; // 24 = 8(size of block in bytes—same as the very first field (uint64)) + 16 (magic “APK Sig Block 42” (16 bytes))
        for (int index = 0; index < payloads.size(); ++index) {
            final ApkSigningPayload payload = payloads.get(index);
            final byte[] bytes = payload.getByteBuffer();
            length += 12 + bytes.length; // 12 = 8(uint64-length-prefixed) + 4 (ID (uint32))
        }

        ByteBuffer byteBuffer = ByteBuffer.allocate(8); // Long.BYTES
        byteBuffer.order(ByteOrder.LITTLE_ENDIAN);
        byteBuffer.putLong(length);
        byteBuffer.flip();
        dataOutput.write(byteBuffer.array());

        for (int index = 0; index < payloads.size(); ++index) {
            final ApkSigningPayload payload = payloads.get(index);
            final byte[] bytes = payload.getByteBuffer();

            byteBuffer = ByteBuffer.allocate(8); // Long.BYTES
            byteBuffer.order(ByteOrder.LITTLE_ENDIAN);
            byteBuffer.putLong(bytes.length + (8 - 4)); // Long.BYTES - Integer.BYTES
            byteBuffer.flip();
            dataOutput.write(byteBuffer.array());

            byteBuffer = ByteBuffer.allocate(4); // Integer.BYTES
            byteBuffer.order(ByteOrder.LITTLE_ENDIAN);
            byteBuffer.putInt(payload.getId());
            byteBuffer.flip();
            dataOutput.write(byteBuffer.array());

            dataOutput.write(bytes);
        }

        byteBuffer = ByteBuffer.allocate(8); // Long.BYTES
        byteBuffer.order(ByteOrder.LITTLE_ENDIAN);
        byteBuffer.putLong(length);
        byteBuffer.flip();
        dataOutput.write(byteBuffer.array());

        byteBuffer = ByteBuffer.allocate(8); // Long.BYTES
        byteBuffer.order(ByteOrder.LITTLE_ENDIAN);
        byteBuffer.putLong(ApkUtil.APK_SIG_BLOCK_MAGIC_LO);
        byteBuffer.flip();
        dataOutput.write(byteBuffer.array());

        byteBuffer = ByteBuffer.allocate(8); // Long.BYTES
        byteBuffer.order(ByteOrder.LITTLE_ENDIAN);
        byteBuffer.putLong(ApkUtil.APK_SIG_BLOCK_MAGIC_HI);
        byteBuffer.flip();
        dataOutput.write(byteBuffer.array());

        return length;
    }

}

ApkSigningPayload

package com.zw.apk.channel.apk;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Arrays;

class ApkSigningPayload {
    private final int id;
    private final ByteBuffer buffer;
    private final int totalSize;

    ApkSigningPayload(final int id, final ByteBuffer buffer) {
        super();
        this.id = id;
        if (buffer.order() != ByteOrder.LITTLE_ENDIAN) {
            throw new IllegalArgumentException("ByteBuffer byte order must be little endian");
        }
        this.buffer = buffer;
        // assume buffer is not consumed
        this.totalSize = 8 + 4 + buffer.remaining(); // size + id + value
    }

    public int getId() {
        return id;
    }

    public byte[] getByteBuffer() {
        final byte[] array = buffer.array();
        final int arrayOffset = buffer.arrayOffset();
        return Arrays.copyOfRange(array, arrayOffset + buffer.position(),
                arrayOffset + buffer.limit());
    }

    /**
     * Total bytes of this block
     */
    public int getTotalSize() {
        return totalSize;
    }
}

ApkUtil

package com.zw.apk.channel.apk;

import java.io.IOException;
import java.nio.BufferUnderflowException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.channels.FileChannel;
import java.util.LinkedHashMap;
import java.util.Map;

public class ApkUtil {
   private ApkUtil() {
       super();
   }

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

   public static boolean PUT_CHANNEL=false;
   public static final long APK_SIG_BLOCK_MAGIC_HI = 0x3234206b636f6c42L; // LITTLE_ENDIAN, High
   public static final long APK_SIG_BLOCK_MAGIC_LO = 0x20676953204b5041L; // LITTLE_ENDIAN, Low
   private static final int APK_SIG_BLOCK_MIN_SIZE = 32;

   /*
    The v2 signature of the APK is stored as an ID-value pair with ID 0x7109871a
    (https://source.android.com/security/apksigning/v2.html#apk-signing-block)
     */
   public static final int APK_SIGNATURE_SCHEME_V2_BLOCK_ID = 0x7109871a;

   /**
    * The padding in APK SIG BLOCK (V3 scheme introduced)
    * See https://android.googlesource.com/platform/tools/apksig/+/master/src/main/java/com/android/apksig/internal/apk/ApkSigningBlockUtils.java
    */
   public static final int VERITY_PADDING_BLOCK_ID = 0x42726577;

   public static final int ANDROID_COMMON_PAGE_ALIGNMENT_BYTES = 4096;


   // Our Channel Block ID
   public static final int APK_CHANNEL_BLOCK_ID = 0x71777777;

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

   private static final int ZIP_EOCD_REC_MIN_SIZE = 22;
   private static final int ZIP_EOCD_REC_SIG = 0x06054b50;
   private static final int UINT16_MAX_VALUE = 0xffff;
   private static final int ZIP_EOCD_COMMENT_LENGTH_FIELD_OFFSET = 20;

   public static long getCommentLength(final FileChannel fileChannel) throws IOException {



       final long archiveSize = fileChannel.size();
       if (archiveSize < ZIP_EOCD_REC_MIN_SIZE) {
           throw new IOException("APK too small for ZIP End of Central Directory (EOCD) record");
       }
       // ZIP End of Central Directory (EOCD) record is located at the very end of the ZIP archive.
       // The record can be identified by its 4-byte signature/magic which is located at the very
       // beginning of the record. A complication is that the record is variable-length because of
       // the comment field.
       // The algorithm for locating the ZIP EOCD record is as follows. We search backwards from
       // end of the buffer for the EOCD record signature. Whenever we find a signature, we check
       // the candidate record's comment length is such that the remainder of the record takes up
       // exactly the remaining bytes in the buffer. The search is bounded because the maximum
       // size of the comment field is 65535 bytes because the field is an unsigned 16-bit number.
       final long maxCommentLength = Math.min(archiveSize - ZIP_EOCD_REC_MIN_SIZE, UINT16_MAX_VALUE);
       final long eocdWithEmptyCommentStartPosition = archiveSize - ZIP_EOCD_REC_MIN_SIZE;
       for (int expectedCommentLength = 0; expectedCommentLength <= maxCommentLength;
            expectedCommentLength++) {
           final long eocdStartPos = eocdWithEmptyCommentStartPosition - expectedCommentLength;

           final ByteBuffer byteBuffer = ByteBuffer.allocate(4);
           fileChannel.position(eocdStartPos);
           fileChannel.read(byteBuffer);
           byteBuffer.order(ByteOrder.LITTLE_ENDIAN);

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

               final int actualCommentLength = commentLengthByteBuffer.getShort(0);
               if (actualCommentLength == expectedCommentLength) {
                   return actualCommentLength;
               }
           }
       }
       throw new IOException("ZIP End of Central Directory (EOCD) record not found");
   }

   public static long findCentralDirStartOffset(final FileChannel fileChannel) throws IOException {
       return findCentralDirStartOffset(fileChannel, getCommentLength(fileChannel));
   }

   public static long findCentralDirStartOffset(final FileChannel fileChannel, final long commentLength) throws IOException {
       // End of central directory record (EOCD)
       // Offset    Bytes     Description[23]
       // 0           4       End of central directory signature = 0x06054b50
       // 4           2       Number of this disk
       // 6           2       Disk where central directory starts
       // 8           2       Number of central directory records on this disk
       // 10          2       Total number of central directory records
       // 12          4       Size of central directory (bytes)
       // 16          4       Offset of start of central directory, relative to start of archive
       // 20          2       Comment length (n)
       // 22          n       Comment
       // For a zip with no archive comment, the
       // end-of-central-directory record will be 22 bytes long, so
       // we expect to find the EOCD marker 22 bytes from the end.

       final ByteBuffer zipCentralDirectoryStart = ByteBuffer.allocate(4);
       zipCentralDirectoryStart.order(ByteOrder.LITTLE_ENDIAN);
       fileChannel.position(fileChannel.size() - commentLength - 6); // 6 = 2 (Comment length) + 4 (Offset of start of central directory, relative to start of archive)
       fileChannel.read(zipCentralDirectoryStart);
       final long centralDirStartOffset = zipCentralDirectoryStart.getInt(0);
       return centralDirStartOffset;
   }

   public static Pair<ByteBuffer, Long> findApkSigningBlock(
           final FileChannel fileChannel) throws IOException, SignatureNotFoundException {
       final long centralDirOffset = findCentralDirStartOffset(fileChannel);
       return findApkSigningBlock(fileChannel, centralDirOffset);
   }

   public static Pair<ByteBuffer, Long> findApkSigningBlock(
           final FileChannel fileChannel, final long centralDirOffset) throws IOException, SignatureNotFoundException {

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

       // FORMAT:
       // OFFSET       DATA TYPE  DESCRIPTION
       // * @+0  bytes uint64:    size in bytes (excluding this field)
       // * @+8  bytes payload
       // * @-24 bytes uint64:    size in bytes (same as the one above)
       // * @-16 bytes uint128:   magic

       if (centralDirOffset < APK_SIG_BLOCK_MIN_SIZE) {
           throw new SignatureNotFoundException(
                   "APK too small for APK Signing Block. ZIP Central Directory offset: "
                           + centralDirOffset);
       }
       // Read the magic and offset in file from the footer section of the block:
       // * uint64:   size of block
       // * 16 bytes: magic
       fileChannel.position(centralDirOffset - 24);
       final ByteBuffer footer = ByteBuffer.allocate(24);
       fileChannel.read(footer);
       footer.order(ByteOrder.LITTLE_ENDIAN);
       if ((footer.getLong(8) != APK_SIG_BLOCK_MAGIC_LO)
               || (footer.getLong(16) != APK_SIG_BLOCK_MAGIC_HI)) {
           throw new SignatureNotFoundException(
                   "No APK Signing Block before ZIP Central Directory");
       }
       // Read and compare size fields
       final long apkSigBlockSizeInFooter = footer.getLong(0);
       if ((apkSigBlockSizeInFooter < footer.capacity())
               || (apkSigBlockSizeInFooter > Integer.MAX_VALUE - 8)) {
           throw new SignatureNotFoundException(
                   "APK Signing Block size out of range: " + apkSigBlockSizeInFooter);
       }
       final int totalSize = (int) (apkSigBlockSizeInFooter + 8);
       final long apkSigBlockOffset = centralDirOffset - totalSize;
       if (apkSigBlockOffset < 0) {
           throw new SignatureNotFoundException(
                   "APK Signing Block offset out of range: " + apkSigBlockOffset);
       }
       fileChannel.position(apkSigBlockOffset);
       final ByteBuffer apkSigBlock = ByteBuffer.allocate(totalSize);
       fileChannel.read(apkSigBlock);
       apkSigBlock.order(ByteOrder.LITTLE_ENDIAN);
       final long apkSigBlockSizeInHeader = apkSigBlock.getLong(0);
       if (apkSigBlockSizeInHeader != apkSigBlockSizeInFooter) {
           throw new SignatureNotFoundException(
                   "APK Signing Block sizes in header and footer do not match: "
                           + apkSigBlockSizeInHeader + " vs " + apkSigBlockSizeInFooter);
       }
       return Pair.of(apkSigBlock, apkSigBlockOffset);
   }

   public static Map<Integer, ByteBuffer> findIdValues(final ByteBuffer apkSigningBlock) throws SignatureNotFoundException {
       checkByteOrderLittleEndian(apkSigningBlock);
       // FORMAT:
       // OFFSET       DATA TYPE  DESCRIPTION
       // * @+0  bytes uint64:    size in bytes (excluding this field)
       // * @+8  bytes pairs
       // * @-24 bytes uint64:    size in bytes (same as the one above)
       // * @-16 bytes uint128:   magic
       final ByteBuffer pairs = sliceFromTo(apkSigningBlock, 8, apkSigningBlock.capacity() - 24);

       final Map<Integer, ByteBuffer> idValues = new LinkedHashMap<Integer, ByteBuffer>(); // keep order

       int entryCount = 0;
       while (pairs.hasRemaining()) {
           entryCount++;
           if (pairs.remaining() < 8) {
               throw new SignatureNotFoundException(
                       "Insufficient data to read size of APK Signing Block entry #" + entryCount);
           }
           final long lenLong = pairs.getLong();
           if ((lenLong < 4) || (lenLong > Integer.MAX_VALUE)) {
               throw new SignatureNotFoundException(
                       "APK Signing Block entry #" + entryCount
                               + " size out of range: " + lenLong);
           }
           final int len = (int) lenLong;
           final int nextEntryPos = pairs.position() + len;
           if (len > pairs.remaining()) {
               throw new SignatureNotFoundException(
                       "APK Signing Block entry #" + entryCount + " size out of range: " + len
                               + ", available: " + pairs.remaining());
           }
           final int id = pairs.getInt();
           idValues.put(id, getByteBuffer(pairs, len - 4));

           pairs.position(nextEntryPos);
       }

       return idValues;
   }

   /**
    * Returns new byte buffer whose content is a shared subsequence of this buffer's content
    * between the specified start (inclusive) and end (exclusive) positions. As opposed to
    * {@link ByteBuffer#slice()}, the returned buffer's byte order is the same as the source
    * buffer's byte order.
    */
   private static ByteBuffer sliceFromTo(final ByteBuffer source, final int start, final int end) {
       if (start < 0) {
           throw new IllegalArgumentException("start: " + start);
       }
       if (end < start) {
           throw new IllegalArgumentException("end < start: " + end + " < " + start);
       }
       final int capacity = source.capacity();
       if (end > source.capacity()) {
           throw new IllegalArgumentException("end > capacity: " + end + " > " + capacity);
       }
       final int originalLimit = source.limit();
       final int originalPosition = source.position();
       try {
           source.position(0);
           source.limit(end);
           source.position(start);
           final ByteBuffer result = source.slice();
           result.order(source.order());
           return result;
       } finally {
           source.position(0);
           source.limit(originalLimit);
           source.position(originalPosition);
       }
   }

   /**
    * Relative <em>get</em> method for reading {@code size} number of bytes from the current
    * position of this buffer.
    * <p>
    * <p>This method reads the next {@code size} bytes at this buffer's current position,
    * returning them as a {@code ByteBuffer} with start set to 0, limit and capacity set to
    * {@code size}, byte order set to this buffer's byte order; and then increments the position by
    * {@code size}.
    */
   private static ByteBuffer getByteBuffer(final ByteBuffer source, final int size)
           throws BufferUnderflowException {
       if (size < 0) {
           throw new IllegalArgumentException("size: " + size);
       }
       final int originalLimit = source.limit();
       final int position = source.position();
       final int limit = position + size;
       if ((limit < position) || (limit > originalLimit)) {
           throw new BufferUnderflowException();
       }
       source.limit(limit);
       try {
           final ByteBuffer result = source.slice();
           result.order(source.order());
           source.position(limit);
           return result;
       } finally {
           source.limit(originalLimit);
       }
   }

   private static void checkByteOrderLittleEndian(final ByteBuffer buffer) {
       if (buffer.order() != ByteOrder.LITTLE_ENDIAN) {
           throw new IllegalArgumentException("ByteBuffer byte order must be little endian");
       }
   }


}

辅助工具Pair

package com.zw.apk.channel.apk;

/**
 * Pair of two elements.
 */
final class Pair<A, B> {
    private final A mFirst;
    private final B mSecond;

    private Pair(final A first, final B second) {
        mFirst = first;
        mSecond = second;
    }

    public static <A, B> Pair<A, B> of(final A first, final B second) {
        return new Pair<A, B>(first, second);
    }

    public A getFirst() {
        return mFirst;
    }

    public B getSecond() {
        return mSecond;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((mFirst == null) ? 0 : mFirst.hashCode());
        result = prime * result + ((mSecond == null) ? 0 : mSecond.hashCode());
        return result;
    }

    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        @SuppressWarnings("rawtypes")
        final Pair other = (Pair) obj;
        if (mFirst == null) {
            if (other.mFirst != null) {
                return false;
            }
        } else if (!mFirst.equals(other.mFirst)) {
            return false;
        }
        if (mSecond == null) {
            if (other.mSecond != null) {
                return false;
            }
        } else if (!mSecond.equals(other.mSecond)) {
            return false;
        }
        return true;
    }
}

PayloadReader

package com.zw.apk.channel.apk;

import com.zw.apk.channel.utils.HttpUrlFilePath;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.Arrays;
import java.util.Map;

/**
 * 读取apk渠道信息
 * @author Administrator
 */
public final class PayloadReader {
    private PayloadReader() {
        super();
    }


    /**
     * get string (UTF-8) by id
     *
     * @param apkFile apk file
     * @return null if not found
     */
    public static String getString(final File apkFile, final int id) {
        final byte[] bytes = PayloadReader.get(apkFile, id);
        if (bytes == null) {
            return null;
        }
        try {
            return new String(bytes, ApkUtil.DEFAULT_CHARSET);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * get bytes by id <br/>
     *
     * @param apkFile apk file
     * @param id      id
     * @return bytes
     */
    public static byte[] get(final File apkFile, final int id) {
        final Map<Integer, ByteBuffer> idValues = getAll(apkFile);
        if (idValues == null) {
            return null;
        }
        final ByteBuffer byteBuffer = idValues.get(id);
        if (byteBuffer == null) {
            return null;
        }
        return getBytes(byteBuffer);
    }

    /**
     * get data from byteBuffer
     *
     * @param byteBuffer buffer
     * @return useful data
     */
    private static byte[] getBytes(final ByteBuffer byteBuffer) {
        final byte[] array = byteBuffer.array();
        final int arrayOffset = byteBuffer.arrayOffset();
        return Arrays.copyOfRange(array, arrayOffset + byteBuffer.position(),
                arrayOffset + byteBuffer.limit());
    }

    /**
     * get all custom (id, buffer) <br/>
     * Note: get final from byteBuffer, please use {@link PayloadReader#getBytes getBytes}
     *
     * @param apkFile apk file
     * @return all custom (id, buffer)
     */
    private static Map<Integer, ByteBuffer> getAll(final File apkFile) {
        Map<Integer, ByteBuffer> idValues = null;
        try {
            RandomAccessFile randomAccessFile = null;
            FileChannel fileChannel = null;
            try {
                randomAccessFile = new RandomAccessFile(apkFile, "r");
                fileChannel = randomAccessFile.getChannel();
                final ByteBuffer apkSigningBlock2 = ApkUtil.findApkSigningBlock(fileChannel).getFirst();
                idValues = ApkUtil.findIdValues(apkSigningBlock2);
            } catch (IOException ignore) {
            } finally {
                try {
                    if (fileChannel != null) {
                        fileChannel.close();
                    }
                } catch (IOException ignore) {
                }
                try {
                    if (randomAccessFile != null) {
                        randomAccessFile.close();
                    }
                } catch (IOException ignore) {
                }
            }
        } catch (SignatureNotFoundException ignore) {
        }

        return idValues;
    }
    public static void main(String[] args) {
//        String str= getString(new File("E:\\JavaProject\\ApkChannel\\288\\1\\1230.apk"), ApkUtil.APK_CHANNEL_BLOCK_ID);

//        String str= getString(new File("C:\\Users\\Administrator\\Downloads\\1511677290510627.apk"), ApkUtil.APK_CHANNEL_BLOCK_ID);
//        System.out.println("获取数据:"+str);

    }

}

PayloadWriter

package com.zw.apk.channel.apk;

import java.io.*;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.channels.FileChannel;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.UUID;


public final class PayloadWriter {
    private PayloadWriter() {
        super();
    }

    /**
     * put (id, String) into apk, update if id exists
     * @param apkFile apk file
     * @param id id
     * @param string string content
     * @throws IOException
     * @throws SignatureNotFoundException
     */
    public static void put(final File apkFile, final int id, final String string) throws IOException, SignatureNotFoundException {
        put(apkFile, id, string, false);
    }
    /**
     * put (id, String) into apk, update if id exists
     * @param apkFile apk file
     * @param id id
     * @param string string
     * @param lowMemory if need low memory operation, maybe a little slower
     * @throws IOException
     * @throws SignatureNotFoundException
     */
    public static void put(final File apkFile, final int id, final String string, final boolean lowMemory) throws IOException, SignatureNotFoundException {
        final byte[] bytes = string.getBytes(ApkUtil.DEFAULT_CHARSET);
        final ByteBuffer byteBuffer = ByteBuffer.allocate(bytes.length);
        byteBuffer.order(ByteOrder.LITTLE_ENDIAN);
        byteBuffer.put(bytes, 0, bytes.length);
        byteBuffer.flip();
        put(apkFile, id, byteBuffer, lowMemory);
    }
    /**
     * put (id, buffer) into apk, update if id exists
     *
     * @param apkFile apk file
     * @param id      id
     * @param buffer  buffer
     * @throws IOException
     * @throws SignatureNotFoundException
     */
    public static void put(final File apkFile, final int id, final ByteBuffer buffer) throws IOException, SignatureNotFoundException {
        put(apkFile, id, buffer, false);
    }

    /**
     * put (id, buffer) into apk, update if id exists
     * @param apkFile apk file
     * @param id id
     * @param buffer buffer
     * @param lowMemory if need low memory operation, maybe a little slower
     * @throws IOException
     * @throws SignatureNotFoundException
     */
    public static void put(final File apkFile, final int id, final ByteBuffer buffer, final boolean lowMemory) throws IOException, SignatureNotFoundException {
        final Map<Integer, ByteBuffer> idValues = new HashMap<Integer, ByteBuffer>();
        idValues.put(id, buffer);
        putAll(apkFile, idValues, lowMemory);
    }
    /**
     * put new idValues into apk, update if id exists
     *
     * @param apkFile  apk file
     * @param idValues id value. NOTE: use unknown IDs. DO NOT use ID that have already been used.  See <a href='https://source.android.com/security/apksigning/v2.html'>APK Signature Scheme v2</a>
     * @throws IOException
     * @throws SignatureNotFoundException
     */
    public static void putAll(final File apkFile, final Map<Integer, ByteBuffer> idValues) throws IOException, SignatureNotFoundException {
        putAll(apkFile, idValues, false);
    }
    /**
     * put new idValues into apk, update if id exists
     *
     * @param apkFile  apk file
     * @param idValues id value. NOTE: use unknown IDs. DO NOT use ID that have already been used.  See <a href='https://source.android.com/security/apksigning/v2.html'>APK Signature Scheme v2</a>
     * @param lowMemory if need low memory operation, maybe a little slower
     * @throws IOException
     * @throws SignatureNotFoundException
     */
    public static void putAll(final File apkFile, final Map<Integer, ByteBuffer> idValues, final boolean lowMemory) throws IOException, SignatureNotFoundException {
        handleApkSigningBlock(apkFile, originIdValues -> {
            if (idValues != null && !idValues.isEmpty()) {
                originIdValues.putAll(idValues);
            }
            final ApkSigningBlock apkSigningBlock = new ApkSigningBlock();
            final Set<Map.Entry<Integer, ByteBuffer>> entrySet = originIdValues.entrySet();
            for (Map.Entry<Integer, ByteBuffer> entry : entrySet) {
                final ApkSigningPayload payload = new ApkSigningPayload(entry.getKey(), entry.getValue());
                System.out.println("payload:"+payload.getTotalSize());
                apkSigningBlock.addPayload(payload);
            }
            return apkSigningBlock;
        }, lowMemory);
    }
    /**
     * remove content by id
     *
     * @param apkFile apk file
     * @param id id
     * @throws IOException
     * @throws SignatureNotFoundException
     */
    public static void remove(final File apkFile, final int id) throws IOException, SignatureNotFoundException {
        remove(apkFile, id, false);
    }
    /**
     * remove content by id
     *
     * @param apkFile apk file
     * @param id id
     * @param lowMemory  if need low memory operation, maybe a little slower
     * @throws IOException
     * @throws SignatureNotFoundException
     */
    public static void remove(final File apkFile, final int id, final boolean lowMemory) throws IOException, SignatureNotFoundException {
        PayloadWriter.handleApkSigningBlock(apkFile, new PayloadWriter.ApkSigningBlockHandler() {
            @Override
            public ApkSigningBlock handle(final Map<Integer, ByteBuffer> originIdValues) {
                final ApkSigningBlock apkSigningBlock = new ApkSigningBlock();
                final Set<Map.Entry<Integer, ByteBuffer>> entrySet = originIdValues.entrySet();
                for (Map.Entry<Integer, ByteBuffer> entry : entrySet) {
                    if (entry.getKey() != id) {
                        final ApkSigningPayload payload = new ApkSigningPayload(entry.getKey(), entry.getValue());
                        apkSigningBlock.addPayload(payload);
                    }
                }
                return apkSigningBlock;
            }
        }, lowMemory);
    }

    interface ApkSigningBlockHandler {
        ApkSigningBlock handle(Map<Integer, ByteBuffer> originIdValues);
    }

    static void handleApkSigningBlock(final File apkFile, final ApkSigningBlockHandler handler, final boolean lowMemory) throws IOException, SignatureNotFoundException {
        RandomAccessFile fIn = null;
        FileChannel fileChannel = null;
        try {
            fIn = new RandomAccessFile(apkFile, "rw");
            fileChannel = fIn.getChannel();
            final long commentLength = ApkUtil.getCommentLength(fileChannel);
            final long centralDirStartOffset = ApkUtil.findCentralDirStartOffset(fileChannel, commentLength);
            // Find the APK Signing Block. The block immediately precedes the Central Directory.
            final Pair<ByteBuffer, Long> apkSigningBlockAndOffset = ApkUtil.findApkSigningBlock(fileChannel, centralDirStartOffset);
            final ByteBuffer apkSigningBlock2 = apkSigningBlockAndOffset.getFirst();
            final long apkSigningBlockOffset = apkSigningBlockAndOffset.getSecond();

            final Map<Integer, ByteBuffer> originIdValues = ApkUtil.findIdValues(apkSigningBlock2);
            // Find the APK Signature Scheme v2 Block inside the APK Signing Block.
            final ByteBuffer apkSignatureSchemeV2Block = originIdValues.get(ApkUtil.APK_SIGNATURE_SCHEME_V2_BLOCK_ID);

            if (apkSignatureSchemeV2Block == null) {
                throw new IOException(
                        "No APK Signature Scheme v2 block in APK Signing Block");
            }

            final boolean needPadding = originIdValues.remove(ApkUtil.VERITY_PADDING_BLOCK_ID) != null;
            final ApkSigningBlock apkSigningBlock = handler.handle(originIdValues);
            // replace VERITY_PADDING_BLOCK with new one
            if (needPadding) {
                // uint64:  size (excluding this field)
                // repeated ID-value pairs:
                //     uint64:           size (excluding this field)
                //     uint32:           ID
                //     (size - 4) bytes: value
                // (extra dummy ID-value for padding to make block size a multiple of 4096 bytes)
                // uint64:  size (same as the one above)
                // uint128: magic

                int blocksSize = 0;
                for (ApkSigningPayload payload : apkSigningBlock.getPayloads()) {
                    blocksSize += payload.getTotalSize();
                }

                int resultSize = 8 + blocksSize + 8 + 16; // size(uint64) + pairs size + size(uint64) + magic(uint128)
                if (resultSize % ApkUtil.ANDROID_COMMON_PAGE_ALIGNMENT_BYTES != 0) {
                    int padding = ApkUtil.ANDROID_COMMON_PAGE_ALIGNMENT_BYTES - 12 // size(uint64) + id(uint32)
                            - (resultSize % ApkUtil.ANDROID_COMMON_PAGE_ALIGNMENT_BYTES);
                    if (padding < 0) {
                        padding += ApkUtil.ANDROID_COMMON_PAGE_ALIGNMENT_BYTES;
                    }
                    final ByteBuffer dummy =  ByteBuffer.allocate(padding).order(ByteOrder.LITTLE_ENDIAN);
                    apkSigningBlock.addPayload(new ApkSigningPayload(ApkUtil.VERITY_PADDING_BLOCK_ID,dummy));
                }
            }

            if (apkSigningBlockOffset != 0 && centralDirStartOffset != 0) {

                // read CentralDir
                fIn.seek(centralDirStartOffset);

                byte[] centralDirBytes = null;
                File tempCentralBytesFile = null;
                // read CentralDir
                if (lowMemory) {
                    tempCentralBytesFile = new File(apkFile.getParent(), UUID.randomUUID().toString());
                    FileOutputStream outStream = null;
                    try {
                        outStream = new FileOutputStream(tempCentralBytesFile);
                        final byte[] buffer = new byte[1024];

                        int len;
                        while ((len = fIn.read(buffer)) > 0){
                            outStream.write(buffer, 0, len);
                        }
                    } finally {
                        if (outStream != null) {
                            outStream.close();
                        }
                    }
                } else {
                    centralDirBytes = new byte[(int) (fileChannel.size() - centralDirStartOffset)];
                    fIn.read(centralDirBytes);
                }

                //update apk sign
                fileChannel.position(apkSigningBlockOffset);
                final long length = apkSigningBlock.writeApkSigningBlock(fIn);

                // update CentralDir
                if (lowMemory) {
                    FileInputStream inputStream = null;
                    try {
                        inputStream = new FileInputStream(tempCentralBytesFile);
                        final byte[] buffer = new byte[1024];

                        int len;
                        while ((len = inputStream.read(buffer)) > 0){
                            fIn.write(buffer, 0, len);
                        }
                    } finally {
                        if (inputStream != null) {
                            inputStream.close();
                        }
                        tempCentralBytesFile.delete();
                    }
                } else {
                    // store CentralDir
                    fIn.write(centralDirBytes);
                }
                // update length
                fIn.setLength(fIn.getFilePointer());

                // update CentralDir Offset

                // End of central directory record (EOCD)
                // Offset     Bytes     Description[23]
                // 0            4       End of central directory signature = 0x06054b50
                // 4            2       Number of this disk
                // 6            2       Disk where central directory starts
                // 8            2       Number of central directory records on this disk
                // 10           2       Total number of central directory records
                // 12           4       Size of central directory (bytes)
                // 16           4       Offset of start of central directory, relative to start of archive
                // 20           2       Comment length (n)
                // 22           n       Comment

                fIn.seek(fileChannel.size() - commentLength - 6);
                // 6 = 2(Comment length) + 4 (Offset of start of central directory, relative to start of archive)
                final ByteBuffer temp = ByteBuffer.allocate(4);
                temp.order(ByteOrder.LITTLE_ENDIAN);
                temp.putInt((int) (centralDirStartOffset + length + 8 - (centralDirStartOffset - apkSigningBlockOffset)));
                // 8 = size of block in bytes (excluding this field) (uint64)
                temp.flip();
                fIn.write(temp.array());

            }
        } finally {
            if (fileChannel != null) {
                fileChannel.close();
            }
            if (fIn != null) {
                fIn.close();
            }
            ApkUtil.PUT_CHANNEL=true;
        }
    }

    /**
     * 设置渠道信息
     * @param args
     */
    public static void main(String[] args) {
        try {
            put(new File("E:/leidian/MyPront/app/release/app-release.apk"), ApkUtil.APK_CHANNEL_BLOCK_ID,"856230");
        } catch (IOException | SignatureNotFoundException e) {
            e.printStackTrace();
        }
    }
}
SignatureNotFoundException
package com.zw.apk.channel.apk;

/**
 * @author Administrator
 */
public class SignatureNotFoundException extends Exception {
    private static final long serialVersionUID = 1L;

    public SignatureNotFoundException(final String message) {
        super(message);
    }

    public SignatureNotFoundException(final String message, final Throwable cause) {
        super(message, cause);
    }
}

本人技术有限,以上代码复制就可以测试,但是能实现就好,感谢https://www.cnblogs.com/plumsq/p/11589776.html博主提供文档参考

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 219,270评论 6 508
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,489评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 165,630评论 0 356
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,906评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,928评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,718评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,442评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,345评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,802评论 1 317
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,984评论 3 337
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,117评论 1 351
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,810评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,462评论 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,011评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,139评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,377评论 3 373
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,060评论 2 355

推荐阅读更多精彩内容