一、问题描述:
最近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