Apktool Failed To Extract Native Libraries Res -2
How to Fix Apktool Failed to Extract Native Libraries Error
If you are an Android developer or enthusiast, you may have encountered the error "Failed to extract native libraries, res=-2" when trying to install a modified APK file on your device. This error can be frustrating and prevent you from testing or using your modified app. In this article, we will explain what causes this error and how to fix it using three different methods.
apktool failed to extract native libraries res -2
What is Apktool and why do you need it?
Apktool is a tool for reverse engineering Android APK files
Apktool is a powerful piece of software developed by XDA Senior Member ibotpeaches. The tool allows you to reverse engineer APK files, allowing you to decode resource files so you can modify them and rebuild them after making some changes. You can use Apktool to compile APK from a modified source, decompile APK, edit and recompile APK files, and make an Android app package.
You can use Apktool to modify, debug, and recompile APK files
Some of the reasons why you may want to use Apktool are:
To change the app icon, name, theme, or layout
To add or remove features or permissions
To inject code or ads into an app
To analyze or debug an app's behavior or performance
To learn how an app works or how it was made
Apktool is compatible with Windows, Linux, and Mac OS systems. You can download it from its official website or from its GitHub repository . You will also need Java Runtime Environment (JRE) installed on your system to run Apktool.
What causes the error "Failed to extract native libraries, res=-2"?
The error occurs when you try to install a modified APK file
The error "Failed to extract native libraries, res=-2" The error "Failed to extract native libraries, res=-2" is a common problem that many Apktool users face when they try to install a modified APK file on their device. The error means that the Android system failed to extract the native libraries (such as .so files) from the APK file, resulting in a failed installation.
The error is related to the extractNativeLibs flag in the AndroidManifest.xml file
One of the possible causes of this error is the extractNativeLibs flag in the AndroidManifest.xml file. This flag determines whether the native libraries should be extracted from the APK file or not. If the flag is set to false, the native libraries will not be extracted and will be loaded directly from the APK file. This can save some disk space and improve performance, but it also requires some conditions to be met, such as:
The APK file must be page aligned with zipalign tool
The native libraries must be uncompressed and stored in the APK file without any padding or alignment
The device must support loading native libraries from APK files
If any of these conditions are not met, the installation will fail with the error "Failed to extract native libraries, res=-2". This can happen if you modify the APK file with Apktool and change some of the native libraries or their compression settings.
The error can also be caused by a missing or outdated aapt tool
Another possible cause of this error is a missing or outdated aapt tool. Aapt stands for Android Asset Packaging Tool and it is a tool that compiles and packages resources for Android applications. Apktool uses aapt to recompile the APK file after making some changes. However, if you don't have aapt installed on your system or if you have an older version of it, Apktool may not be able to recompile the APK file correctly and cause some issues with the native libraries.
How to fix the error "Failed to extract native libraries, res=-2"?
There are several methods that you can try to fix this error and install your modified APK file successfully. Here are three of them:
Method 1: Set extractNativeLibs flag to true in the AndroidManifest.xml file
This method involves setting the extractNativeLibs flag to true in the AndroidManifest.xml file. This will tell the Android system to extract the native libraries from the APK file and store them on the device. This can solve the problem if your device does not support loading native libraries from APK files or if your APK file is not page aligned or has compressed or padded native libraries.
Step 1: Decompile the APK file with Apktool
To decompile the APK file with Apktool, you need to open a command prompt or terminal window and navigate to the folder where you have Apktool and your APK file. Then, type the following command:
apktool d -f -o output_folder apk_file.apk
This command will decompile your APK file and create an output folder with all the files and folders of your app. You can change the name of the output folder and apk_file.apk as you wish.
Step 2: Edit the AndroidManifest.xml file and change the extractNativeLibs attribute to true
Next, you need to open the output folder and find the AndroidManifest.xml file. This is an XML file that contains information about your app, such as its package name, version, permissions, activities, etc. You need to open this file with a text editor and look for the following line:
<application android:extractNativeLibs="false" ... >
This line sets the extractNativeLibs flag to false for your app. You need to change it to true like this:
<application android:extractNativeLibs="true" ... >
This will tell the Android system to extract the native libraries from your app when installing it. Save and close the file after making this change.
Step 3: Recompile the APK file with Apktool
Now that you have edited the AndroidManifest.xml file, you need to recompile your app with Apktool. To do this, go back to your command prompt or terminal window and type the following command:
apktool b -f -o new_apk_file.apk output_folder
This command will recompile your app and create a new APK file with your changes. You can change the name of new_apk_file.apk as you wish.
<h4 Step 4: Sign and align the APK file with apksigner and zipalign tools
Before you can install your new APK file on your device, you need to sign and align it with apksigner and zipalign tools. These are tools that verify and optimize your APK file for distribution. You can find these tools in the Android SDK build-tools folder or download them from the internet.
To sign your APK file with apksigner, you need to have a keystore file that contains your private key and certificate. You can create a keystore file with the keytool command or use an existing one. Then, type the following command:
apksigner sign --ks keystore_file --out signed_apk_file.apk new_apk_file.apk
This command will sign your APK file with your keystore file and create a signed APK file. You can change the name of keystore_file, signed_apk_file.apk, and new_apk_file.apk as you wish.
To align your APK file with zipalign, type the following command:
zipalign -f -v 4 signed_apk_file.apk aligned_apk_file.apk
This command will align your APK file on 4-byte boundaries and create an aligned APK file. You can change the name of signed_apk_file.apk and aligned_apk_file.apk as you wish.
Step 5: Install the APK file on your device
Finally, you can install your aligned APK file on your device. To do this, you need to enable the installation of apps from unknown sources in your device settings. Then, you can use a file manager app or an adb command to transfer and install the APK file on your device. You should be able to install it without any errors.
Method 2: Page align the APK file with zipalign tool and add -p parameter
This method involves page aligning the APK file with zipalign tool and adding -p parameter when recompiling it with Apktool. This can solve the problem if your device supports loading native libraries from APK files but your APK file is not page aligned or has compressed or padded native libraries.
Step 1: Decompile the APK file with Apktool
The first step is the same as in method 1. You need to decompile the APK file with Apktool using the following command:
apktool d -f -o output_folder apk_file.apk
Step 2: Recompile the APK file with Apktool
The second step is slightly different from method 1. You need to recompile the APK file with Apktool using the following command:
apktool b -f -o new_apk_file.apk -p output_folder output_folder
The -p parameter tells Apktool to use the output folder as a framework folder. This will ensure that Apktool does not compress or pad any native libraries in the APK file.
Step 3: Page align the APK file with zipalign tool and add -p parameter
The third step is also different from method 1. You need to page align the APK file with zipalign tool using the following command:
zipalign -f -v -p 4096 new_apk_file.apk aligned_apk_file.apk
The -p parameter tells zipalign to page align the entire APK file, not just individual files within it. This will ensure that the native libraries are stored in the APK file without any padding or alignment.
Step 4: Sign the APK file with apksigner tool
The fourth step is the same as in method 1. You need to sign the APK file with apksigner tool using the following command:
apksigner sign --ks keystore_file --out signed_apk_file.apk aligned_apk_file.apk
Step 5: Install the APK file on your device
The fifth step is also the same as in method 1. You need to install the signed APK file on your device after enabling unknown sources in your device settings.
Method 3: Install ia32-li