一、問題描述:
最近Apk適配了Android 11版本,在使用完樂固線上加固后,通過adb命令安裝測試,安裝失敗,報出如下錯誤:
Failure [-124: Failed parse during installPackageLI: Targeting R+ (version 30 and above) requires the resources.arsc of installed APKs to be stored uncompressed and aligned on a 4-byte boundary]
看下官方的說明如下:
Apps that target Android 11 (API level 30) or higher can’t be installed if they contain a compressed resources.arsc file or if this file is not aligned on a 4-byte boundary. This file cannot by memory-mapped by the system if either of these conditions is present. Resources tables that cannot be memory-mapped must be read into a buffer in RAM resulting in unnecessary memory pressure on the system and greatly increased device RAM usage.
翻譯一下:
如果以 Android 11(API 級別 30)或更高版本為目標平臺的應用包含壓縮的 resources.arsc 文件或者如果此文件未按 4 字節邊界對齊,應用將無法安裝。如果存在其中任意一種情況,系統將無法對此文件進行內存映射。無法進行內存映射的資源表必須讀入 RAM 中的緩沖區,從而給系統造成不必要的內存壓力,并大大增加設備的 RAM 使用量。
二、解決方案
1.降級target版本到30以下
2.使用Windows上的軟件加固,測試沒問題
3.先使用Sdk\build-tools下的zipalign工具做對齊操作,再使用apksigner重簽名
這里詳細說一下第三種解決方案:
a.取出加固后的apk包(input.apk),使用Sdk\build-tools下的zipalign工具做對齊操作進行
先進入Sdk\build-tools\30.0.3目錄下,如: cd C:\Sdk\build-tools\30.0.3
//對齊操作命令,4代表對齊為4個字節
zipalign -p -f -v 4 input.apk output_unsigned.apk
//確認對齊結果命令,按需使用
zipalign -c -v 4 output_unsigned.apk
//操作或驗證成功后會看到 Verification succesful
b.使用apksigner對上面的apk進行重簽名
//簽名命令,需輸入密碼,無提示成功
//kotlinDemoKey.jks,output_unsigned.apk為待簽名apk
//最后生成的簽名apk會直接替換原來的這個apk
apksigner sign --ks kotlinDemoKey.jks output_unsigned.apk
/ /驗證簽名結果命令,,按需使用,操作結果如下
apk apksigner verify -v --print-certs output_unsigned.apk