Very classic picture, easy to understand.

1. aapt

Use AAPT to package res resource files to generate R.Java, resources.arsc, and RES files. The Android Gradle plugin 3.0.0 and later enables AAPT2 by default. The directory is in the SDK /platform-tools/ AAPT2 directory.

Details can see aapt tool use: developer. The android. Google. Cn/studio/comm…

R.java

All resource items, with their ids, will be in the R.Java file. Each resource class in R. Java corresponds to an inner class.

Res file

There are nine directories for res files, many of which have corresponding files due to adaptation of version, language, screen, etc. Such as:

  • Version layout – v22
  • Size layout – sw600dp
  • Values – useful language

The directory is as follows:

  • animator

    Such resources are stored as XML files in the res/ Animator directory and are used to describe attribute animations.

  • anim

    Such resources are stored as XML files in the res/anim directory and are used to describe tween animations.

  • color

    These resources are stored as XML files in the RES /color directory and are selected by describing the object’s color state.

  • drawable

    These resources are stored as XML or Bitmap files in the res/drawable directory and are used to describe drawable objects. For example, we can place images (.png,.9.png,.jpg,.gif) inside as background images for the application interface view.

  • layout

    These resources are stored as XML files in the RES/Layout directory and are used to describe the application interface layout.

  • menu

    Such resources are stored as XML files under the RES /menu directory and are used to describe application menus.

  • raw

    These assets are stored as files in the RES/RAW directory in any format. They are packaged in an APK file just like assets, but they are given resource ids so that they can be accessed from within the program using their IDS.

    Resources res = getResources();  
    InputStream is = res.openRawResource(R.raw.xxx)
    Copy the code
  • values

    These resources are stored as XML files in the RES /values directory and are used to describe simple values such as arrays, colors, sizes, strings, and styles. In general, These six different values are stored in files named arrays.xml, colors.xml, Dimens. XML, strings. XML, and styles.xml, respectively. -xml

    These resources are stored as XML files in the RES/XML directory and are generally used to describe the configuration information of the application.

resources.arsc

The resource index table records the mapping between resource files and resource ids.

The res directory is specially used to store resource files. When the resource files need to be called in the code, just call findviewbyId() to get the resource files. Whenever a file is placed in the RES folder, AAPT will automatically generate the corresponding ID and save it in the. R file, we can call this ID, but this ID is not enough, the.r file is only to ensure that the compiler does not error, in fact, when the program is running, the system needs to find the corresponding resource path according to the ID, and resources. Arsc file is used to record the relationship between these ids and the location of the resource file file file.

2. Aidl generates Java files

AIDL is short for Android Interface Definition Language and is a way for Android to communicate across processes.

Retrieve all aiDL files in the project and convert them to the corresponding Java files.

3. JavaCompiler stage

All.java files (including R files and.java files generated by AIDL) are generated using javac tools to generate class files.

4. Dex phase

The.class files that will be generated and the.class files of the third party libraries will be generated using the dx tool to generate the classes.dex files (multiple, if subcontracted).

The current gradle multi-dex compiler generates classes2.dex… ClassesN. Dex.

5. ApkBuilder stage

Resource files, dex files and third-party non-Java resource packages (so) in the AAPT stage, and unsigned APK packages are generated by the ApkBuilder tool.

Uncompiled Resources(e.g. Res /raw, images, etc.), Other Resources(assets files), compiled Resources,.dex files, resources.arsc, and Androidmanifest.xml Will be packaged by the ApkBuilder tool into the final.APk file.

Note that:

  • Similarities between Res/Raw and Assets:

    • Files in both directories are stored intact in the APK package after packaging and will not be compiled into binary.
  • Differences between RES/Raw and Assets:

    • Files in res/raw are mapped to the r.java file, which is accessed using the resource ID r.i.d.filename. Files in assets will not be mapped to R.Java and will be accessed using the AssetManager class.
    • Res /raw cannot have a customized directory structure, while assets can have a directory structure. That is, folders can be created under assets.

6. JarSigner phase

Signature, using the Jarsigner tool, default signature for Debug mode, developer signature for Release mode.

7. ZipAligin phase

If you publish an official APK, you must align the APK using zipalign, which is also located in the Android-SDK /tools directory.

Use zipalign to “align” uncompressed resources (images, videos) in APK to align resources with 4-byte boundaries, making resource access faster.

At the end

Android APK file (1) Compilation and packaging process

Android APK file (2) decompression and decompilation

Android APK file (III. AAPT2 tool use)

Android APK file (4)