合作开发的三方apk, 用我们的platform keystore签名后,无法在android 11的设备上安装成功,一直提示安装错误

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]

在这里插入图片描述
更多关于zip 4字节对齐,请参考https://developer.android.com/studio/command-line/zipalign

开发app的同学,通常大家为了避免每次需要手动签名,都会把签名转化为keystore,在编译阶段直接签名,如此一来确实省去了居多麻烦.但是遇到手动给第三方的apk调试时,遇到上面的报错,就得手动解决了。实话说,签名这一块也没有做过多和深入的研究,就把过程奉上,如果大家遇到相似的问题,有个解决办法.

客户给了个做了v1签名的apk, source.apk

  1. 先删除了客户的v1签名
    zip -d source.apk META-INF/*
  2. 获取4KB对齐apk
    zipalign -v 4 source.apk source_4KB_PBS.apk
  3. 查看是否4KB对齐
    zipalign -c -v 4 source.apk source_4KB_PBS.apk
Verifying alignment of source_4KB_PBS.apk (4)...
      64 res/drawable-hdpi-v4/icon.png (OK)
    3736 res/drawable-ldpi-v4/icon.png (OK)
    6108 res/drawable-mdpi-v4/icon.png (OK)
    8480 res/drawable-xhdpi-v4/icon.png (OK)
   13504 res/drawable-xxhdpi-v4/icon.png (OK)
   21548 res/drawable-xxxhdpi-v4/icon.png (OK)
   32412 res/drawable/icon.png (OK)
   36084 res/drawable/vmware_logo.png (OK)
   49784 res/mipmap-hdpi-v4/ic_launcher.png (OK)
   53436 res/mipmap-hdpi-v4/ic_launcher_foreground.png (OK)
   60672 res/mipmap-hdpi-v4/ic_launcher_round.png (OK)
   66624 res/mipmap-mdpi-v4/ic_launcher.png (OK)
   68916 res/mipmap-mdpi-v4/ic_launcher_foreground.png (OK)
   72944 res/mipmap-mdpi-v4/ic_launcher_round.png (OK)
   76500 res/mipmap-xhdpi-v4/ic_launcher.png (OK)
   81552 res/mipmap-xhdpi-v4/ic_launcher_foreground.png (OK)
   92428 res/mipmap-xhdpi-v4/ic_launcher_round.png (OK)
  100952 res/mipmap-xxhdpi-v4/ic_launcher.png (OK)
  109440 res/mipmap-xxhdpi-v4/ic_launcher_foreground.png (OK)
  129056 res/mipmap-xxhdpi-v4/ic_launcher_round.png (OK)
  143232 res/mipmap-xxxhdpi-v4/ic_launcher.png (OK)
  155808 res/mipmap-xxxhdpi-v4/ic_launcher_foreground.png (OK)
  185248 res/mipmap-xxxhdpi-v4/ic_launcher_round.png (OK)
  206116 res/raw/binary_blob.cer (OK)
  207196 res/raw/binary_blob_playstore.cer (OK)
  208264 resources.arsc (OK)
  213669 AndroidManifest.xml (OK - compressed)
  214973 assets/font/gotham_rounded_bold.otf (OK - compressed)
  277290 assets/font/gothamrounded_book.otf (OK - compressed)
  341189 classes.dex (OK - compressed)
  356833 res/layout/activity_main.xml (OK - compressed)
  357684 res/mipmap-anydpi-v26/ic_launcher.xml (OK - compressed)
  357993 res/mipmap-anydpi-v26/ic_launcher_round.xml (OK - compressed)
  358287 META-INF/com/android/otacert (OK - compressed)
  359510 META-INF/MANIFEST.MF (OK - compressed)
  360687 META-INF/CERT.SF (OK - compressed)
  361963 META-INF/CERT.RSA (OK - compressed)
Verification succesful

我尝试过默认keystore编译出来的apk,默认可以安装成功的apk,通过

apksigner verify -v source_v2_signed.apk

Verifies
Verified using v1 scheme (JAR signing): true
Verified using v2 scheme (APK Signature Scheme v2): true
Verified using v3 scheme (APK Signature Scheme v3): false
Verified using v4 scheme (APK Signature Scheme v4): false
Verified for SourceStamp: false
Number of signers: 1

那我们看下直接v2签名命令出来的apk,需要纠正此前的错误点,只要在v2 签名之前完成了4字节对齐,enable v3签名也能正常安装在android 11的设备上

// apksigner sign --ks (签名地址) --ks-key-alias (别名) --out (签名后的apk地址) (待签名apk地址)
apksigner sign --ks platform.keystore --ks-key-alias androidpublishkey  --out  signed_source_v2_final.apk  source_4KB_PBS.apk

执行签名时,此处需要输入aligs 别名的密码

adb install signed_source_v2_final.apk 安装成功

默认的签名调用逻辑apksigner的源代码
一般位于源码android/tools/apksig.

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