Decompile APK to obtain code & resources


Introduction to this section

"Decompile Apk" seems to be very high-end, but in fact it is not the case. It just uses some decompilation software to decompile our APK to obtain the source code, pictures, XML resources and other files of the program; I don't know Have you ever done this before? You see someone else’s APP interface is very beautiful, or you like someone else’s picture material. To make it simpler, we can download someone else’s APK and then change the suffix name to xxx. zip, then unzip: The author unzipped an APK at random:

We can open the res directory and get the picture materials inside

However, this method will only obtain some bitmap file resources such as .png or .jpg. If it is an xml resource, we will find that it is garbled when we open it. And if we want to see the Java code of the APK program, It doesn't work either, because they are all packaged into the classes.dex file! But decompilation can solve your needs ~ In addition, do not use decompilation to do illegal things, such as repackaging other people's APKs with your own signature and then publishing it to the relevant market... In addition, we refer to other people's code , rather than a complete copy! ! ! Remember! !

1. Three tools to prepare

  1. apktool:Get resource files, extract image files, layout files, and some XML resource files

  2. dex2jar:Decompile APK into Java source code (convert classes.dex into jar file)

  3. jd-gui:View the converted jar file in 2, that is, view the Java file For the convenience of readers, here are three packages together and put them on the cloud disk. You can download them if needed: Three tools related to decompilation.zip


2. Use apktool to decompile the APK to obtain images and XML resources:

After decompressing the downloaded apktool, we can see the following files (ignore the two csdn, one is the decompiled apk, and the other is the decompiled file):


Next, double-click cmd.exe, come to the command line, type: apktool.bat d csdn.apk, and press Enter:

Then you can see the generated csdn folder, which contains the resources we want.


Okay, now you have the XML resource, right? The picture materials are also available!


3. Use dex2jar to convert classes.dex into a jar file:

Unzip the downloaded dex2jar folder, and copy the classes.dex in the apk to the directory where dex2jar.bat is located:

Open cmd and go to this directory: type: d2j-dex2jar.bat classes.dex

Then we can see that a jar package is generated:

Okay, the conversion is complete!


4. Use jd-gui to view the Java code in the jar package:

OK, open the jd-gui folder

After opening, open the converted jar package in our 3, we can see the code inside:

The csdn client does not obfuscate the code...maybe it is in the spirit of open source, let us learn the code! Generally speaking, apk release will Obfuscate, and then perform some encryption, or use a third-party encryption platform, the more commonly used "Love Encryption". If you are interested, you can also check Baidu for a more detailed introduction!


Summary of this section

Okay, that’s it for introducing the decompilation of APK. I believe you are already gearing up and want to try it. Then give it a try. One final reminder, don’t do bad things! Respect the fruits of other people's labor! In addition, that's it for the first chapter about environment construction and some common development techniques. From the next section, we will start the second chapter of this series of tutorials - learning about common UI controls in Android! Because there are many related basic controls, there are probably dozens. If you keep learning the controls, it may not be interesting. You may write tutorials in parallel, learning one control every day + some other knowledge points. In this way, the author will come up with ideas, so stay tuned~Thank you~