Android Packaging process

Source: Internet
Author: User

First, the Android program is roughly divided into resources and code

Resources are assets and res,assets can put files, in the process of packaging into AKP, the files in this directory will be sealed into the package intact. Assetmanager am= getassets (); InputStream is = Assset.open ("filename");

There are many kinds of resources under the Res directory:

drawable: This type of resource is stored in an XML or bitmap file in the Res/drawable directory, which is used to describe the object that can be drawn. For example, we can place some pictures (. png,. 9.png,. jpg,. gif) in it, as a background to the program interface view. Note that the bitmap files stored in this directory may be optimized during the packaging process. For example, a true-color PNG file that does not require more than 256 colors may be converted to a PNG panel with a 8-bit palette, which compresses the picture without compromising the memory resources used by the image.

Animator: This type of resource is stored in an XML file in the Res/animator directory, which is used to describe property animations. Property animations animate objects by altering their properties, for example, by constantly modifying the object's coordinate values to animate object movement, and by continually modifying the object's alpha channel values to achieve the object's gradient.

anim: This type of resource is stored in an XML file in the Res/anim directory to describe the motion tween. Tween animation and property animation, it is not by modifying the properties of the object to achieve, but in the original shape or position of the object to achieve a transformation, for example, the object to apply a rotation transformation, you can get a rotation animation, as well as a zoom transform on the object, you can get a zoom animation. Mathematically speaking, it is to apply a transformation matrix on the basis of the original shape or position of the object to achieve the animation effect. Note that during the execution of an animation, the properties of an object are always constant, and what we see is just a deformed copy of it.

color: This type of resource is stored in an XML file in the Res/color directory, using the Description object color state to select the child. For example, we can define a selector that specifies that an object displays different colors in different states. The state of the object can be divided into 7 pressed, focused, selected, Checkable, checked, enabled, and window_focused.

layout: This type of resource is stored in an XML file in the Res/layout directory to describe the layout of the application interface.

Menu: This type of resource is stored in an XML file in the Res/menu directory and is used to describe the application menu, such as Options menu, Context menu, and sub menu.

Raw: These resources are stored in the Res/raw directory in any format, and they are packaged in the APK file in the same way as the assets class resources, but they are given a resource ID so that we can access them through the ID in the program. For example, suppose you have a file named filename in the Res/raw directory, and it is compiled with a resource ID of r.raw.filename, then you can access it using the following code:Resources res =  Getresources (); InputStream is = Res. Openrawresource (r.raw.filename);

values: These resources are stored in an XML file in the Res/values directory to describe some simple values, such as arrays, colors, dimensions, strings, and style values, in general, these six different values are saved in the name Arrays.xml, Colors.xml, Dimens.xml, Strings.xml, and Styles.xml files.

XML: This type of resource is stored in an XML file in the Res/xml directory, which is typically used to describe the configuration information of the application.

These resources are packaged by AAPT (Android Asset package tool) packaging tools.

Res/animator, Res/anim, Res/color, res/drawable (non-bitmap files, non. png,. 9.png,. jpg,. gif files), res/layout, Res/menu, res/ Both values and Res/xml's resource files are compiled into binary-formatted XML files from text-formatted XML files, and assets and Res/raw are packaged in intact. In addition to assets resources, other resources are assigned a resource ID.

The packaging tool compiles and packages resources, and after compilation, it generates a RESOURCES.ARSC file and a R.java that holds a resource Index table, which defines the individual resource ID constants. The application configuration file Androidmanifest.xml is also compiled into a binary XML file and then packaged into the APK. The application accesses the resource through Assetmanager at run time, either through a resource ID or through a file name.

Code: It is Java->class->dex

Summarize the packaging process

1. Package the resource file and generate the R.java file.

