Recently, I have been learning the knowledge of Android development. I have written down my feelings during the learning process, which is not only a summary of my own learning, but also hope to provide some help to future generations. Throughout the series, we will try to compare and contrast knowledge and concepts related to iOS development, including controls, layouts, ideas, etc.

APK compilation packaging process

Process Overview:

  1. Compile the resource file (res folder) with AAPT to generate R.Java (resource ID constant file) and resources.arsc(resource index table).
  • Resource ID is similar to the tag attribute of UIView in iOS, which can uniquely identify a resource. In Android, the concept of resources is not limited to control views. All resources except assets are assigned a resource ID.
  • When writing Android applications in development tools such as Android Studio, the IDE generates r. Java files directly for us, which is essentially generated by compiling resource files through AAPT.
  • Resources. Arsc is a resource index table that maintains the mapping between resource IDS, names, and paths. The id is converted to the name of the resource file via resources.arsc, which is then loaded by AssetManager.
  1. Java files such as r.java and project code SRC are compiled into. Class files by using javac command.
  2. The. Class file of the second step and the. Class file of the third party library are packaged into the dex file through the dx command. Dex explains below.
  3. Apkbuilder packages compiled resources, compiled code, and non-compiled resources (such as assets) into APK files.
  4. The signature.
  5. Alignment processing. Alignment is mainly used to offset resource files in APK packages to a 4 byte integer multiple from the start of the file, so that access to APK files through memory mapping will be faster.

runtime

JVM vs DVM

The dex file mentioned above is a device architecture-independent bytecode. DVM translates part of Dalvik bytecode into machine code in real time while the Android program is running.

DVM, short for Dalvik Virtual Machine(Android Virtual Machine), is based on the concept of JVM(Java Virtual Machine) and is also optimized. The JVM is stack-based and THE DVM is register-based, which is much faster. The dex file stores the public parts of multiple class files in a unified manner and removes redundant information.

Dalvik vs ART

As an iOS developer, when you think of Runtime, you inevitably think of objC runtime. In contrast, objC’s syntax is translated at compile time into C code such as objc_msgSend, which is compiled into a directly-runnable binary, but this binary is platform dependent. The idea of a virtual machine is platform independent, so the compiler generates only intermediate code that can be translated into different code on different platforms. The problem, then, is that Android applications themselves run on Android, a specific platform, and can theoretically be compiled into runnable binary code at compile time, without having to translate it at run time. With this question looked up some data, found that there is indeed a trend towards this direction.

Since Android 4.4, the ART runtime mechanism has been introduced. ART is compiled using AOT(Ahead of time), and DEX bytecode is translated into machine code and stored on the device’s memory during the installation of the application. Since ART executes native code, the code execution speed and resource occupancy are more advantageous.

This article briefly introduces the process of apK compilation and packaging and the introduction of Android VIRTUAL machine, shortcomings welcome comments pointed out. The next article will cover controls and interface layouts.