Regarding the method of re-signing by ADB after the APK cannot be installed after the relevant server is strengthened, the signature is swiped after hardening, and the compressed so library is aligned by zipalign after hardening

Some problems encountered and their answers in work (Android Chapter)

1. After the relevant server is reinforced, the APK cannot be installed and an error is reported

2. After the signature is brushed after hardening, the method of ADB re-signing

3. Align the compressed so library through zipalign after reinforcement

After first hardening:

Failure [INSTALL_PARSE_FAILED_NO_CERTIFICATES: Failed to collect certificates from /data/app/vmdl951451933.tmp/base.apk: Attempt to get length of null array] appears during adb install

This error message roughly means that there is a problem when parsing the APK file. It may be because of a problem with the APK file itself or some unpredictable problems during installation. Here are some possible causes and solutions for this issue:
 1. Check the integrity of the APK file: Please make sure that the APK file you are trying to install is intact and not damaged. If there is a network outage or other problem while downloading the file, it may cause the APK file to become corrupted. It is recommended that you re-download the APK file and try installing again. If you compiled the APK yourself, please check whether there are any errors in the compilation process, such as missing resource files, etc.
 2. Check whether the APK file is signed: If you compiled the APK yourself, you also need to sign your APK file. If the APK file is not signed, it cannot be installed on the device. You can use Android Studio or command-line tools (such as jarsigner) to sign APK files. Note that signing can only be done after the APK is packaged, not during compilation and packaging.
 3. Check if the same APK is already installed on the device: This issue may occur if the APK you are trying to install is the same version that is already installed on the device. In this case, you will need to uninstall the version on your device before attempting to install your APK.
 4. Check if the device has enough space: If the device has insufficient storage space, the installation cannot be performed. Please check your device storage and try to free some storage space.
 5. Check whether the "USB debugging" function is enabled on the device: If the "USB debugging" function is not enabled on your device, ADB may not be able to install the APK file correctly. Make sure your device has "Developer options" enabled and USB debugging turned on.
These are some of the causes and solutions that can cause the "Failure [INSTALL_PARSE_FAILED_NO_CERTIFICATES]" error.

Note: APK may also be damaged when it is sent and received by the communication tool.

After checking that there is no problem with the integrity of the APK, ask the hardening colleague to say that it needs to be re-signed after hardening


How to sign apk through ADB command

To sign the APK through the ADB command, you need to ensure that the Java Development Kit (JDK) and Android SDK have been installed, and the corresponding path has been configured in the system environment variable. Then follow the steps below:
 1. Use keytool to create a new signing keystore (if you have not created a signing keystore before), for example:
keytool -genkey -v -keystore my-release-key.jks -keyalg RSA -keysize 2048 -validity 10000 -alias my-alias
where my-release-key.jks is your keystore filename and my-alias is your key alias. When executing this command, you need to set a password to protect the keystore and key alias.
 2. Use the jarsigner command to sign the APK, for example:
jarsigner -verbose -sigalg SHA1withRSA -digestalg SHA1 -keystore my-release-key.jks my-app.apk my-alias
where my-app.apk is the APK file name you want to sign. When using this command, you need to provide the keystore and key alias you just generated, and the password used to protect the keystore and key alias.
 3. Finally, after signing the APK with jarsigner, use zipalign to optimize the APK file, for example:
zipalign -v 4 my-app.apk my-app-aligned.apk
where my-app-aligned.apk is the optimized APK file name. Note that this command must be executed after signing the APK in the previous step.
 This completes the process of signing the APK via ADB commands. You can install the signed APK file on a device for testing or distribution.

How to sign apk if there is already a signature file

To sign an APK, you first need to create a private key and a certificate. You can use the Java Keytool tool to create a private key and certificate, and then use the jarsigner tool to apply the private key and certificate to the APK file.
 Here are some basic steps:
 1. Create a private key
 Create a private key with the following command:
keytool -genkey -v -keystore my-release-key.jks -keyalg RSA -keysize 2048 -validity 10000 -alias my-alias
This command will generate a Java keystore named my-release-key.jks containing your private key and certificate.
 2. Build the APK file
 Build your APK file using Android Studio or Gradle. Note that you need to build the APK file with the release version, not the debug version.
 3. Sign the APK file
 Apply the private key and certificate to the APK file with the following commands:
jarsigner -verbose -sigalg SHA1withRSA -digestalg SHA1 -keystore my-release-key.jks app-release-unsigned.apk my-alias
This command will sign an APK file named app-release-unsigned.apk with your private key and certificate, and rename it app-release.apk.
 You have now successfully created a signature for your APK file.

