Java服務(wù)打包動(dòng)態(tài)Android渠道

Z前言

由于我單位正在做游戲推廣秋秤,每次要做渠道號(hào)得去三方上傳應(yīng)用 ,過(guò)程時(shí)間長(zhǎng)還能麻煩,所以就打算自己做一個(gè)服務(wù)來(lái)打包渠道信息跟伏,于是就有了下面的經(jīng)歷

歷程

1、一開(kāi)始我是打算在apk中做多渠道打包翩瓜,需要什么渠道信息就打一個(gè)安裝包受扳,這樣效率低還很麻煩,不方便業(yè)務(wù)隨時(shí)弄
2兔跌、APK本身就是個(gè)ZIP壓縮包嘛勘高,我把他解壓縮,在里面中放一個(gè)配置文件坟桅,然后下載的時(shí)候华望,WEB服務(wù)器根據(jù)用戶(hù)信息的不同動(dòng)態(tài)替換這個(gè)配置文件,最后重新壓縮成apk仅乓,然后APP運(yùn)行的時(shí)候赖舟,讀取這個(gè)配置文件就可以了。代碼寫(xiě)好了夸楣,生成的安裝包放手機(jī)一安裝宾抓,安裝包解析失敗豫喧!
3石洗、于是在GitHub還有百度和谷歌各種找資料,很多博客也都寫(xiě)了Java動(dòng)態(tài)添加apk配置文件紧显,修改配置讲衫,可是代碼都是很老的了,現(xiàn)在系統(tǒng)最高都12了孵班,估計(jì)到時(shí)候會(huì)去適配改代碼很麻煩涉兽,于是就找到了一個(gè)忘zip中的commont區(qū)中添加信息的博客,這也是一種思路重父,于是就開(kāi)啟了復(fù)制代碼之旅花椭,往comments區(qū)添加洗洗,讀取信息都可以房午,但是只是勾選了V1簽名
4矿辽、最后高版本對(duì)安裝包解析很?chē)?yán)格,每個(gè)廠(chǎng)商對(duì)apk檢測(cè)很?chē)?yán),最終在GitHub上找到美團(tuán)的項(xiàng)目https://github.com/Meituan-Dianping/walle袋倔,在美團(tuán)的多渠道打包工具提供了一個(gè)walle-cli-all.jar包雕蔽,經(jīng)歷多個(gè)挫折最終找到他的核心代碼和另外一個(gè)博主的博客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("獲取數(shù)據(jù):"+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 >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 >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;
        }
    }

    /**
     * 設(shè)置渠道信息
     * @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);
    }
}

本人技術(shù)有限,以上代碼復(fù)制就可以測(cè)試宾娜,但是能實(shí)現(xiàn)就好批狐,感謝https://www.cnblogs.com/plumsq/p/11589776.html博主提供文檔參考

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市前塔,隨后出現(xiàn)的幾起案子嚣艇,更是在濱河造成了極大的恐慌,老刑警劉巖华弓,帶你破解...
    沈念sama閱讀 206,311評(píng)論 6 481
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件食零,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡寂屏,警方通過(guò)查閱死者的電腦和手機(jī)贰谣,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,339評(píng)論 2 382
  • 文/潘曉璐 我一進(jìn)店門(mén),熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)迁霎,“玉大人吱抚,你說(shuō)我怎么就攤上這事】剂” “怎么了秘豹?”我有些...
    開(kāi)封第一講書(shū)人閱讀 152,671評(píng)論 0 342
  • 文/不壞的土叔 我叫張陵,是天一觀(guān)的道長(zhǎng)昌粤。 經(jīng)常有香客問(wèn)我憋肖,道長(zhǎng),這世上最難降的妖魔是什么婚苹? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 55,252評(píng)論 1 279
  • 正文 為了忘掉前任,我火速辦了婚禮鸵膏,結(jié)果婚禮上膊升,老公的妹妹穿的比我還像新娘讥蟆。我一直安慰自己亚亲,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 64,253評(píng)論 5 371
  • 文/花漫 我一把揭開(kāi)白布乙濒。 她就那樣靜靜地躺著债查,像睡著了一般非区。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上盹廷,一...
    開(kāi)封第一講書(shū)人閱讀 49,031評(píng)論 1 285
  • 那天征绸,我揣著相機(jī)與錄音,去河邊找鬼。 笑死管怠,一個(gè)胖子當(dāng)著我的面吹牛淆衷,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播渤弛,決...
    沈念sama閱讀 38,340評(píng)論 3 399
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼祝拯,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了她肯?” 一聲冷哼從身側(cè)響起佳头,我...
    開(kāi)封第一講書(shū)人閱讀 36,973評(píng)論 0 259
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎晴氨,沒(méi)想到半個(gè)月后康嘉,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 43,466評(píng)論 1 300
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡瑞筐,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 35,937評(píng)論 2 323
  • 正文 我和宋清朗相戀三年凄鼻,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片聚假。...
    茶點(diǎn)故事閱讀 38,039評(píng)論 1 333
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡块蚌,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出膘格,到底是詐尸還是另有隱情峭范,我是刑警寧澤,帶...
    沈念sama閱讀 33,701評(píng)論 4 323
  • 正文 年R本政府宣布瘪贱,位于F島的核電站纱控,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏菜秦。R本人自食惡果不足惜甜害,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,254評(píng)論 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望球昨。 院中可真熱鬧尔店,春花似錦、人聲如沸主慰。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 30,259評(píng)論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)共螺。三九已至该肴,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間藐不,已是汗流浹背匀哄。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 31,485評(píng)論 1 262
  • 我被黑心中介騙來(lái)泰國(guó)打工秦效, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人拱雏。 一個(gè)月前我還...
    沈念sama閱讀 45,497評(píng)論 2 354
  • 正文 我出身青樓棉安,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親铸抑。 傳聞我的和親對(duì)象是個(gè)殘疾皇子贡耽,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 42,786評(píng)論 2 345

推薦閱讀更多精彩內(nèi)容