I. Analyze the apK that has been made
To understand the packaging process of APK, let’s first take a look at what is contained in the APK package once it is packaged. The.apk file is actually a compressed file, change the suffix of the file to. Zip can be used to decompress the software:
1. Change the apK suffix to RAR package
2. Decompress the RAR package
Apk is a compressed package containing lib, meta-INF, classes.dex, res, resources.arsc folders and files. Let’s look at what each of them does.
- Assets resources.
- Lib is not available in every APK, mainly depending on the project, folder is stored in the so dynamic link library, so dynamic link library does not need to do processing.
- Meta-inf is a signature folder that contains three files.
- Res: in addition to the pictures and the res/raw file folder, the rest of the XML files have been aapt compiled into the binary XML files, it also points animator, anim, color, drawable, layout, menu and raw folder.
- Androidmanifest.xml: aAPT compiled binary XML file at the root of the project that describes the components exposed in the package (activities, services, etc.), their respective implementation classes, All kinds of data that can be processed and startup locations. In addition to declaring Activities, ContentProviders, Services, and Intent Receivers in an application, permissions and Instrumentation can be specified. This file is very important, it contains our Four Android components and application permissions.
- Classes.dex is the.dex file, which is the Java code that we wrote and processed. If you unpack it, you have classes1.dex, classes2.dex… Multiple classes.dex files.
- Resources.arsc records information about all application resource directories, including the name, type, value, ID, and configured dimension information for each resource. We can think of the resources.arsc file as a resource index table that can quickly find the best match in an application’s resource catalog given a resource ID and device configuration information.
We find that XML files cannot be recognized directly because: XML files are compiled into binary XML files by AAPT. The reasons for converting text XML to binary XML are as follows: binary XML files take up less space; XML files in binary format are parsed faster.
2. Detailed process of packaging
1. Compile the packaging flowchart
By default, Android Studio uses Gradle organization to complete the packaging process, which is easy for developers to execute related tasks. This transparent packaging process also makes us ignore many details.Packaging tool details.
names | Function is introduced | The path |
---|---|---|
aapt | The Android resource packaging tool packages the RES resource files to generate r.Java, resources.arsc and RES files (binary & non-binary such as RES/RAW and PIC remain unchanged) | {SDK_PATH}/platform-tools/{tools_version}/appt.exe |
aidl | Android API tool for converting. Java files | {SDK_PATH}/platform-tools/{tools_version}/aidl.exe |
javac | Java Compiler Java code is converted to class files | {JDK_HOME}/javac |
dex | Convert the. Class file to a. Dex file recognized by the Davik VM | {JDK_HOME}/platform-tools/{tools_version}/dx.bat |
apkbuilder | All uncompiled resources such as images, assets, compiled resources and.dex files are packaged into the final.apk file by the ApkBuilder tool | {JDK_HOME}/tools/opkbuilder |
jarsigner | Jar file signing tool. Once the APK file is generated, it must be signed before it can be installed on the device. Debug. keystore for debugging and release.keystore for releasing the official version | {JDK_HOME}/jarsigner |
zipalign | Bytecode alignment tool. The main process of alignment is to offset all resource files in the APK package to an integer multiple of 4 bytes from the start of the file. This will make access to APK files faster through memory mapping. The purpose of alignment is to reduce runtime memory usage. | {JDK_HOME}/tools/zipalign |
### 2. Compile the packaging step | ||
##### (1.) Package resource files and generate r.java files | ||
Aapt (The Android Asset Packaing Tool), SDK \build-tools\25.0.0\aapt. | ||
In this process, both the androidmanifest.xml file and the layout file XML in the project are compiled to generate the corresponding R.java, and the Androidmanifest.xml is compiled into binary by AAPT. | ||
Resources stored in the RES directory of the APP. Most of these resources are compiled into binary files before the APP is packaged, and each of these files is given a Resource ID. The application layer code accesses this type of resource through the Resource ID. | ||
During the compilation process of Android applications, aAPT will compile resource files and generate a resource. Arsc file, which is equivalent to a file index table, records a lot of information related to resources. | ||
##### (2.) Process aiDL files and generate corresponding Java files | ||
The tool for processing AIDL files is Android Interface Definition Language (AIDL). The directory is SDK \build-tools\25.0.0\ aiDL. | ||
The AIDL tool parses the interface definition file and generates the corresponding Java code interface for the program to invoke. You can skip this step if you are not using aiDL files in your project. | ||
##### (3.) Compile the project source code and generate the class file | ||
Compile source code using the Java compiler (JAVAC) | ||
All Java code in the project, including r.java and.aidl files, will be converted into.class files by the Java compiler (Javac), and the resulting class files will be located in the bin/classes directory of the project. | ||
##### (4.) Convert all the class files to generate the classes.dex file | ||
This process uses the dx (dex) tool to generate a classes.dex file that can be executed by the Android Dalvik VM. The tool directory is SDK \build-tools\25.0.0\dx. | ||
Any third-party libraries and. Class files will be converted to. Dex files. The main work of dx tool is to convert Java bytecode to Dalvik bytecode, compress constant pool, eliminate redundant information, etc. | ||
##### (5.) Package and generate APK files | ||
Package tools apkBuilder, directory Android-SDK /tools, ApkBuilder for a script file, The actual call is (SDK \ tools \ lib) files in the com. Android. Sdklib. Build. ApkbuilderMain class. | ||
All resources that are not compiled, such as resources in images and assets (such files are original files that are not compiled during APP packaging, but are packaged directly into APP. For access to these resource files, the application layer code needs to access them through the file name); | ||
The compiled resources and.dex files are packaged into the final.apk file by the ApkBuilder tool. | ||
##### (6.) Sign the APK file | ||
Once an APK file is generated, it must be signed before it can be installed on the device. | ||
In the development process, the main use is two types of signed keystore. Debug. keystore is used for debugging. The debug.keystore is used for debugging when you run directly in Eclipse or Android Studio. | ||
The other is relese.keystore, which is used to publish the official version and needs to be configured by the developer. | ||
##### (7.) Align the signed APK file | ||
If the apK you publish is official, you must align the APK using zipalign, SDK \build-tools\25.0.0\zipalign. | ||
The main process of alignment is to offset all resource files in the APK package to integer multiples of 4 bytes from the start of the file so that APK files can be accessed faster through memory mapping. The purpose of alignment is to reduce runtime memory usage. |
In summary, the Build-Tools directory in the Android SDK provides a variety of programs that are independently runnable, and you can think of the Android Studio build packaging process as a wrapper around these tools.
Android SDK directory structure
APK packages scripts under the SDK folder:
1. Build – the tools directory
Build tools directory, which contains the build tools for davlik virtual machine, such as ADB, aapt, aidl, dx, etc.
2. The emulator directory
Android Emulator emulator directory.
3. The extras directory
This file stores the USB driver provided by Google and the hardware acceleration accessory kit provided by Intel.
4. Platforms directory
Is the real file of each platform SDK, which houses different versions of Android system. Corresponding to compileSdkVersion in Android Studio build.gradle
5. Platform – the tools directory
Common tools for each version. Examples include ADB, SQlite3, Fastboot, etc.
6. The sources directory
Contains each version of the SDK source code.
7.system-images
It stores the image files used to create Android virtual machines (the compiled image files can be directly loaded by the emulator).
8.tools
Tools: This folder contains a large number of Android development and debugging tools. This includes testing, debugging, and third-party tools. Simulators, data management tools, etc.
4. Android Studio engineering structure
1. Gradle directory.
Gradle runtime automatically generated directory (files generated by the automatic compilation tool), version specified by wrapper. The general situation is not modified and does not need to be included in the project source code management.
2.. The idea of directory.
Intellij IDEA runtime generated file directory, generally not modified, do not need to be included in the project source code management.
3. The app (module) directory.
- App/Build: Output file compiled by the app module
- App /libs: Library files to place references to
- App/SRC: The main file directory for your app
- App/SRC /androidTest: unit test directory
- App/SRC /main: The main project directory and code
- App/SRC /main/assets: Place native files, which retain their original format and can be accessed through streams
- App/SRC /main/ Java: source code for the project
- App/SRC /main/res: Resources for the project
- App/SRC /main/res/anim: XML file to store animation
- App/SRC/main/res/drawable: for all kinds of bitmap file (… The PNG, JPG, PNG,. GIF, etc.) and drawable types of XML file
- App/SRC/main/the res/drawable – v24: store custom Drawables class (24, Android API can be used in XML)
- App/SRC /main/res/layout: Stores layout files
- App/SRC /main/res/menu: stores menu files
- App/SRC /main/res/mipmap-hdpi: Stores high-resolution image resources
- App/SRC /main/res/ mipmap-MDpi: Stores medium resolution image resources
- App/SRC /main/res/mipmap-xdpi: Store ultra-high resolution image resources
- App/SRC /main/res/mipmap-xxdpi: Stores ultra-high-resolution image resources
- App/SRC /main/res/mipmap-xxxdpi: Store super, super and super high resolution image resources
- App/SRC /main/res/raw: Store all kinds of native resources (audio, video, some XML files, etc.)
- App/SRC /main/res/values: Store various configuration resources (color, size, style, string, etc.)
- App/SRC/main/res/values/attrs. XML: custom controls in the more, the attribute of custom controls
- App/SRC/main/res/values/arrays. The XML: define an array of resources
- App/SRC/main/res/values/colors. The XML: define the color of resources
- App/SRC/main/res/values/dimens XML: define size of resources
- App/SRC/main/res/values/string. The XML: define a string resource
- App/SRC/main/res/values/styles. The XML: define the style of resources
- App/SRC /main/res/values-v11: called on devices with API 11+
- App/SRC /main/res/values-v14: called on devices with API 14+
- App/SRC /main/res/values-v21: called on devices with API 21+
- App/SRC/main/res/AndroidManifest. The list of XML: the project file (name, version, the SDK, permissions and other configuration information)
- App/SRC /.gitignore: ignored file or directory
- App /app.iml: configuration file of the APP module
- App /build.gradle: Gradle compilation file for app modules
- App/Proguard-rules.pro: App module code obfuscates configuration files
4. Gradle directory and configuration items.
- gradle/wrapper/gradle-wrapper.jar
- Gradle /wrapper/gradle-wrapper.properties specifies what version of Gradle is needed for the project, where to download gradle from, and where to put gradle-wrapper.properties
- .gitignore: files or directories to be ignored
- Build. gradle: Gradle compilation file for your project
- Gradle. properties: Gradle-related global properties
- Gradlew: Compiles scripts that can be packaged on the command line
- Gradlew. Bat: Executable file gradle Wrapper for Windows
- Local. properties: specifies the path where the SDK/NDK is stored
- Myapplication. iml: Saves information about this module
- Settings. gradle: Sets the associated Gradle scripts
5. APk installation process
1. An overview of the
As we all know, Android apps end up packaged in. Apk format (essentially a compressed package), installed on the phone and running. APK stands for Android Package. The application management service PackageManagerService installs an application by parsing the application configuration file Androidmanifest.xml and getting information about the application from it. For example, you can obtain information about the application components, such as activities, services, Broadcast Receivers, and Content Providers. Once you have these information, you can use the ActivityManagerService, We can use these applications normally in the system.
2. Four ways to install Android app APK
- System application installation: APK and applications of the system are loaded when the system is started, and there is no installation interface;
- Network download application installation: complete through various market applications, no installation interface;
- ADB tool installation: that is, through the Android SDK development tools inside the adB. exe program installation, no installation interface;
- Third-party application installation: The third-party application is installed using the APK file on the SD card (for example, by double-clicking the APK file). The installation page is displayed. By default, the system has installed a program for installing and uninstalling applications, that is, the packageInstaller.
3. Directories involved in application installation
- /system/app: built-in application of the system. You can delete it only after obtaining adb root permission
- /data/app: user program installation directory. Copy the APK files to this directory when you install
- /data/data: stores application data
- / data/dalvik cache: Install the dex file in apK to the dalvik-cache directory (the dex file is the executable file of dalvik VM, of course, ART – Android Runtime executable file format is OAT, when ART is enabled, the system will perform the dex file conversion to OAT file)
- / data/system: The packs.xml file in this directory, which is similar to the Windows registry, is created by writeLP() when apK is parsed, and contains the permissions of the system, And the name of each apk, codePath, flags, ts, version, uesrid information, such as the information mainly through the apk AndroidManifest. The XML parsing, after parsing the apk will update information written to the file and save to flash, Read relevant information directly from it and add it to the list of memory related information after the next startup. This file will be updated when apK is upgraded, installed, or deleted.
- / data/system/packages. In the XML content, rounding, here are the label content is not necessarily complete, just list the core content of packages. The complete definition of XML can be found in the official document) :
4. Installation process
Copy the APK file to the specified directory
On Android, the apK installation file is saved. By default, the apK installed by the user is first copied to the /data/app directory. The /data/app directory is a directory that users have permission to access. When apK is installed, it is automatically selected to store the files installed by users. The apK files delivered by the system are stored in the /system partition, including /system/app and /system/vendor/app. /system/priv-app, etc. This partition can only be accessed by users with Root permission, which is why we cannot delete the system’s factory APP without Root phone.
Decompress apK, copy files, and create a data directory for the application
In order to speed up the startup of APP, APK will first copy the executable file (dex) of app to /data/dalvik-cache directory and cache it during installation. Then, create an application data directory (named after the application package name) under /data/data/ to store application-related data such as databases, XML files, caches, binary SO dynamic libraries, and so on.
Parse the androidManifinest.xml file of APK
Android also has a registry that records basic information about all currently installed apps and updates this file every time the system installs or uninstalls any APK files. This file is located in the following directory: / data/system/packages. XML system in the process of installation apk, parses the apk AndroidManifinest. The XML file, extract the important information into the apk to packages. The XML file, this information includes: Permissions, application package name, APK installation location, version, userID, etc. This explains why some app marketplaces and software management apps are able to keep track of all the apps currently installed on the phone, as well as their details. The other thing is the Linux user Id and user group Id so that he can get the right permissions to run it. All of these are done by PackageServiceManager, and we will focus on PackageServiceManager below.
Display shortcuts
These apps are just registered with the PackageManagerService, and if we want to see these apps on the Android desktop, we need a Home app, Be responsible for pulling the installed applications out of the PackageManagerService and presenting them on the desktop in a friendly way, such as as shortcut ICONS. In The Android system, the Home application is the Launcher, which displays the installed applications in the system on the desktop
Saving installation Information
Save the package name, version number, and installation path of APP in the data/system/ packes. XML file for use in the next installation.