APK file structure and installation process

Source: Internet
Author: User

APK file Structure

Android apps are written in Java, compiled with the Android SDK, and packaged all the data and resource files into a apk (Android Package) file, This is a compressed file with a suffix named. apk, and the APK file contains all the content of an Android application and is the file that the Android platform uses to install the application. apk is a zip compression package that unlocks this APK package and we can see the following structure:


Figure 1 APK file structure

1 Assets Directory: for storing static files that need to be packaged into APK, and res differs in that the assets directory supports subdirectories of any depth, and users can deploy any folder schema to their own needs, and the files in the Res directory will be available. R file to generate the corresponding resource id,assets will not automatically generate the corresponding ID, access to the Assetmanager class.

2 lib directory: here to store application-dependent native library files, is generally used to write, the Lib library here may contain 4 different types, according to the different CPU models, can be divided into arm,arm-v7a,mips,x86, respectively, corresponding to the ARM architecture, ARM-V7 architecture, MIPS architecture and X86 architecture, these so libraries in the APK package in the form of the following figure Figure2:


Figure 2 LIB directory structure

Among them, different CPU architecture corresponds to different directories, each directory can put a lot of the corresponding version of so library, and the structure of the directory is fixed, users can only according to this directory to store their own so library. At present, the mobile terminals used in the market are mostly based on arm or arm-v7a architecture, X86 and MIPS architecture of mobile intelligent terminals are relatively few, so some applications Lib directory contains only Armeabi directory or armeabi-v7a directory, that is to say, These four directories to be based on the CPU structure to choose, and the market on the arm structure of the majority of mobile phones, so the general apk only contains arm and arm-v7a so.

3 Res Directory: RES is the abbreviation for resource, this directory holds resource files, All files that exist in this folder will be mapped to the Android project's. r file, generating the corresponding ID, the direct use of the resource ID when accessing the R.id.filename,res folder, where the Anim holds the animated file, and the drawable directory holds the image resource; Layo UT directory to store layout files, values directory to store some eigenvalues, The colors.xml holds the color color value, Dimens.xml defines the dimension value, String.xml defines the value of the string, Styles.xml defines the style object, the XML folder holds any XML file, and can be passed at run time by Resources.getxml () read; Raw is any file that can be copied directly to the device without compiling them.

4 Meta-inf Directory: Save the application signature information, the signature information can verify the integrity of the APK file. ANDROIDSDK calculates the integrity of all the files in the APK package when packaging apk, and saves the integrity to the Meta-inf folder, where the application first verifies Meta-inf integrity based on the APK folder. This ensures that every file in the APK can not be tampered with. This ensures that the APK application is not maliciously modified or infected, helping to ensure the integrity of the Android application and the security of the system. The files contained in the Meta-inf directory are cert.rsa,cert.dsa,cert.sf and MANIFEST.MF, where Cert.rsa is a signature file signed by the developer using the private key to APK, CERT.SF,MANIFEST.MF records files in the file SHA-1 ha A Greek value.

5) Androidmanifest.xml: is the Android application profile, a setup file that describes the Android application "overall information," in simple terms, equivalent to Android apps "Self-introduction" to the Android system, The Android system can fully understand the information of the APK application based on this "self-introduction", and each Android application must contain a androidmanifest.xml file, and its name is fixed and cannot be modified. When we are developing Android apps, we typically put every single activity,service in the Code, Provider and receiver are registered in Androidmanifest.xml, only so that the system can start the corresponding components, and this file also contains some permission statements and the SDK version information used, and so on. When the program is packaged, the Androidmanifest.xml is simply compiled for easy identification by the Android system, and the format after compilation is Axml format, as shown in the following figure Figure3:


Figure 3 axml format

Axml header: Where the Axml header is a fixed identity axml file, its value is fixed 0x00080003.

Axml file Length: Identifies the size of the Axml file.

All string types in the Stringdatasegment:xml file are saved here.

The resource file ID declared in the resourceidsegment:xml file is stored here.

Xmlcontentsegment: Is the content segment of XML, according to the structure in the XML file in order to save the XML data content.

6) Classes.dex:

Traditional Java programs, first compiled Java files into a class file, bytecode are stored in the class file, the Java Virtual machine can be interpreted to execute these class files. And the Dalvik virtual machine is optimized in the Java virtual machine, executes the Dalvik byte code, and these Dalvik bytecode is converted from Java bytecode, in general, Android applications convert Java bytecode to Dalvik bytecode by using the DX tool in ANDROIDSDK during packaging. The DX tool can merge, reorganize and optimize multiple class files, which can reduce the volume and shorten the running time. The conversion process for the DX tool is as shown in the figure:


Figure 4 DX Tool converts. class file to Dex file

As shown in Figure figure 4,dx the contents of each area of each. class file are weighed, reorganized, and optimized to generate Dex files, the generated Dex file can be executed in Dalvik virtual machine, and faster.

7) RESOURCES.ARSC: Used to record the mapping between resource files and resource IDs to find resources based on resource IDs. Android Development is a modular, res directory is dedicated to the storage of resource files, when the need to invoke resource files in code, only need to call Findviewbyid () to get resource files, whenever a file in the Res folder, AAPT will automatically generate the corresponding ID saved in the. r file, we can call this ID, but only this ID is not enough,. R file is only to ensure that the compiler does not complain, in fact, when the program is running, the system will be based on ID to find the corresponding resource path, and RESOURCES.ARSC file is used to record these IDs and resources File location of the corresponding relationship.

APK installation Process

The application installation of adroid involves several directories as follows:

/data/app: Store the user-installed APK directory, when installed, copy apk to this.

/data/data: After the application is installed, a folder, like the APK package name (PackageName), is automatically generated in the/data/data directory for storing application data.

/data/dalvik-cache: Storage of apk Odex files, easy to use when the application starts directly.

The specific installation process is as follows:

First of all, copy APK installation package to/data/app, and then verify that the APK signature is correct, check the APK structure is normal, and then extract and verify the APK in the Dex file, to determine the Dex file has not been damaged, then the Dex optimized into Odex, so that the application start time to accelerate, At the same time in the/data/data directory is based on the same APK package name folder, if the APK has Lib library, the system will judge the name of the so library, to see if the beginning of the LIB, whether to the end of the. So, and then according to the CPU structure to extract the corresponding so library to/data/data/ Packagename/lib under.

APK installation will be the Dex file decompression and optimize the bit odex, the Odex format is shown in the Figure 5 Image:


Figure 5 Odex file format

Odex added some data to the original Dex file header, adding dependent libraries and secondary data needed to run the program at the end of the file, making the program run faster.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.