How to install the apk signed by ADB command into the system (example)

To install a signed APK to an Android device, you can use the following two methods:
1. Certified app store
   You can upload an APK file to a certified app store, such as Google Play or the Amazon Appstore, so that users can install it through the app store. This allows users to easily download and install applications from the application store.
 2. ADB command line
   You can install signed APK files to Android devices using the ADB command-line tool. Here are the basic steps to install an APK file:
   1. Make sure you have installed the Android SDK and your device is connected to your computer.
   2. Open a command line terminal and move to the directory of the adb tool. For example, if your adb tools are located in the C:\Android\platform-tools directory, you can enter the following command at the command line:
cd C:\Android\platform-tools
3. Upload the APK file to the device with the following command:
adb push myapp.apk /sdcard/
where myapp.apk is the name of your signed APK file.
   4. Install the APK file in the device using the following command:
adb install /sdcard/myapp.apk
If your APK file contains Native libraries, you may need to authorize the device with the following command before installing the APK file:
adb shell pm grant <your-package-name> android.permission.FORCE_STOP
To check your application package name, look in your project's build.gradle file.
   5. After the installation is complete, you can find your app on your device and start using it.

The following error occurred when continuing to install after signing

ADB command install apk reports Failure [INSTALL_FAILED_INVALID_APK: Failed to extract native libraries, res=-2]

This error message indicates that when installing the APK file, the Android system cannot extract the native library in the APK package. This could be due to the following situations:
 1. The native library file is missing or damaged.
   When building the project, if some native library files do not exist or are damaged, the Android system will encounter this problem when installing the APK file. You can check your project and make sure that all required native library files are present and correctly added to the project.
 2. Missing necessary permissions.
   It could be that you are using certain permissions or features in your APK file and not declaring them in your manifest file. Please check your manifest file and make sure you have declared all necessary permissions and capabilities.
 3. Not enough space on the phone.
   If your device is low on space, the Android system cannot extract all the files in the APK package, including native library files. You can try to delete some unnecessary files or applications and reinstall the APK file.
 4. Your APK file has been corrupted.
   If your APK file is corrupted, the Android system won't be able to properly extract all the files during installation. You can try rebuilding and signing the APK file.
 If the above doesn't solve the problem, try installing with the following command:
  `adb install -r -t app-release.apk`
The `-r` parameter is used to replace the existing application, and the `-t` parameter is used to allow the application to be installed on the simulator or device.

Then continue to use adb install -r -t app-release.apk to install and also report Failure [INSTALL_FAILED_INVALID_APK: Failed to extract native libraries, res=-2] error, guessing it may be native The library is compressed and damaged after hardening, so try to use zipalign to align the compressed so library

Reference blog post:

Installation APK prompts Failure [INSTALL_FAILED_INVALID_APK: Failed to extract native libraries, res=-2]

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

https://developer.android.google.cn/studio/command-line/zipalign?hl=zh-cn




If your APK contains shared libraries (.so files), use -p to make sure they align to mmap(2)-fit 4KiB page boundaries. For other files (whose alignment is determined by the required alignment parameter of zipalign ), Android Studio will align to 4 bytes on 32-bit and 64-bit systems.

To align infile.apk and save it as outfile.apk, run the following command:
zipalign -p -f -v 4 infile.apk outfile.apk
To confirm the alignment of existing.apk, use the following command. If you're using Android Studio or AGP to build, you should use this command to verify that the APK is aligned.


zipalign -c -v 4 existing.apk

The following table lists the available zipalign options:

Options Description
-c Only check alignment (does not modify file).
-f Overwrite existing output file.
-h Display tool help.
-p Make uncompressed .so The document is aligned to the page.
-v Verbose output.
-z Recompress with Zopfli.

After the above operations, the APK successfully installs the machine

How to install zipalign on ubuntu

To install zipalign on Ubuntu, you can use the following steps:
 1. Open a terminal and install the Android SDK with the following command:
sudo apt-get install android-sdk
2. Now, install Zipalign using the following command:
sudo apt-get install zipalign
3. If you installed Android Studio on Ubuntu, you can find the location of the zipalign tool. Find the path to the zipalign tool with the following command:
find ~/ -name zipalign
4. If you want to add zipalign to your environment variables, you can add the path in your .bashrc file with the following command:
echo "export PATH=\$PATH:/path/to/zipalign" >> ~/.bashrc
In the above command, replace /path/to/zipalign with the path to the zipalign tool you found in step three.
 Once installed, you can now use the zipalign tool to optimize your APK file.