Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[INSTALL_FAILED_INVALID_APK: Failed to extract native libraries, res=-2] after Compile #1626

Closed
Deltafox79 opened this issue Sep 26, 2017 · 23 comments

Comments

@Deltafox79
Copy link

Deltafox79 commented Sep 26, 2017

Information

  1. Apktool Version (apktool -version) -
    2.3.0
  2. Operating System (Mac, Linux, Windows) -
    Windows
  3. APK From? (Playstore, ROM, Other) -
    PlayStore

Stacktrace/Logcat

Include stacktrace here

After recompile apk and signed I have this issue when install recompiled apk...

`Failure [INSTALL_FAILED_INVALID_APK: Failed to extract native libraries, res=-2]

Steps to Reproduce

  1. apktool
    apktool.bat r ..apk

Frameworks

If this APK is from an OEM ROM (Samsung, HTC, LG). Please attach framework files
(.apks that live in /system/framework or /system/priv-app)

APK

If this APK can be freely shared, please upload/attach a link to it.

http://www17.zippyshare.com/v/cE36EDY0/file.html

Questions to ask before submission

  1. Have you tried apktool d, apktool b without changing anything?
    yes
  2. If you are trying to install a modified apk, did you resign it?
    yes
  3. Are you using the latest apktool version?
    yes
@iBotPeaches
Copy link
Owner

Sorry delayed on this. Was going to look into it, but forgot my dev phone today. Just letting you know I read this and have the application locally and will look into it hopefully tomorrow.

@Deltafox79
Copy link
Author

Hi I have update download link any info on this issue? Regards

@danhawkes
Copy link

Think this is triggered by settingextractNativeLibs to false in the manifest. Have reverted a change to set it and the recompiled APK installs successfully.
https://developer.android.com/guide/topics/manifest/application-element.html

@iBotPeaches
Copy link
Owner

Interesting. Apktool should not change values of the Manifest though. I'll run some tests with that info.

 E: application (line=34)
      A: android:label(0x01010001)=@0x7f0d0032
      A: android:icon(0x01010002)=@0x7f0b0000
      A: android:name(0x01010003)="com.bittorrent.client.BTApp" (Raw: "com.bittorrent.client.BTApp")
      A: android:allowBackup(0x01010280)=(type 0x12)0x0
      A: android:hardwareAccelerated(0x010102d3)=(type 0x12)0xffffffff
      A: android:supportsRtl(0x010103af)=(type 0x12)0x0
      A: android:extractNativeLibs(0x010104ea)=(type 0x12)0x0
      A: android:resizeableActivity(0x010104f6)=(type 0x12)0xffffffff
      A: android:roundIcon(0x0101052c)=@0x7f0b0000

AAPT already has it set to 0x0 or false.

@Deltafox79
Copy link
Author

Deltafox79 commented Nov 23, 2017

Yes if I put true the extractNativeLibs apk compiled works.. strange if whit -r option apk have same problems..

Regards

@iBotPeaches
Copy link
Owner

Looked into this some more. If going by the words of @danhawkes & @Deltafox79 that manually changing this extractNativeLibs property to true resolves the booting issue doesn't make much sense.

I confirmed that the property was false in the original application, the decoded original application and the rebuilt (dirty) application. Apktool should not be in the market of random changing properties in the Manifest.

There was some research online that an application needs to be "zipaligned" to resolve this issue. I read that here: scala-android/sbt-android#252

Might be worth testing. Resign, then zipalign and try with the property set to false.

@slowjack2k
Copy link

I can confirm this behavoir with a different apk. It still only works when I set extractNativeLibs to true. With extractNativeLibs false I get INSTALL_FAILED_INVALID_APK

@peret
Copy link
Contributor

peret commented Nov 23, 2018

I can also confirm that this is still an issue in 2.3.4. zipaligning the apk did unfortunately not fix it.
The documentation of the extractNativeLibs attribute says: "If set to false, then your native libraries must be page aligned and stored uncompressed in the APK."

Could the issue be related to apktool compressing the native libs for some reason? From what I can see, .so-files are actually on the doNotCompress list though...

@Deltafox79
Copy link
Author

Hi Any news on this issue? also whit latest apktool-2.4.1-c20659-SNAPSHOT-small.jar this bug is present..

Just use d for decompile apk and manifest change android:extractNativeLibs="false" why false?

Download apk for test :

debug.apk

Regards

@rosasurfer
Copy link

rosasurfer commented Nov 8, 2019

With the current version of the Android build environment the manifest attribute /manifest/application[@android:extractNativeLibs] is always set to false. This means the libraries in the APK are page aligned by the build environment (not to confuse with zipaligning the whole APK) and the loader loads them directly from the APK (presumably storage space considerations). When the ApkTool (version 2.4.0) recompiles such an APK it doesn't change the manifest settings but due to the recompilation the resulting library file layout in the APK is not page aligned anymore, and that's the cause the loader fails and the error occures. Removing the attribute or explicitely setting it to true (the default value) prevents the bug to happen.

@timjosten
Copy link

timjosten commented Aug 9, 2020

@iBotPeaches If extractNativeLibs="false", then .so files should not be ZIP compressed. The system will load .so files directly from APK binary file. It won't work if .so files are compressed.

For a dirty fix, you should always change this attribute to true.
For a proper fix, you should implement new code to not compress .so files if this attribute is set to false, but compress everything else.

@iBotPeaches
Copy link
Owner

@iBotPeaches If extractNativeLibs="false", then .so files should not be ZIP compressed. The system will load .so files directly from APK binary file. It won't work if .so files are compressed.

For a dirty fix, you should always change this attribute to true.
For a proper fix, you should implement new code to not compress .so files if this attribute is set to false, but compress everything else.

Thanks for the research. I see. So the property extraction is required if true, if false is just uses random access to stream file.

@giacomoferretti
Copy link
Contributor

Seems to work with 2.4.2-d3f9d5-SNAPSHOT.

After rebuilding, zipalign it using this command: zipalign -p -f 4 input.apk output.apk
Then sign it with apksigner and should work now.

@bletham
Copy link

bletham commented Oct 10, 2020

I was running into this issue (in apktool 2.4.1, and using zipalign after jarsigner) and @giacomoferretti's approach worked for me.

@iBotPeaches
Copy link
Owner

This should be handled now with proper handling of compression. Zipaligning may be required to align to 4byte boundries.

Closing.

@scottkennedy
Copy link

I'm having this issue on 2.6.0. Removing the property from the manifest allows it to work.

@cbrevik
Copy link

cbrevik commented Dec 15, 2021

Seems to work with 2.4.2-d3f9d5-SNAPSHOT.

After rebuilding, zipalign it using this command: zipalign -p -f 4 input.apk output.apk Then sign it with apksigner and should work now.

Chiming in to say that the important part for me was that I had to add the -p flag to do alignment of .so files in the apk

@baptx
Copy link

baptx commented Dec 22, 2022

I still got this error even with the latest version 2.7. The workaround with zipalign -p did not work for me and I cannot set extractNativeLibs="true" or remove this option from AndroidManifest.xml because of another issue that requires apktool -r to not decompile resources (https://stackoverflow.com/questions/23317208/apktool-build-apk-fails/23719727#23719727).
But I have another workaround to share, you just remove the lib folder from the APK file with an archive manager like file-roller, then install the APK (after having aligned and signed the file), make sure the app is stopped in Android settings, fill the lib folder with the subfolder from an original installation in /data/data/YOUR_APP_ID/lib, change permissions with chown -R system:system on the folder added if needed, start the app.

To sign the app, I used these commands (from https://blog.securityevaluators.com/bypassing-okhttp3-certificate-pinning-c68a872ca9c8):

keytool -genkey -v -keystore my-release-key.keystore -alias alias_name -keyalg RSA -keysize 2048 -validity 10000
jarsigner -verbose -sigalg SHA1withRSA -digestalg SHA1 -keystore my-release-key.keystore <patched.apk> alias_name

Note that according to https://developer.android.com/studio/command-line/zipalign, if we use jarsigner, we have to align the APK after, not before (as we can see on https://medium.com/@felipecsl/bypassing-certificate-pinning-on-android-for-fun-and-profit-1b0d14beab2b).

@gelldur
Copy link

gelldur commented Aug 26, 2023

When you add extra .so file maybe you try inject libfrida-gadget.so then:

  1. Add libfrida-gadget.so to apktool.yml file (check section doNotCompress:) and add there your file
  2. apktool b target
  3. zipalign -p -f 4 target.apk aligned.apk
  4. java -jar uber-apk-signer-1.3.0.jar --skipZipAlign --apks app/dist

In my case it worked. No changes to manifest and I decompiled with apktool d -rs

@Starmania
Copy link

adding frida in the doNotCompress list in apktool.yml also solve my problem

@ego-lay-atman-bay
Copy link

I am also having this issue in 2.9.1, although I haven't tried any of the fixes in this thread yet.

@HackJJ
Copy link

HackJJ commented Jan 24, 2024

The fix to change extractNativeLibs to true works but seems a bodge.

@firdiar-mita
Copy link

When you add extra .so file maybe you try inject libfrida-gadget.so then:

  1. Add libfrida-gadget.so to apktool.yml file (check section doNotCompress:) and add there your file
  2. apktool b target
  3. zipalign -p -f 4 target.apk aligned.apk
  4. java -jar uber-apk-signer-1.3.0.jar --skipZipAlign --apks app/dist

In my case it worked. No changes to manifest and I decompiled with apktool d -rs

i decompile using apktool d -r
following those 3 steps allowing the app to be installed, but when i open it it crash immidiately
despite im not changing anything n source

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests