(Original) The whole process of Android apk application reinforcement, byte alignment, and secondary signature

This blog mainly talks about how to reinforce, align and sign apk applications. Detailed steps will be introduced one by one.

Preface

As major reinforcement tools begin to charge one by one, how to reinforce applications for individual developers or small and medium-sized enterprises has become a problem. In the past, I used the 360 ​​Reinforcement Assistant. Although it is limited to a certain number of free times per day, it can at least be used.
But recently it also prompted me to force an update. After the update, I had to set up the signature, and it became like this:
Insert image description here
As a result of my test, although the 360 ​​reinforcement tool can now provide you with free reinforcement, after the reinforcement, your original apk Your signature will be lost, which means you have to re-sign. Let’s take a look at how to solve this problem

Reinforcement

Needless to say about reinforcement, I still use the 360 ​​reinforcement assistant here. The main interface of
the 360 ​​reinforcement official website is as follows: click "Add Application" to add your apk, or drag it in directly to perform the reinforcement operation. After completion, we will get An apk that has been stripped of its signature but hardened

Insert image description here

Byte alignment

The benefit of byte alignment is to help the operating system index resources more efficiently according to requests and reduce memory consumption.
The Android SDK comes with the byte alignment tool zipalign. Before signing for the second time, it is best to perform a byte alignment operation (usually 4-byte alignment). Although the apk packaged by Android Studio is byte aligned by default, due to the application hardening steps, there is no guarantee that the data in the application is still aligned. Just in case, perform another byte alignment operation.

first step

To perform byte alignment, first modify the apk file you just reinforced and name its suffix .zip (other compression formats are also available, mainly to open the file with compression software). After the modification is completed, open the file and delete it. The "META-INF" folder selected in the red box in the picture below (after the folder has been reinforced by the application, the signature information has been deleted, and there are some more miscellaneous files, which are of no use. If you do not delete the folder, subsequent The signature will not be successful);
Insert image description here
after the deletion is successful, change the suffix of the file to .apk;

Step 2

Copy the apk file to the directory where the zipalign tool is located.
The tool is located in the sdk\build-tools\build version directory.
Generally, you can choose the directory of the version used in your project.
I am using 30.0.3 as
Insert image description here
shown in the picture above. The zipalign.exe in the red box is the byte alignment tool, and ziptest.apk is the apk file that needs to be aligned.

third step

Enter cmd in the address bar of the current directory and press Enter
to enter the command prompt window .
Enter zipalign.exe -v -p 4 input.apk output.apk
(input.apk in the command is the name of the apk file that needs to be aligned, output.apk It is the name of the apk file output after the alignment is completed)
Insert image description here

Omit a large section of code output during operation...
Insert image description here

As shown in the figure above, after the run is completed, the code in the red box appears, indicating that the byte alignment is successful. At the same time, you can see more apk files in the directory after the byte alignment is successful, as shown in the figure below:
Insert image description here
In this way, the alignment is completed
We can also use the command to verify whether the apk is aligned or
put the apk to be verified whether it is aligned in this directory and execute the command:

zipalign -c -v 4 apk名字

For example, verify that ziptest.apk was not aligned just now

zipalign -c -v 4 ziptest.apk

The output result of "succesful" means that it is aligned, and
"FAILED" means that it is not aligned.
The result is as follows:
Insert image description here

secondary signature

After completing the byte alignment, it is time to re-sign the apk.
The signature tool SDK also comes with it, apksigner. This tool is located at the lower level of the lib directory of the same level as the byte alignment tool,
as shown in the figure below:
Insert image description here
The next thing to do is to put our aligned apk and signature file jks into the directory
. Then, open the cmd window in this path. input the command:

java -jar apksigner.jar sign --ks key.jks --ks-key-alias releasekey output.apk

key.jks is the signature file, releasekey is the alias of the signature file, and output.apk is the apk file shown in red box 2 in the above figure.
For example, as shown in the figure below:
Insert image description here
the signature file alias in the command is hidden for privacy reasons. You can replace it with the alias of your own signature file.
If there is no problem with the command line input, the instruction to enter the Keystore password will be displayed below, as shown in the last line of the picture above Keystore password for signer #1: (Note here: there will be no characters displayed in the command prompt window when entering the password. Yes, probably for privacy and security, just enter directly after inputting).
If the signature is successful, there will be no prompt in the command prompt window; otherwise, there will be an error prompt. Just check whether the modification date of the apk file in the directory has changed, for example, as shown in the figure below:
Insert image description here
the time is changed from the previous time to the time after the signature command was successfully executed. To ensure that the signature is successful, you can also enter the following command to check:

java -jar apksigner.jar verify -v output.apk

The output.apk is the file name of the signature file. The successful signature result is as shown in the figure below:
Insert image description here
At this point, the entire process from reinforcement to secondary signature is over.
By the way, I would like to recommend a re-signature tool called Love Encryption. The implementation principle is actually the above.
This tool can avoid manually entering the above commands
Insert image description here

But I tried it myself, but there is still a problem, that is, the alias cannot be displayed.
Anyway, if you can’t use this tool, you can just follow the steps I gave above.

Guess you like

Origin blog.csdn.net/Android_xiong_st/article/details/130853689