Targeting SDK Android Q results in Failed to finalize session : INSTALL_FAILED_INVALID_APK: Failed to extract native libraries, res=-2

12,197

Solution 1

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>

Solution 2

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

Solution 3

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.

Share:
12,197

Related videos on Youtube

user1159819
Author by

user1159819

Updated on September 15, 2022

Comments

  • user1159819
    user1159819 almost 2 years

    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.

    • Francis
      Francis over 5 years
      same issue on real device (running Q)
  • user1159819
    user1159819 over 5 years
    Tried it, does not help
  • user1159819
    user1159819 over 5 years
    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]
  • ranjk89
    ranjk89 over 5 years
    Try adding the following to defaultConfig in app/build.gradle file ` packagingOptions{ doNotStrip "/armeabi/.so" doNotStrip "/armeabi-v7a/.so" doNotStrip "/x86/.so" } `
  • ranjk89
    ranjk89 over 5 years
    Also make sure any old version of the apk is uninstalled
  • ranjk89
    ranjk89 over 5 years
    Can you share relevant extracts from your app/build.gradle and Manifest?
  • ranjk89
    ranjk89 over 5 years
    Looks like this might be a zipalign issue, can you try setting extractNativeLibs to true instead? - issuetracker.google.com/issues/37045367
  • ranjk89
    ranjk89 over 5 years
    If my answer helped, I'd appreciate if you can accept it as a solution!
  • appapurapu
    appapurapu over 4 years
    With latest gradle version 3.6.0 or greater, android sdk tools 28.0.3 or higher, gradle automatically replacing extractNativeLibs false. So better to use -p option to zipalign commend as like below zipalign -v -p 4 app.apk app-aligned.apk
  • Ivan Caravanio
    Ivan Caravanio almost 4 years
    It did help on my side using Xamarin.Android. The issue appeared after manually adding AndroidX components.
  • Gary Klasen
    Gary Klasen over 3 years
    you deserve a crown! great catch, indeed i am using zipalign and that was the issue.