The author

Hello everyone, my name is Jia 😊;

I graduated from Guangdong University of Technology in 2010 and joined the 37 Mobile Game Android team in June 2020.

Currently, I am mainly responsible for the development of Domestic game release and Android.

background

Recently, I have been in touch with the business related to game package generation channel package, which involves a knowledge point related to Android called “Decompilation of Android Apk”. I think besides being busy with daily business, I also need to spend some time to accumulate technical points (such as writing blog) to help myself grow.

Although I don’t understand a lot when I first enter the workplace, I believe that a little bit of accumulation will be very helpful.

Finally, I hope this article can also be useful for android students who just entered the workplace and game publishing industry to communicate with each other and make progress together.

APK content

Apk is actually a ZIP file that looks like the following when opened:

【 meta-inf 】

Signature information is stored to ensure the integrity of APK packages and system security. It contains three files, manifest. MF, cert. SF and cert.rsa

1) manifest.mf: lists all apK files and their base64-encoded SHA1 hash, as follows:

Name: classes.dex
SHA1-Digest: l8Lc+gtdLvtxJ1K+kscUmWQGQ1E=
Copy the code

2) Cert. SF: lists the hash values of each message in the manifest.mf file, as follows:

Name: classes.dex
SHA1-Digest: ubmMiUS24rU/6K5JloMVG4vHKIU=
Copy the code

Above the hash value ubmMiUS24rU / 6 k5jlomvg4vhkiu = corresponding is MANIFEST. The MF in the following a few lines of the hash value of the string

Name: classes.dex
SHA1-Digest: l8Lc+gtdLvtxJ1K+kscUmWQGQ1E=
Copy the code

3) cert. RSA: contains some information after the public and private keys are signed

[RES directory]

This directory is mainly for storing resources

1) Animator: XML file used to define attribute animations

2) Anim: XML file that defines gradient animation. (Property animations can also be saved in this directory, but the animator/ directory is preferred for property animations to distinguish between the two types.)

3) color: XML file used to define a list of color states. See Color State List resources 4) Drawable: bitmap files (.png,.9.png,.jpg,.gif) or XML compiled into the following subtypes of drawable object resources

5) MIPmap: drawable object files suitable for different initiator icon densities.

6) Layout: XML file used to define the layout of the user interface

7) Menu: XML file used to define application menus such as options menu, context menu, or submenu

8) RAW: Any file to be saved in its original form

Content after APK decompilation Apktool decompilation APK content is as follows:

1) Assets: some media-related resources are not compressed when aAPT is packaged, and there is no resource ID value.

2) META-INF: signature file related to APK package.

3) lib: so file related.

4) res: some resource files used by APK. The AAPT tool compresses XML files to save space, so they are binary files.

5) class_xxx.dex file: executable file of dalvik VM (Java ->class->dex).

6) Androidmanifest.xml: manifest file, aAPT tool also compressed into binary file.

7) resources.arsc: Resources. arsc converts the ID of the Resource into the name of the Resource file, which is then loaded by AssetManager. The file resources. arsc is stored in the APK package, which is generated by the AAPT tool during the packaging process. It is an index table of Resources, which maintains the corresponding relationship of resource ID, Name, Path or Value. The ID of the resource can be used to find the file or data corresponding to the resource. After dragging an APK into an AS, resources. Arsc will look like this:

8) Other folders: APK will use, but not a common structure.

APK decompiles related processes

Apktool decompilation is handled differently for different folders. Here is the general flow of decode (describing the general flow) :

The following steps will be shown in red, according to the apktool source code to explain:

1) Main entry categories of the program:

Some parameters are set before decompilation, such as D for decompilation and B for backcompilation

Resource. Arsc:

Call order: SetTargetSdkVersion – getResTable- loadMainPkg- getResPackagesFromApk- – loadLoadLoadLoadLoadLoadAckagesFromapk – And store resources.arsc in mResTable.

3) Decode the androidmanifest.xml file:

Call order: decodeManifestWithResources — “decodeManifest -” decode

When decoding the file, some information about the APK will be saved here, such as package, versionCode, versionName, minSdkVersion, targetSdkVersion.

4) Decode res folder:

DecodeResourcesFull — “decode –” fileDecoder. Decode — “decode –” 1 Raw file, directly copy 2. XML file, need to be decoded 3.9patch, need to be decoded. 4 Copy other file formats.

Create values folder:

Contents of values folder:

The file content is generated from the Resources.arsc content.

Call order: pkG.listValuesFiles — “mResSpecs. Values () –” arscdecoder.readEntry () — “mpkg.addresSpec (spec)

For different types of XML files, the generation method is also different.

Call to order: AndrolibResources. GenerateValuesFile – “AndrolibResources. SerializeToResValuesXml

Public. The content of the XML:

Public.xml is used to fix the ID because apkTool uses the resource ID when compiling the resource, and the resource ID is compiled randomly. So we’re going to have to use some fixed values to fix it. The id value here is an integer value, 8 bytes; Consisting of three parts:

PackageId+TypeId+EntryId

PackageId: is the Id of the package. The default value is 0x7F for third-party applications on Android and 0x01 for system applications.

TypeId: indicates the TypeId of the resource. Attr = drawable, layout, dimen, string, style, etc. These values are increments from 1, and the order cannot be changed. Attr =0x01, drawable=0x02…. It takes two bytes.

EntryId: Indicates the id of the resource entity of a specific type. The value starts from 0 and increases in ascending order. It occupies four bytes.

6) Generate smali folder:

Here we need to use a tool package, Dexlib, which is used to process dex files. After processing dex files, we can give the tool class Baksmali to generate SMali files.

7) Generate libs, Lib, assets folders:

These files are copied directly without any changes to these resources

8) Generate the unknown folder

Unknown file refers to the contents of non-APK fixed files: “classes.dex”, “AndroidManifest.xml”, “resources.arsc”, “res”, “r”, “R”, “lib”, “libs”, “assets”, “META-INF”, “kotlin”

The file is stored in the unknown folder and the file path is recorded.

9) Create Original folder:

Put androidmanifest.xml and meta-INF in this directory.

10) Record uncompressed files

Records files that are not compressed and reads the file compression level. If the file compression level is not compressed or the file meets the following types, the file is not compressed.

11) Generate apktool.yml file

The contents of the apktool.yml file are as follows:

1.putUsesFramework(meta); Record the framework used —- usesFramework configuration items

2.putSdkInfo(meta); Record sdkVersion information. —- sdkInfo Configuration item

3.putPackageInfo(meta); Record the packageInfo. —- packageInfo Configuration item

4.putVersionInfo(meta); Record the package versionCode, versionName —- versionInfo configuration item

5.putSharedLibraryInfo(meta); Whether the record is a library. —- sharedLibrary configuration item

6.putSparseResourcesInfo(meta); I haven’t understood this yet. – sparseResources configuration

7.putUnknownInfo(meta); Record an unknown file. —- unknownFiles configuration items

8.putFileCompressionInfo(meta); Record file compression information. —- doNotCompress Configuration item

conclusion

Students who have problems or need to communicate in the process can scan the TWO-DIMENSIONAL code to add friends, and then enter the group for problems and technical exchanges, etc.