39

Once I switch my target api to 'Q' I cannot install the APK on Android Q Emulator. I get error:

Failed to finalize session : INSTALL_FAILED_INVALID_APK: Failed to extract native libraries, res=-2

Android Studio (v3.3.2) recommends I uninstall apk first. I tried uninstalling apk and I still get the same error. App work if I downgrade target api to 28.

2
  • 1
    same issue on real device (running Q) Commented Mar 15, 2019 at 9:28
  • same error on my pixel 2 Commented Mar 27, 2019 at 7:19

10 Answers 10

61

This happens because of an issue with zipalign, see - https://issuetracker.google.com/issues/37045367. You need to set extractNativeLibs in your Application Tag on AndroidManifest.xml

<application android:allowBackup="false" android:label="@string/app_name" android:extractNativeLibs="true" ... > 

If you are using adb to install the apk try adding -t flag

adb install -t <path-to-apk> 
Sign up to request clarification or add additional context in comments.

11 Comments

Tried it, does not help
adb install -t apk-free.apk Performing Streamed Install adb: failed to install apk-free.apk: Failure [INSTALL_FAILED_INVALID_APK: Failed to extract native libraries, res=-2]
Try adding the following to defaultConfig in app/build.gradle file ` packagingOptions{ doNotStrip "/armeabi/.so" doNotStrip "/armeabi-v7a/.so" doNotStrip "/x86/.so" } `
Also make sure any old version of the apk is uninstalled
Can you share relevant extracts from your app/build.gradle and Manifest?
|
13

If you want android:extractNativeLibs="false", use zipalign with -p key in order to page align ELFs within ZIP:

zipalign -p 4 app.apk app-aligned.apk 

Comments

4

For my case I solved it by adding block codes below into build.gradle inside android block.

android { ... packagingOptions { jniLibs { useLegacyPackaging true } } } 

Comments

3

Faced similar sort of error while upgrading sdk 33 to 34. As suggested, we should use useLegacyPackaging inside build.gradle rather than android:extractNativeLibs in AndroidManifest.

When i updated build.gradle with useLegacyPackaging true, able to install apk .

Comments

1

I also got this error in AWS Device Farm. Turns out they have sdk version 21 installed and my minSdkVersion was set to 24. Lowering my minSdkVersion to 21 resolved this. This error was getting returned on trying to install apk on the test device. Hope this helps for anyone else setting up device farm on android.

Comments

1

Finally, after trying all the answers which didn't work out, I found a solution that worked for me.

Remember, if you are running it on the emulator/device, the minSDK value should match with the Android SDK number installed in that emulator/device.

In my case, I was using vanillaIcecream Android version, so I changed the minSDK value to 35. Hence, just change the minSDK value in the app/build.gradle file to the required value and see the magic!

Comments

1

In my case I was not using latest gradle version and compatible android version in emulator.

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
1

Here the detailed step by step solution Issue in debug console

  1. Change the Android Manifest file accordingly. add the line : android:extractNativeLibs="true" path:android/app/src/main/AndroidManifest.xml

Android Manifest file

  1. Change the app level build.gradle file accordingly.
 android { namespace = "com.example.driving_test" compileSdk = flutter.compileSdkVersion ndkVersion = flutter.ndkVersion packagingOptions { jniLibs { useLegacyPackaging = true } } 

path: android/app/build.gradle

buila.gradle

Comments

1

I was getting this after upgrading minSdk from 21 to 26, and only when running APK on Android 15 16kb page size emulator.

Solution was to add useLegacyPackaging in build.gradle

jniLibs { useLegacyPackaging = true } 

https://developer.android.com/reference/tools/gradle-api/7.1/com/android/build/api/dsl/JniLibsPackagingOptions#uselegacypackaging

Whether to use the legacy convention of compressing all .so files in the APK. If null, .so files will be uncompressed and page-aligned when minSdk >= 23. 

So I guess it's the combo of these

  • minsdk was updated 21 -> 26

    • JNIs delivery format will change
  • native library in project (which is already compiled with pagesize 16)

  • 16kb page size target device

  • v34 buildtools and gradle 8.1.4 not supporting fully packaging 16kb compatible package

I guess upgrading newer gradle+tools would fix it as well.

Comments

0

In my case,my issue was related to the emulator I was using

Device: Resizable (Experimental) API 35

Using the same system image on Pixel Fold, the app was able to install without any issues.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.