I wanted to decompile an existing android app and after modification recompile and sign it, but I can't install on Android 11. I got this error message on Emulator:
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'
My used method:
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
What tool shall I use to solve this problem? As I read this caused by Android 11.
The error message describes what you have to do:
Targeting R+ (version 30 and above) requires the resources.arsc of installed APKs to be stored uncompressed and aligned on a 4-byte boundary'
APK files are ZIP files and in ZIP files every entry can have a different compression algorithm. On of the algorithms does not compress at all, it simply stores the file content - you can see that when opening a ZIP/APK file in 7ZIP. There is a column Method
and for files stored uncompressed you will find the entry Store
.
When decompiling/recompiling apktool considers files that are stored with and without compression. The rebuilt APK will have the same configuration as the source APK file.
The error indicates that you have edit an older APK that has compressed resources.arsc
but after editing it requires to have uncompresed resources.
You can adapt this by editing apktool.yml
before building the recompiled APK. In this file you will find an entry named resourcesAreCompressed:
. make sure it is set to false:
resourcesAreCompressed: false
After building, aligning, signing the APK with apktool this error should be bypassed.
Then align the APK file:
zipalign 4 my.apk my-aligned.apk
and sign it with
apksigner sign --ks-key-alias alias_name --ks my.keystore my-aligned.apk
Please do no longer use jarsigner (not for signing and verifying), especially not after apksigner. jarsigner is outdated and can only create APKv1 signatures which are no longer accepted by recent Android versions. apksigner creates v1 v2 and optionally v3 signature all in one command.