packer-ng-plugin|下一代安卓打包工具

jopen 8年前發布 | 12K 次閱讀 Android Java Android開發 移動開發

packer-ng-plugin 是下一代Android渠道打包工具Gradle插件,支持極速打包, 1000 個渠道包只需要 5 秒鐘,速度是 gradle-packer-plugin 1000 倍以上,可方便的用于CI系統集成,支持自定義輸出目錄和最終APK文件名,依賴包: com.mcxiaoke.gradle:packer-ng:1.0.+ 簡短名: packer ,可以在項目的 build.gradle 中指定使用,還提供了命令行獨立使用的Java和Python腳本。

實現原理

PackerNg原理

優點

  • 使用APK注釋字段保存渠道信息和MAGIC字節,從文件末尾讀取渠道信息,速度快

  • 實現為一個Gradle Plugin,支持定制輸出APK的文件名等信息,方便CI集成

  • 提供Java版和Python的獨立命令行腳本,不依賴Gradle插件,支持獨立使用

  • 由于打包速度極快,單個包只需要5毫秒左右,可用于網站后臺動態生成渠道包

缺點

  • 沒有使用Android的productFlavors,無法利用flavors條件編譯的功能

文件格式

Android應用使用的APK文件就是一個帶簽名信息的ZIP文件,根據 ZIP文件格式規范 ,每個ZIP文件的最后都必須有一個叫 Central Directory Record 的部分,這個CDR的最后部分叫"end of central directory record",這一部分包含一些元數據,它的末尾是ZIP文件的注釋。注釋包含 Comment LengthFile Comment 兩個字段,前者表示注釋內容的長度,后者是注釋的內容,正確修改這一部分不會對ZIP文件造成破壞,利用這個字段,我們可以添加一些自定義的數據,PackerNg項目就是在這里添加和讀取渠道信息。

細節處理

原理很簡單,就是將渠道信息存放在APK文件的注釋字段中,但是實現起來遇到不少坑,測試了好多次。

ZipOutputStream.setComment

FileOutputStream is = new FileOutputStream("demo.apk", true);ZipOutputStream zos = new ZipOutputStream(is);
zos.setComment("Google_Market");
zos.finish();
zos.close();ZipFile zipFile=new ZipFile("demo.apk");System.out.println(zipFile.getComment());

使用Java寫入APK文件注釋雖然可以正常讀取,但是安裝的時候會失敗,錯誤信息是:

adb install -r demo.apk
Failure [INSTALL_FAILED_INVALID_APK]

原因未知,可能Java的Zip實現寫入了某些特殊字符導致APK文件校驗失敗,于是只能放棄這個方法。同樣的功能使用Python測試完全沒有問題,處理后的APK可以正常安裝。

ZipFile.getComment

上面是ZIP文件注釋寫入,使用Java會導致APK文件被破壞,無法安裝。這里是讀取ZIP文件注釋的問題,Java 7里可以使用 zipFile.getComment() 方法直接讀取注釋,非常方便。但是Android系統直到API 19,也就是4.4以上的版本才支持 ZipFile.getComment() 方法。由于要兼容之前的版本,所以這個方法也不能使用。

解決方法

由于使用Java直接寫入和讀取ZIP文件的注釋都不可行,使用Python又不方便與Gradle系統集成,所以只能自己實現注釋的寫入和讀取。 實現起來也不復雜,就是為了提高性能,避免讀取整個文件,需要在注釋的最后加入幾個MAGIC字節,這樣從文件的最后開始,讀取很少的幾個字節就可以定位 渠道名的位置。

幾個常量定義:

// ZIP文件的注釋最長65535個字節
static final int ZIP_COMMENT_MAX_LENGTH = 65535;
// ZIP文件注釋長度字段的字節數
static final int SHORT_LENGTH = 2;
// 文件最后用于定位的MAGIC字節
static final byte[] MAGIC = new byte[]{0x21, 0x5a, 0x58, 0x4b, 0x21}; //!ZXK!

讀寫注釋

Java版詳細的實現見 PackerNg.java ,Python版的實現見 ngpacker.py

