Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”
This article has participated in the “Digitalstar Project” and won a creative gift package to challenge the creative incentive money.
preface
Before analyzing the installation process, it’s important to understand how the Android project is compiled -> packaged to produce the final installation package in.apk(.aab) format. Google has an official image depicting the packaging process for APK, as shown below.
The Android application module building process (shown above) follows these general steps:
- 1. The compiler converts your source code into a DEX (Dalvik executable) file, which contains bytecodes for running on Android devices, and everything else into the compilation resource.
- 2. APKPackager combines the DEX file and compiled resources into an APK. However, before you can install and deploy your application to Android devices, you must sign the APK.
- 3. APKPackager signs your APK using a debug or publish keystore:
- 3.1 If you are building a debug version of your application, that is, an application that you intend to use only for testing and analysis, the wrapper will sign your application using the debug keystore. Android Studio automatically configures new projects using a debug keystore.
- 3.2 If you are building a release version of an application that you intend to distribute externally, the wrapper will sign your application using the release keystore.
- Before generating the final APK, the packager uses the Zipalign tool to optimize your application to use less memory when running on the device.
At the end of the build process, you will have a debug or release APK for your application that can be deployed, tested, or released to external users.
The above is the official introduction. Now let’s begin to understand.
When you start a new project, Android Studio automatically creates some of these files for you and fills them with reasonable default values. Therefore, no matter that a complete Android project may contain multiple modules, the content of each module can be divided into two parts from a macro perspective:
-
Resources Resource file
-
Java or Kotlin source code.
Therefore, the compilation and packaging process of the whole project is also completed for these two parts, as shown below:
Compilation phase
Resources Resource file
Resource files include various XML files, animations, drawable images, audio and video files in the RES directory of your project. The AAPT tool is responsible for compiling these resource files in the project. All resource files are compiled and processed, and XML files (except drawable images) are compiled into binary files, so you cannot open XML files directly after decompressing APK. However, resources in assets and raw directories are not compiled and are packaged into apK compressed packages intact.
The result of compiling the resource file consists of two parts: the resources.arsc file and an R file. The former holds a resource index table, while the latter defines the resource ID constants. The combination of the two will find the corresponding resource reference in the code. As shown below:
As you can see, the resource ID in the R file is a 4-byte unsigned integer, expressed in hexadecimal. The highest byte indicates the Package ID, the second highest byte indicates the Type ID, and the lowest two bytes indicate the Entry ID.
Resources. Arsc is equivalent to a resource index table, also known as a map map table. The map key is the resource ID in R. Java, and the value is the path of the corresponding resource. Arsc contains additional information about resources. Arsc files. For details on how to parse resources.
The source code section
The source code in the project is first compiled by javac into.class bytecode files, which are then optimized into.dex files by the dx tool along with.class files in the dependent tripartite libraries. If there is subcontracting, multiple.dex files may also be generated.
In fact, the source code files also include.java files generated after the AIDL interface files are compiled. If an Android project contains.aidl interface files, these.aidl files will be compiled into.java files.
Packaging phase
Finally, use the tool APK Builder to package the compiled resource and.dex files into APK, as well as some other resources. Examples include the Androidmanifest.xml manifest file and the dynamic library.so file used in the tripartite library.
APK file structure
Android Package APK(Android Package APK) can be regarded as a zip Package. You can change. APK to.
- Assert: A native resource file to store, accessed through the AssetManager class
- Lib: native library file
- META-INF: Stores signature information to ensure the integrity of APK packages and system security. When the APK is installed, the application manager verifies the files in the package based on the corresponding algorithm. If the verification result is inconsistent with the meta-INF content, the APK will not be installed.
- Cert. SF: Generates the key relative to each file
- Manifest. MF: indicates the digital signature information
- Res: The resource file system automatically generates the ID of the resource file in r.java, so it is easy to access the resource file through r.xx.id
- Androidmanifest.xml: Each application must define and contain information that describes the application’s name, version permissions, reference library files, and so on. Androidmanifest.xml in APK is compressed and can be unwrapped using the AXMLPrinter2 tool.
- Classes. dex: JAVA bytecode files generated by compiling JAVA source code. However, dalvik VIRTUAL machine used by Android is incompatible with standard JAVA virtual machine. Dex files are different from class files in both file structure and Opcode.
- Resources.arsc: compiled binary resource file.
Once apK is created, it cannot be used directly. You need to use the tool Jarsigner to sign it, because the Android system does not install programs that are not signed. After the signature, the META_INF folder is generated, which holds the files related to the signature.
The PackageManagerService(PKMS) checks the validity of the signature certificate in the APK during installation. Details are described later.
Normally, the signed APK should be installed and used normally, but the actual packaging process will use zipalign tool to optimize the OPERATION of APK.
Zipalign: is a zip archive alignment tool. It aligns uncompressed resources (pictures, videos, etc.) in APK relative to the beginning of the file. This way, you can access these files directly from Mmap (2) without copying the data in RAM and reducing the memory usage of your application.
APK files should be optimized using Zipalign before being distributed to end users. If you’re building with Android Studio, this step will be done automatically.
Mmap (2) : Mmap, munmap – maps or unmaps files or devices into memory.
At this point a complete APK installation package has been created.
The entire compilation and packaging process can be described as follows:
AIDL: AIDL is one of the inter-process Communication (IPC) modes in Android. AIDL is the abbreviation of Android Interface Definition Language. For Xiaobai, The purpose of AIDL is to allow you to bind a Service from another APP to your APP, so that your APP can interact with other apps. Note: You need to use AIDL only if you need clients from different applications to access the service via IPC and you want multithreading in the service. If you do not need to perform concurrent IPC across different applications, implement binders to create interfaces; Or, if you want to perform IPC but don’t need to deal with multiple threads, use Messenger to implement the interface.
In any case, before implementing AIDL, it is important that you understand binding services (which are implementations of the Service class that other applications can bind to and interact with).
AAPT2: Parses resources, indexes them, and compiles them into binary formats optimized for the Android platform.
Javac: Generate.class files from all.java files (including R files and.java files generated by AIDL) using javac tools.
The dx:.class file, along with the.class file in the dependent tripartite library, is optimized by the dx tool into **.dex files.
Javac: Generate.class files from all.java files (including R files and.java files generated by AIDL) using javac tools.
Now that the Apk is packaged, let’s look at how the PackageManagerService installs it into the device.