Unpacking, packaging and modification of APK file
I believe everyone who plays computer is familiar with APK files. You may have to deal with APK files every day, whether it is installing or uninstalling useful application tools, plug-ins, fun games and so on. . . Do you know what these APK files accompany you every day? How to change them? For example, the English version is sinicized, the function is modified, the text description is modified, and advertisements are removed. This paper introduces the basic knowledge and structure of APK, the unpacking, packaging and signing of APK files, and the daily modification of APK files.
1 Introduction. APK file
APK is the abbreviation of Android Package, that is, Android application package file or Android installation package. Every application to be installed on the Android platform should be compiled and packaged into a separate file with the suffix. apk。 APK file is a package compiled by professional software eclipse, which contains binary code, resources, configuration files and so on. It can be installed by directly transferring APK files to Android phones for execution. APK file is actually in zip format, but the extension is changed to apk, which can be opened directly with decompression software. After decompression by WinRAR or unzip, you will see several files and folders. A typical APK file usually consists of the following contents:
Global configuration file of AndroidManifest.xml program.
Class.dex Darwich bytecode
Compiled binary resource file of resources.arsc
META-INF\ Signature information is stored in this directory.
Res\ This directory holds resource files.
Assets\ This directory can hold some configuration files.
The following are some basic descriptions and introductions of these files and directories.
AndroidManifest.xml
This file is a file that every application must define and include. It describes the name, version, permissions, referenced library files and other information of the application. You need to open it before you can read it.
Classes.dex file
Classes.dex is a java bytecode file generated by compiling java source code. Dex is the full name of Dalvik VM executes, that is, Android Dalvik Executor, which is not the bytecode of Java ME but the bytecode of Dalvik.
resources.arsc
Compiled binary resource file.
Meta-information catalogue
META-INF directory stores signature information to ensure the integrity of apk package and the security of the system. When eclipse compiles and generates apk packages, it will check and calculate all the files to be packaged, and put the calculation results in the META-INF directory. This ensures that the files in the apk package cannot be replaced at will. For example, after getting an apk package, it is basically impossible to directly decompress, replace and repackage a picture, a piece of code or a piece of copyright information. This makes virus infection and malicious modification more difficult and helps to protect the security of the system.
Res catalog
Res directory stores resource files. Including pictures, character strings and so on.
After unpacking, almost all possible modifications and edits are basically here.
Inventory of assets
Assets directory can store some configuration files, and the contents of these files can be obtained through the relevant API during the running of the program.
2. Unpacking and packaging. APK file
APK file is a software package compiled and generated by professional software eclipse. Many softwares can be found online to decompile the contents of apk. For example, you can unlock APK's androidmanifest.xml: Java-jar AXMLPrinter2.jar androidmanifest.xml through axmlprinter2 tools and commands. Recently, the industry has a powerful unpacking and packaging toolkit, apktool, which can be used to unpack and package APK files conveniently and quickly under Windows, bringing a lot of convenience to modification and editing. Let's introduce its use.
1) APKtool software package
The APKtool software package consists of two programs: apktool.jar and aapt.exe.
In addition, a batch file apktool.bat is provided, which is as follows:
Java-jar " % ~ dp0 \ apk tool . jar " % 1% 2% 3% 4% 5% 6% 7% 8% 9
Running apktools.jar requires a java environment (version 1.6.0 or above).
The apktool.jar is used for unpacking, and the apktool.jar and aapt.exe are used for packaging together.
2) open the APK file
Let's take the solution of Contacts.apk as an example. First, copy Contacts.apk to the current working directory (for example, Test). Type the command in DOS.
Apktool d contact
Here "d" means decoding. Contact person. APK is the apk file to unpack. ABC is a subdirectory name. All decompressed files will be placed in this subdirectory.
3) Packaging of 3)APK documents
Type the command in DOS.
Apk tool babac new contact. apk
Here "b" means packing.
ABC is a subdirectory name, which is generated during decompression and used to store all decompressed and modified files.
New contact. APK is a new apk file generated after packaging.
4) Framework document
When unpacking APK files, apktool needs a framework file (framework-res.apk) to decode and package. The Apktool already contains the standard framework, so when unpacking most APK files, there is no need to provide additional framework files. However, some manufacturers use their own framework documents. In order to understand this package, they must extract the framework file (framework-res.apk) from the mobile phone and install it on the computer. The installation command is:
apktool if framework-res.apk
After installation, you will get: ~ \ apktool \ framework \1.apk.
5) Unpacking, unpacking and signature batch processing.
In actual use, multiple APK files may be processed. It is convenient to write it as a batch file. Packaging and signing can be done at one time.
Unpacking batch command:
For %%i in (*. D%% i _%%i & amp& amp move _%% i to modify the file. & Copy %%i back up the original file & @ echofile [%%i] Unpacking process is complete!
Package and sign batch commands:
For/d%% i in (*) do cd .. & jar b modify _ file \%% i & java-jar sign apk.jar test key.x509.pemtest key.pk8 modify _ files \% i \ dist \ *. APK %%i & Ampren%% iNew%% I & moved the new%% iModified signature file & amp& amp@ echo%% I finished repackaging the signature! & amp& ampcd modify _ file
3. Application example: editing and modification after opening APK.
Why did you open the APK file? Of course, it is necessary to make necessary changes to its content. Modify what? Usually, if you just replace the image, there is no need to unpack it. Open APK file with WinRAR and replace it directly. However, if you want to modify non-image content such as text, you can only decode it by unpacking. The following tasks require unpacking and packaging APK. This is just a general introduction. Please find out the detailed operation methods and tutorials yourself.
1) Chinese APK software
In the res folder, we can see that there are many folders whose values are-* * *, that is, language packs. Values is an English language pack, values-zh is a China (including Hong Kong, Macao and Taiwan) language pack, values-zh-rCN is a simplified Chinese language pack (including Chinese mainland only), and values-zh-rTW is a traditional Chinese language pack (Hong Kong, Macao and Taiwan). In addition, language packs in other regions are simplified objects, so you may not know much about them.
In the values folder, there are usually language files such as arrays.xml and strings.xml If you want to localize these files into Chinese, you must modify them. Sometimes you need to modify other xml files and look at them carefully one by one.
2) Modify the icon label
Each APK file has an "icon label". After the APK program is installed on the mobile phone, the icon label text will be displayed below the icon. The contents of this icon label can be modified. Find strings.xml under \res\values and modify a line:
& ltstring name = "app _ name"> icon label
For example:
Similarly, if it is a widget, you need to modify widget_name.
Note: apk under system/app should not be modified, because the corresponding odex file should be modified at the same time.
3) Delete the advertisement of APK.
There are many APK applications with advertisements. In order to delete advertisements in the program, it is necessary to modify the contents related to advertisements in the main.xml file. Find the main.xml file in the \res directory. Usually in the \layout directory, sometimes in other directories. Even, sometimes there is no main.xml file, and the advertising line is placed in other xml files. Only one file can be searched carefully at a time. In either case, check its contents and you will see a similar command, as shown below. This is an advertising display.
& ltcom . admob . Android . ads . ad view Android:id = " @ id/ ad " Android:layout _ width = " fill _ parent " Android:layout _ height = " wrap _ content " admobsdk:background color = " # ff 000000 " admobsdk:text color = " # ffffffff " admobsdk:keywords = " Android application "/& gt;
Amend as follows:
& ltcom . admob . Android . ads . ad view Android:id = " @ id/ ad " Android:layout _ width = " 0.0 dip " Android:layout _ height = " 0.0 dip " admobsdk:background color = " # ff 000000 " admobsdk:text color = " # ffffffff " admobsdk:keywords = " Android application "/& gt;
As you can see, the key is to change the fill_parent to 0.0dip, change the wrap_content to 0.0dip, and keep everything else unchanged. This change is not for advertising space, of course, you can't see the advertisement.
4) Change the display battery to 1% accuracy.
On XT502, only 7 files are displayed by default: 0%, 10%, 20%, 40%, 60%, 80%, 100%. By modifying the framework-res.apk, the display accuracy can be changed. However, on XT502, it has been proved that the best possible realistic accuracy is only 10%. The modification work is as follows:
(1) Open the frame package-res.apk.
(2) Modify and add battery status icon
(3) modify the file stat _ sys _ battery.xml
(4) modify the stat _ sysstat _ sys _ battery _ charge.xml file.
(5) packaging
(6) extract stat_sys_battery.xml, stat_sys_battery_charge.xml, resources.arsc and an icon directory: drawable-mdpi.
(7) Reassemble the frame-res.apk.
5) The status bar information informs the text color to be modified.
The text color of the status bar information notification is controlled by the colors.xml file under res\values in the framework-res.apk file, so we only need to modify the colors.xml file. In addition, this file also controls the text color of the drop-down bar and can be modified.
Open the colors.xml file with a text editor and find it.
& ltcolor name = " HW _ status bar _ time " & gt; # ff000000 & lt/color & gt;
Amend this sentence to read:
The status bar information informs that the text color changes from black to white.