APK construction process

APK construction process

Insert picture description here

  1. aapt compiles the resource file to generate R.java and packs the resource file
  2. Java compiler Java compiler Compile Java code and aidl into .class file
  3. The dex tool compiles .class files into .dex files
  4. apkbuilder packages the above files into .apk packages
  5. jarsigner signs the apk package
  6. zipalign bytecode optimization

aapt/aapt2: Android resource packaging tool (build-tools/27.0.2/under Android sdk) Full name: Android Asses Packaging Tool
aidl: A tool for converting Android interface description language into .java files for cross-process communication
javac: Java compilation period
proguard : Code obfuscation tool
dx/d8: Convert .class files to .dex files that Davik VM can recognize
apkbuilder: package to generate apk package
jarsigner: signature tool
zipalign: bytecode alignment optimization tool

aapt

Android resource packaging tool (build-tools/27.0.2/under Android sdk) full name: Android Asses Packaging Tool

Resource consolidation

Insert picture description here

Resource file compilation

Insert picture description here

res resources
  • Generate R.java file. Give each non-assets resource an ID value, which is defined in the R.java file in the form of bed cooler.
  • Generate the resources.arsc file to describe the configuration information of those resources with ID. Its content is equivalent to a resource index table. A data set that contains all ID values. In the file, if a certain ID corresponds to a String, then the file will directly contain the value, if the resource corresponding to the ID is a certain layout or drawable resource, then the file will be stored in the path of the corresponding resource

The structure diagram of R.java is as follows:
Insert picture description here

R.String.appname =0x7f0f0000;
0x7f represents the resource package being compiled,
0f represents the string type, this 0f is not static,
0000 represents the string number 0000 is the first string

resource.arsc file (check the resource.arsc file structure through the Android Studio Analyze apk function as follows, from the figure we can conclude that resource.arsc is a resource index table)
Insert picture description here

AndroidManifest file

The AndroidManifest file is the global configuration file of the Android application, package name, permissions, various components, version number, etc., edited as a binary xml file

assets

It is directly packaged into the apk as shown below

aidl file compilation

aidl:Android Interface Definition Language

  • The aidl tool parses the interface definition file, and then generates the corresponding Java code interface provider call. If the aidl file is not used in the project, you can skip this step.
  • Input: the file with aidl suffix, located in the project src/main/aidl directory.
  • Output: C/S-side Java code that can be used for process communication, located in build/generated/source/aidl
Java source code compilation
  • The Java files generated by R.java and aidl, plus the source code of the project, are compiled with javac to generate class files.
  • Input: java source code folder (also includes R.java generated by aapt, Java files generated by aidl, already BuildConfig.java)
  • Output: For gradle compilation, the generated class file is saved in build/intermediates/classes.
proguard code obfuscation
  • After javac completes the code compilation, it will generally obfuscate the source code, similar to encryption, in order to increase the difficulty of decompilation, while also shortening the code name and reducing the size of the code.
  • Input: the compiled .class file, the obfuscation rule configuration is proguard-rules.pro
  • Output: the obfuscated .class file, the mapping file before and after obfuscation.
Convert to DEX file
  • The dx tool generates classes.dex files that can be executed by the Android system virtual machine. dx converts classes to Dalvik bytecode, generates constant pools, and eliminates redundant data.
  • Input: all .class files.
  • Output: classes.dex file.
Package the apk file
  • The apk file is generated by packaging. The old apkbuilder script has been abandoned and is now packaged through the ApkBuilder class of sdklib.jar.
  • Input: .ap_ resource package files, classes.dex files, uncompiled resource files (assets resources, etc.), libs and other files.
  • Output: apk file
Sign apk file
  • Sign the apk file and then install it on the device.
  • Input: The .apk file and signature file (debug or Release Keystore) generated in the previous step.
  • Output: apk file after signing.
zipalign optimization
  • Zipalign performs class-alignment processing on the signed apk file, so that the start offset of all resource files in the apk is an integer multiple of 4 bytes, so that the apk file is accessed faster through memory mapping. It also reduces memory consumption when the class is running on the device.
  • Input: signed apk file
  • Output: Aligned and optimized apk file

APK typical file

  • AndroidManifest.xml program global configuration file
  • classes.dex Dalvik bytecode
  • resources.arsc resource index table
  • META-INF This directory stores signature information
  • res stores resource files in this directory
  • assets This directory can store some configuration or resource files

Guess you like

Origin blog.csdn.net/yanwenyuan0304/article/details/106337631