Packaging Resource Tool AAPT is located in the Andorid-sdk\platform-tools directory, the source of the tool in the Android system source code frameworks\base\tools\aapt directory, The process of generating is primarily called the buildresources () function in the Resource.cpp file in the AAPT source directory, The function first checks the legitimacy of the androidmanifest.xml, and then to the RES directory under the Resource subdirectory processing, the function of processing is makefileresources (), the content of the processing includes the legitimacy of the resource file name, add entries to the Resource table table, etc. After processing, call the Complieresourceflie () function to compile the resources in the RES and asserts directories and generate the RESOURCES.ARSC file, Compileresourcefile () The function is located in the ResourceTable.cpp file of the AAPT source directory, and the sweet potato will eventually call the Parseandaddentry () function to generate the R.java file, complete the resource compilation, and then call Compilexmlfile () The function compiles the XML files in the subdirectory of the Res directory, so that the processed XML file is simply "encrypted", and finally all the resources are compiled with the generated RESOURCES.ARSC file and "encrypted" Androidmanifest.xml files are packaged and compressed into RESOURCES.AP_ files (using the Ant Tool command-line compilation generates AP_ files with the same name as the property specified in Project name in Buiild.xml).

2. Process the Aidl file and generate the appropriate Java file.

This step can be skipped for Android projects that are not using AIDL. The tool used in this step is Aidl, located in the Android-sdk\platform-tools directory, the Aidl tool parses the interface definition file (AIDL is an acronym for Android Interface Definition language, That is, the Android Interface Description language) and generate the appropriate Java code for the program to call, the source code is located in the Android source code frameworks\base\tools\aidl directory.

3. Compile the project source code and generate the corresponding class file.

This step calls Javac to compile all Java source files in the project SRC directory, the generated class file is located in the project Bin\classes directory, if the program is compiled based on ANDROIDSDK development, the actual development process, may also use Android NDK to compile the native code, so, if possible, this step also requires the Android NDK to compile the C + + code, and of course, the steps to compile C + + code can be advanced to the first or second step.

4. Confuse the. class file according to the Proguard file

The release package requires that all dependent jar packages are processed together, and the single jar package after the trip is confused

5. Convert all the class files to generate the Class.dex file.

Android system Dalvik virtual machine executable file for the DEX format, the program to run the required class.dex is generated in this step, the use of the tool is DX, it is located in the Android-sdk\platform-tools directory, The main task of the DX tool is to convert Java bytecode to Dalvik bytecode, to compress constant pools, to eliminate redundant information, and so on.

6. Package build apk file.

The packaged tool is Apkbuilder, which is located in the Android-sdk\tools directory, apkbuilder as a script file, which is actually called android-sdk\tools\lib\ The Com.android.sdklib.build.apkbuilderMain class in the Sdklib.jar file. Its implementation code is located in the Android system source sdk\sdkmanager\libs\sdklib\src\com\android\sdklib\build\ Akpbuildermain.java file, the code constructs a Apkbuilder class, and then builds the APK file based on the file containing Resources.arec, which is usually the ap_ end of the file, and then calls Addsourcefolder ( ) function to add resources, Addsourcefolder () calls the Processfileforresource () function to add resources to the APK file, including the Res directory and the files in the assets directory. After adding the full source call the Addresourcesfromjar () function to write the dependent library into the apk file, then call Addnativelibraries () The function adds the native library under the Project Libs directory (by ANDROIDDNK compiling the resulting so or bin file), and the last bar closes the apk file with sealapk ().

7. Sign the APK file.

Two kinds of signature situations: one is to sign when the program is debugged, and when you use Eclipse development to compile the debugger, you use Debug.keystore to sign it yourself.

The other is to sign the program when the package is released: 1, signed with the Jarsigner tool provided in the JDK

2, using the SIGNAPK tool provided in the Android source code, it is located under the build\tools\signapk directory of the Android system source.

8. Align the signed apk file with the alignment.

The use of the tool for Zipalign, which is located in the Android-sdk\tools directory, the source code is located in the Android system source code build\tools\zipalign, its main job is to the APK package to align the processing, Make all resource files in the APK package offset from the file starting at 4-byte integer times, so that when accessing the APK file through a memory map, it will be faster to verify that the APK file is aligned to work by the Verify () function of the ZipAlign.cpp file. Work with alignment is done by the process () function.

Android Packaging process

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.