All the contents of this article are based on the practice of net wenjia, no official bibliography, so if there is any mistake, welcome to point out.

Compile the overall process of packaging

aapt

(Android Asset Packaging Tool) Compiles and packages resources

  1. Compile resource files into binaries

Method of use

  1. F:\AndroidStudio\ SDK \build-tools\30.0.2 Add to the path environment variable
  2. Download OpenHub’s APK,

Example Modify apK files

  1. (aapt l) Execute aapt list -v -a base.apk to print all details about apK

    1. -a: Displays all directories in detail
    2. -v: outputs directories in the form of table.
    3. The headings of table are: Length, Method (compressed form, Deflate (a lossless data compression algorithm using both LZ77 algorithm and Huffman Coding) Stored () directly, The compression rate is 0%), Size, Ratio, Date, Time, CRC-32, Name.

  1. AndroidManifest. XML aapt r base. Apk AndroidManifest

  1. Aapt a <*.apk> < file path to add >, this is to add the file to the packaged APK file

The. Class is in the form of bytecode, and can be directly decomcompiled by Intellj to open the content and find that certain optimization has been made compared with the. Java file

Used to generate APK

  1. Compilation: AAPT can be used to compile XML files and image files

    aapt2 compile -o extrahub/app/src/main/res/values/strings.xml
    Copy the code
  2. Link: Merge the files generated during the compile phase and then package them into an APK file. In addition, other auxiliary files such as R.Java and ProGuard can be generated at this stage. However, the APK file generated by the link is not complete.

    aapt2 link -o output.apk -I /Users/nxiangbo/Library/Android/sdk/platforms/android- 28/android.jar myBuild/values_strings.arsc.flat myBuild/mipmap-xhdpi_ic_launcher.png.flat --manifest app/src/main/AndroidManifest.xml -v
    Copy the code
  3. Dump: prints resources and configuration files of APK files generated by using the link command

    aapt2 dump filename.apk [options]
    Copy the code

Apk file analysisOpen the files are all Martian, has been encountered before this situation, this deep baidu, found that different garbled code actually has a rule to follow. Androidmanifest.xml has been compiled into binary files. Ps: The following is a possible fix for normal files: (Not relevant to this article)You can see that the asset resource has not been changed! (The upload – images. Jianshu. IO/upload_imag…)

! (upload – images. Jianshu. IO/upload_imag…). The XML files in RES are also compiled to binary and have their names changed (obfuscated), but the PNG content is all open. ! (upload – images. Jianshu. IO/upload_imag…).

XML parsing methods

SAX parsing

  • The XML document is scanned line by line, parsed as it is scanned.
  • The event handler is notified when the document begins and ends, the element begins and ends, the document ends, etc. The event handler does the corresponding action, and then continues the same scan until the document ends.
  • If SAX parsing wastes processor resources when you only need a small piece of data at the front or in the middle of the document, PULL parsing can be used instead
  • Event-driven XML parsing calls back an event when the start and end tags of the document are read,

An event is also called back when other nodes and content are read

Pull Parsing mode

  • PULL parsing inherits the advantages of SAX parsing speed and less memory.
  • At the same time, it also maintains the characteristics of simple interface and easy programming.
  • The PULL parser works by allowing application code to actively fetch events from the parser,
  • Because you are actively fetching events, you can stop fetching events after the required conditions are met, ending the parsing.

Dom parsing

  • DOM parsing is a standard provided by the W3C organization,
  • Load everything into memory at once, and build a tree structure that resides in memory, and then move between nodes

To parse the XML.

Q&A

  1. How do XML files package into projects?
  • Through compile and link commands of AAPT, the XML files in the project are compiled and linked to the project.
  • The specific content of the XML file is stored in the directory of the original project in binary form.
  • In addition, when aAPT is compiled, it will establish the corresponding ID for resources, and store the XML file IDS of layout resources, control resources, String resources, Drawable resources in R.Java for indexing. R.java will be class files with the source code of the project. And then it’s a dot dex file with dx. In addition to the index function of r. Java, another function is that if a resource in the RES directory is not used in the application, the system will not compile the corresponding resource into the APK package of the application when the application is compiled, which can save the resources of the Android phone.

  1. How do I refer to the resources I need through the R.Java file

2.1 Using the Java syntax to reference the resource in a Java class: r.resource_type. Resource_name In addition, Android itself has many resource files can be used, the method is to add Android header. Android.r.esource_type. Resouce_name 2.2 In the res directory, use the “@drawable/icon” method, where “@” stands for r.Java class, Drawable represents the static inner class “drawable” in R.Java, and /icon represents the static property “icon”, which can point to the icon.png resource in the res directory “drawable-*dpi”. Similarly, you can also use a file from the Android system. The method is “@android:color/red”.

  1. When the configuration file is packaged, will it be put into app? Under which file corresponding to APK?

4. How does Flutter deploy hot, and how can its code be applied quickly To your app without going through a packaging process