26

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.

Olivier Payen
  • 15,198
  • 7
  • 41
  • 70
user1159819
  • 1,549
  • 4
  • 16
  • 29

3 Answers3

38

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>
ranjk89
  • 1,380
  • 16
  • 21
  • 1
    Tried it, does not help – user1159819 Mar 16 '19 at 21:42
  • 1
    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] – user1159819 Mar 17 '19 at 14:05
  • Try adding the following to defaultConfig in app/build.gradle file ` packagingOptions{ doNotStrip "*/armeabi/*.so" doNotStrip "*/armeabi-v7a/*.so" doNotStrip "*/x86/*.so" } ` – ranjk89 Mar 17 '19 at 14:41
  • Also make sure any old version of the apk is uninstalled – ranjk89 Mar 17 '19 at 14:43
  • Can you share relevant extracts from your app/build.gradle and Manifest? – ranjk89 Mar 19 '19 at 20:13
  • Looks like this might be a zipalign issue, can you try setting `extractNativeLibs` to true instead? - https://issuetracker.google.com/issues/37045367 – ranjk89 Mar 20 '19 at 00:13
  • If my answer helped, I'd appreciate if you can accept it as a solution! – ranjk89 Mar 27 '19 at 03:43
  • 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 – appapurapu Mar 11 '20 at 13:40
  • It did help on my side using Xamarin.Android. The issue appeared after manually adding AndroidX components. – Ivan Caravanio Sep 14 '20 at 11:10
  • you deserve a crown! great catch, indeed i am using zipalign and that was the issue. – Gary Klasen Feb 10 '21 at 00:45
10

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
Miha_x64
  • 5,973
  • 1
  • 41
  • 63
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.