寫入ZIP文件注釋:

public static void writeZipComment(File file, String comment) 
throws IOException {
    byte[] data = comment.getBytes(UTF_8);
    final RandomAccessFile raf = new RandomAccessFile(file, "rw");
    raf.seek(file.length() - SHORT_LENGTH);
    // write zip comment length
    // (content field length + length field length + magic field length)
    writeShort(data.length + SHORT_LENGTH + MAGIC.length, raf);
    // write content
    writeBytes(data, raf);
    // write content length
    writeShort(data.length, raf);
    // write magic bytes
    writeBytes(MAGIC, raf);
    raf.close();
}

讀取ZIP文件注釋,有兩個版本的實現,這里使用的是 RandomAccessFile ,另一個版本使用的是 MappedByteBuffer ,經過測試,對于特別長的注釋,使用內存映射文件讀取性能要稍微好一些,對于特別短的注釋(比如渠道名),這個版本反而更快一些。

public static String readZipComment(File file) throws IOException {
    RandomAccessFile raf = null;
    try {
        raf = new RandomAccessFile(file, "r");
        long index = raf.length();
        byte[] buffer = new byte[MAGIC.length];
        index -= MAGIC.length;
        // read magic bytes
        raf.seek(index);
        raf.readFully(buffer);
        // if magic bytes matched
        if (isMagicMatched(buffer)) {
            index -= SHORT_LENGTH;
            raf.seek(index);
            // read content length field
            int length = readShort(raf);
            if (length > 0) {
                index -= length;
                raf.seek(index);
                // read content bytes
                byte[] bytesComment = new byte[length];
                raf.readFully(bytesComment);
                return new String(bytesComment, UTF_8);
            }
        }
    } finally {
        if (raf != null) {
            raf.close();
        }
    }
    return null;
}

讀取APK文件,由于這個庫 packer-helper 需要同時給Gradle插件和Android項目使用,所以不能添加Android相關的依賴,但是又需要讀取自身APK文件的路徑,使用反射實現:

// for android code
private static String getSourceDir(final Object context)
        throws ClassNotFoundException,
        InvocationTargetException,
        IllegalAccessException,
        NoSuchFieldException,
        NoSuchMethodException {
    final Class<?> contextClass = Class.forName("android.content.Context");
    final Class<?> applicationInfoClass = Class.forName("android.content.pm.ApplicationInfo");
    final Method getApplicationInfoMethod = contextClass.getMethod("getApplicationInfo");
    final Object appInfo = getApplicationInfoMethod.invoke(context);
    final Field sourceDirField = applicationInfoClass.getField("sourceDir");
    return (String) sourceDirField.get(appInfo);
}

Gradle Plugin

這個和舊版插件基本一致,首先是讀取渠道列表文件,保存起來,打包的時候遍歷列表,復制生成的APK文件到臨時文件,給臨時文件寫入渠道信息,然后復制到輸出目錄,文件名可以使用模板定制。主要代碼如下:

// 添加打包用的TASK
def archiveTask = project.task("apk${variant.name.capitalize()}",
                type: ArchiveAllApkTask) {
            theVariant = variant
            theExtension = modifierExtension
            theMarkets = markets
            dependsOn variant.assemble
        }
        def buildTypeName = variant.buildType.name
        if (variant.name != buildTypeName) {
            project.task("apk${buildTypeName.capitalize()}", dependsOn: archiveTask)
        }


// 遍歷列表修改APK文件
theMarkets.each { String market ->
            String apkName = buildApkName(theVariant, market)
            File tempFile = new File(tempDir, apkName)
            File finalFile = new File(outputDir, apkName)
            tempFile << originalFile.bytes
            copyTo(originalFile, tempFile)
            PackerNg.Helper.writeMarket(tempFile, market)
            if (PackerNg.Helper.verifyMarket(tempFile, market)) {
                copyTo(tempFile, finalFile)
            } 
        }

詳細的實現可以查看文件 PackerNgPlugin.groovy 和文件 ArchiveAllApkTask.groovy

來自: http://www.oschina.net/p/packer-ng-plugin?fromerr=YmfduTsR

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!