1. Decompile the code
1. To decompile java code, you first need to download the tool dex2jar . The download address is: https://sourceforge.net/projects/dex2jar/files/
. The latest version is 2.0. After downloading, unzip it.
2. Rename the apk file to be decompiled to zip format and decompress it. Pay attention to the classes.dex file, which stores all the java code. Copy the classes.dex file to the root directory after decompression of dex2jar.
3. Open cmd, enter the root directory where dex2jar is decompressed, and execute the command:
d2j-dex2jar classes.dex
As shown below:
After the command is executed, the classes-dex2jar.jar file will be generated in the corresponding directory.
4. To view the java code, you also need to download the tool jd-gui . The download address is: http://jd.benow.ca/ . The latest version is 1.4.0. After downloading, unzip it and use jd-gui.exe Open the jar file decompiled above:
At this point, the java code has been successfully decompiled
. As for the resource files, look back at the decompressed file corresponding to the apk file just now. All xml files are garbled, such as AndroidManifest.xml :
Of course, there are still ways to restore the original resource files, continue reading.
2. Decompile resources
1. To decompile the resource files in the apk, you need the tool apktool . Download address: http://ibotpeaches.github.io/Apktool/install/ . Enter the download page:
Follow the instructions 1 and 2 in the figure to download the two files apktool.bat and apktool.jar . The latest apktool is 2.1.1 and put them in the same folder:
,
2. Place the apk file to be decompiled in the apktool folder, open cmd, enter the apktool folder directory, and execute the command:
apktool d test.apk
As shown below:
After successful execution, a test folder will be generated in the current directory :
Among them, the res folder stores all the decompiled resources, the smali folder stores all the decompiled codes, and AndroidManifest.xml is the manifest file that has been decompiled and restored. The smali file in the smali folder uses the register language used by the Android virtual machine. If you can understand the smail file, you can modify the logic of the source code. It is terrible... Of course, this is not our focus.
At this time, check the AndroidManifest.xml file and find that it has been successfully decompiled:
3. Repackage
1. Now that the resource file has been successfully decompiled, we can modify something appropriately, such as changing the icon, changing the layout file, etc. Here we change the channel value in AndroidManifest.xml to 10001 . Then start repackaging, also switch to the apktool folder directory in cmd, and execute the command:
apktool b test -o new_test.apk
As shown below:
After successful execution, a new new_test.apk file will be generated in the current directory:
2. However, this apk file cannot be installed currently because it needs to be re-signed. If there is no signature file, you can easily generate one through Android Studio. Place the prepared signature file in the root directory of the apktool folder and continue to execute the command in cmd:
jarsigner -verbose -sigalg SHA1withRSA -digestalg SHA1 -keystore <签名文件名> -storepass <签名密码> <待签名Apk文件名> <签名别名>
As shown below:
Note that the jarsigner command file is stored in the bin directory of jdk. The bin directory needs to be configured in the system's environment variables before this command can be executed anywhere.
3. After the signature is completed, it is recommended to perform an alignment operation on the APK file, which can make the program run faster on the Android system. The alignment operation uses the zipalign tool, which is in the **/ build -tools/** directory. , this directory needs to be configured into the system environment variable before this command can be executed anywhere. Continue to execute the command in cmd:
zipalign 4 new_test.apk new_test_aligned.apk
After successful execution, an aligned new_test_aligned.apk file will be generated:
4. Finally, you can use the following command to verify whether the apk signature is successful:
jarsigner -verify -verbose -certs new_test_aligned.apk
As shown below:
At this point, the apk decompilation and repackaging process has been introduced.
at last
If you want to become an architect or want to break through the 20-30K salary range, then don't be limited to coding and business, you must be able to select and expand, and improve your programming thinking. In addition, good career planning is also very important, and learning habits are important, but the most important thing is to be able to persevere. Any plan that cannot be implemented consistently is empty talk.
If you have no direction, here I would like to share with you a set of "Advanced Notes on the Eight Major Modules of Android" written by the senior architect of Ali, to help you organize the messy, scattered and fragmented knowledge systematically, so that you can systematically and efficiently Master various knowledge points of Android development.
Compared with the fragmented content we usually read, the knowledge points of this note are more systematic, easier to understand and remember, and are arranged strictly according to the knowledge system.
Welcome everyone to support with one click and three links. If you need the information in the article, just scan the CSDN official certification WeChat card at the end of the article to get it for free↓↓↓ (There is also a small bonus of ChatGPT robot at the end of the article, don’t miss it)
PS: There is also a ChatGPT robot in the group, which can answer everyone’s work or technical questions.