我想反编译现有的 Android 应用程序,修改后重新编译并签名,但我无法在 Android 11 上安装。我在模拟器上收到此错误消息:
Error code: '-124', message='-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'
我使用的方法:
Manual Process:
Step 1: Generate Keystore (only once)
You need to generate a keystore once and use it to sign your unsigned apk. Use the keytool provided by the JDK found in %JAVA_HOME%/bin/
keytool -genkey -v -keystore my.keystore -keyalg RSA -keysize 2048 -validity 10000 -alias app
Step 2 or 4: Zipalign
zipalign which is a tool provided by the Android SDK found in e.g. %ANDROID_HOME%/sdk/build-tools/24.0.2/ is a mandatory optimization step if you want to upload the apk to the Play Store.
zipalign -p 4 my.apk my-aligned.apk
Note: when using the old jarsigner you need to zipalign AFTER signing. When using the new apksigner method you do it BEFORE signing (confusing, I know). Invoking zipalign before apksigner works fine because apksigner preserves APK alignment and compression (unlike jarsigner).
You can verify the alignment with
zipalign -c 4 my-aligned.apk
Step 3: Sign & Verify
Using build-tools 24.0.3 and newer
Android 7.0 introduces APK Signature Scheme v2, a new app-signing scheme that offers faster app install times and more protection against unauthorized alterations to APK files (See here and here for more details). Therefore, Google implemented their own apk signer called apksigner (duh!) The script file can be found in %ANDROID_HOME%/sdk/build-tools/24.0.3/ (the .jar is in the /lib subfolder). Use it like this
apksigner sign --ks-key-alias alias_name --ks my.keystore my-app.apk
and can be verified with
apksigner verify my-app.apk
The official documentation can be found here.
Using build-tools 24.0.2 and older
Use jarsigner which, like the keytool, comes with the JDK distribution found in %JAVA_HOME%/bin/ and use it like so:
jarsigner -verbose -sigalg SHA1withRSA -digestalg SHA1 -keystore my.keystore my-app.apk my_alias_name
and can be verified with
jarsigner -verify -verbose my_application.apk
我该用什么工具来解决这个问题?当我读到这篇文章时,这是由 Android 11 引起的。
错误消息描述了您必须执行的操作:
针对 R+(版本 30 及更高版本)要求已安装 APK 的 resources.arsc 未压缩存储并在 4 字节边界上对齐'
APK 文件是 ZIP 文件,在 ZIP 文件中,每个条目都可以有不同的压缩算法。其中一种算法根本不压缩,它只是存储文件内容 - 您可以在 7ZIP 中打开 ZIP/APK 文件时看到这一点。有一个列
Method
,对于未压缩存储的文件,您将找到条目 Store
。
反编译/重新编译 apktool 时会考虑压缩和未压缩存储的文件。重建的 APK 将具有与源 APK 文件相同的配置。
该错误表明您编辑了一个已压缩的旧APK,但编辑后需要具有未压缩的资源。 您可以在构建重新编译的 APK 之前通过编辑
resources.arsc
来调整他的内容。在此文件中,您将找到名为 apktool.yml
的条目。确保它设置为 false:resourcesAreCompressed:
使用 apktool 构建 APK 后,应该可以绕过此错误。
然后对齐APK文件:
resourcesAreCompressed: false
并用签名
zipalign 4 my.apk my-aligned.apk
jarsigner