Android app decompilation (2)-APK package decompilation Analysis
Chapter 2 decompilation of APK package
The code and resources processed by the compiler have been packaged into APK, and some have even been converted into binary files. However, we also have some methods to change the compiled content back to the original form. This process is called-decompilation.
Decompilation is divided into two parts: a resource file and a java file. The so file is the result of c ++ compilation and can be considered as non-decompiled (decompiled into assembly instructions, which is probably not understood by most people ).
2.1 decompile resource files
APK tool is a powerful tool for decompilation. It is said that it is an official decompilation tool provided by Google. It can unpackage the APK directory and restore all the resource files to the original format, dex files are decompiled into files in smail format (instead of the original java file we want ). The smail format file is the syntax used by the android Virtual Machine for execution. We will not explain this part.
2.1.1 prepare to download the tool on the official APK tool website
APKToolThe latest version;
2. Download the [script file-APKTool. bat] (https://raw.githubusercontent.com/iBotPeaches/Apktool/master/scripts/windows/apktool.bat) Supporting 'apktool' on the official website. The content is as follows;
@echo offif "%PATH_BASE%" == "" set PATH_BASE=%PATH%set PATH=%CD%;%PATH_BASE%;java -jar -Duser.language=en "%~dp0\apktool.jar" %1 %2 %3 %4 %5 %6 %7 %8 %9
However, this script has a small problem-you need to remove it.apktool.jarThe previous one\After the download is complete, open the script in a text editor and modify the script,
@echo offif "%PATH_BASE%" == "" set PATH_BASE=%PATH%set PATH=%CD%;%PATH_BASE%;java -jar -Duser.language=en "%~dp0apktool.jar" %1 %2 %3 %4 %5 %6 %7 %8 %9
3. decompress the downloaded APKTool package and put it in the directory you want to store it with the script file, for example, 'd: \ Work \ apktool ';
4. for ease of use, add the storage directory to the system's environment variables; right-click 'my pc' and choose 'properties'> 'advanced system settings'> 'Environment variable '. Select 'path' and edit it;
Enter the path 'd: \ Work \ apktool' of apktool at the end of the editing column. If there are other paths before this option, separate them;
5. Start the 'COMMAND line Windows' and enter 'apktool-version'. If the following content is displayed, the installation is successful;
$ apktool -version2.0.3
Now, APKTool is installed and configured.
If you cannot access the official website to download the latest version, log on to Andou.
2.2.2 decompilation Resources
Next, we will start using APKTool,
1. StartCmd command line window;
2. Assume that the apk is placed in D: \ Test.apk.Directory, enterApktool d: \ Test.apk, Start DecompilationTest.apk ';
$ apktool d D:\Test.apkI: Using Apktool 2.0.3 on Test.apk I: Loading resource table... I: Decoding AndroidManifest.xml with resources... I: Loading resource table from file: C:\Users\Gym\apktool\framework\1.apk I: Regular manifest package... I: Decoding file-resources... I: Decoding values */* XMLs... I: Baksmaling classes.dex... I: Copying assets and libs... I: Copying unknown files... I: Copying original files...
3. The compilation result isCmd command line windowAt the startup location, the decompiled directory structure is as follows,
Now the resources under the res directory are restored, and the original design is displayed. The java source code is decompiled into an smli file.
#2.3 decompilation of java source code to view the java source code, there are three methods: 1. convert the dex file to the smail file, and then use a dedicated tool to translate the smail into java; 2. convert the dex file into a class file in the general compilation format of java, and then use a dedicated tool to translate the class into java; 3. convert the dex file into a jar file, and then use a dedicated tool to translate the jar file into java. Here we use the third method. ### 2.3.1 prepare the decompilation tool first.
1. Download the [dex2jar] (http://sourceforge.net/projects/dex2jar/files/) tool on the official website, which can convert the dex file in the APK into a jar file;
2. After downloading the package, decompress the package and put it in the directory you want to store, for example, 'd: \ Work \ dex2jar ';
3. Add the storage directory to the environment variables of the system for ease of use;
4. Download the [JD-GUI] (http://jd.benow.ca/) on the official website, it can view the java source code of the jar file;
5. After downloading the package, decompress the package and place it in the directory you want to store, for example, 'd: \ Work \ jd-gui ';
Now, The Decompilation tool for java source code is installed and set up. If you cannot access the official website to download, please go to [Andou network] (http://anddle.com/resource) download, there will be a total of these tools packaged for your convenience. ### 2.3.2 decompile the code and start to use the decompile tool,
1. decompress the APK file and find 'class. dex 'in the Android software installation package ';
2. use the dex2jar tool to set classes. dex to generate the jar file. Start the 'COMMAND line window of cmd 'and enter 'd2j-dex2jar D: \ classes. dex 'to start decompiling 'classes. dex ', 'd2j-dex2jar' must follow the path of the dex file to be decompiled;
$ d2j-dex2jar D:\classes.dex dex2jar classes.dex -> .\classes-dex2jar.jar
The result is generated.classes-dex2jar.jarFile;
3. Enter the JD-GUI directory, run the JD-GUI tool, open the jar file generated above, you can see the java source code.