Android APK Packaging Process Overview _android how to pack apk

Source: Internet
Author: User

I've recently seen Lao Luo's blog on the analysis of Android resource management and APK packaging process, referencing other materials, doing some tidying up, leaving out tedious packaging details and data structures, and outlining the entire process of apk packaging as a whole.

Process Overview:1, packaging resource files, generate R.java file 2, processing Aidl file, generate the corresponding Java file 3, compile the project source code, generate the corresponding class file 4, convert all class files, generate Classes.dex file 5, package generation Apk6, To sign the APK file 7, the signature of the APK file to deal with it
tools used in the packaging process
name function introduction The path in the operating system source path
AAPT (Android Asset package Tool) Android Resource Packaging Tool ${android_sdk_home}/build-tools/android_version/aapt Frameworks\base\tools\aap
Aidl (Android Interface Definition language)

Android Interface Description Language,

Tools to convert Aidl to. java files

${android_sdk_home}/build-tools/android_version/aidl Frameworks\base\tools\aidl
javac java Compiler

${jdk_home}/java

C or/usr/bin/javac


dex Convert. class files to. dex files that are recognized by the Davik VM ${ANDROID_SDK_HOME}/BUILD-TOOLS/ ANDROID_VERSION/DX
Apkbuilder Build APK Package ${android_sdk_home}/tools/apkbuilder sdk\sdkmanager\libs\sdklib\ src\com\android\sdklib\build\ apkbuildermain.java
jarsigner .jar file's signature tool ${jdk_home}/jarsigner or/usr/bin/jarsigner
Zipalign Byte-code alignment tool

${android_sdk_home}/tools

/zipalign



The first step: Package the resource file and generate the R.java file. The "Input" resource file (which is the file in res in the project), the assets file (which is equivalent to another resource, the Android system does not optimize it as it does for files in res), Androidmanifest.xml file (the package name is read from here, because the build R.java file requires a package name), the Android base Class library (Android.jar file) "Tools" AAPT Tools "Output" Packaged resources (RESOURCES.AP_ files in the bin directory), R.java files (gen directory), tool AAPT for packaging resources,most text-formatted XML resource files are compiled into binary-formatted XML resource files .In addition to assets and Res/raw resources are packaged in the original APK, other resources will be compiled or processed. . The build process is primarily called the Buildresource () function in the Resource.cpp file under the AAPT source directory, which first checks the legitimacy of the androidmanifest.xml and then processes the resource subdirectory under the Res directory, with the function makefilere Source (), which includes checking the legality of the resource file name, adding entries to the Resource Table table, and so on. After processing, call the Compileresourcefile () function to compile the resources under the RES and asserts directories and generate the RESOURCES.ARSC file, and the Compileresourcefile () function is located in AAPT file of the ResourceTable.cpp source directory , the function finally calls the Parseandaddentry () function to generate the R.java file, and after compiling the resource, the next call to the Compilexmlfile () function compiles the XML file for the subdirectory of the Res directory, respectively. This processed XML file is simply "encrypted", and finally all the resources are compiled with the generated RESORCES.ARSC file and the "encrypted" Androidmanifest.xml file is compressed into RESOURCES.AP_ File (using the Ant Tool command-line compilation generates a AP_ file with the same name as the property specified in Project name in Build.xml). More detailed procedures for this step can be read http://blog.csdn.net/luoshengyang/article/details/8744683
Step Two: Process the Aidl file and generate the appropriate Java file. "Input" source files, aidl files, framework.aidl files "Tools" Aidl Tools "output" corresponding to the. java files This step can be skipped for Android projects that are not using AIDL. The Aidl tool parses the interface definition file and generates the appropriate Java code for the program to call.
The third step: Compile the project source code, generate the corresponding class file. "Input" source files (including R.java and aidl generated. java files), library files (. jar files) tools Javac Tools Output. class file This step calls the Javac compile all Java source files in the project SRC directory, The generated class file is located in the Bin\classes directory of the project, assuming that the program was developed based on the Android SDK when compiling the project source code, and that the Android NDK may be used to compile the native code in the actual development process, so, if possible, This step also requires the use of the Android NDK to compile C + + code, and of course, the steps to compile C + + code can be advanced to the first or second step.
Fourth Step: Convert all the class files to generate the Classes.dex file. The "input". class file (including the. class file generated by the Aidl, the. class file generated by the source file), the library file (. jar file) tool Javac tool output. dex files are mentioned many times earlier, Android system Dalvik virtual machine executable file for the DEX format, the program to run the required Classes.dex file is generated in this step, the use of tools for the DX,DX tool is the main work is to convert Java bytecode to Dalvik bytecode, compression constant pool, Eliminate redundant information and more.
Fifth Step: Package build apk. "Enter" the packaged resource file, the packaged class file (. dex file), the Libs file (including the. So file, of course, many projects do not have such a file, if you do not use C + + development) "Tool" Apkbuilder tool "Output" Unsigned. apk file Packaging tool for Apkbuilder,apkbuilder as a script file, the actual call is Android-sdk\tools\lib\ The Com.android.sdklib.build.ApkBuilderMain class in the Sdklib.jar file. Its code implementation is located in the Android system source sdk\sdkmanager\libs\sdklib\src\com\android\sdklib\build\ Apkbuildermain.java file, the code constructs a Apkbuilder class, and then builds the APK file based on the file containing RESOURCES.ARSC, which is usually the end of the AP_ and then calls Addsourcefolder () function to add a project resource, Addsourcefolder () calls the Processfileforresource () function to add resources to the APK file, including the Res directory and the files in the asserts directory. After adding the resource, call the Addresourcefromjar () function to write the dependent library into the apk file, and then call the Addnativelibraries () function to add the native library under the Project Libs directory (via Android NDK compiles the generated so or bin file), and finally calls sealapk () to close the apk file.
Sixth step: Sign the APK file. "Enter" unsigned. apk file "tool" Jarsigner "Output" The signed. apk file for Android applications requires a signature to install on an Android device, there are two cases of signing the APK file: One is to sign when the program is being debugged, and when you use eclipse to develop an Android program, you will use a debug.keystore when compiling the debugger The other is to sign the program when the package is released, in which case you need to provide a signature file that conforms to the requirements in the Android development documentation. There are two ways of signing: one is to use the Jarsigner tool signature provided in the JDK, and the other is to use the SIGNAPK tool provided in the Android source code, which is located in the source build\tools\signapk directory of the Android system.
Seventh step: Align the apk file after the signature. "Input" after signing the. apk file "Tools" zipalign Tool "output" aligned with the. apk file This step requires the use of the tool Zipalign, which is located in the Android-sdk\tools directory, the source code is located in the Android system source code build\ Tools\zipalign directory, its main work is to align the SPK package so that all resource files in the SPK package are offset from the file starting at 4-byte integer times, so that the memory-mapped access to the APK file is faster, The work of verifying that the APK file is aligned is done by the Verify () function of the ZipAlign.cpp file, and the process alignment is done by the process () function.
Take the specific document included in a specific project as an example of the following: Details on the Android packaging apk process and how to get resources, find resources, use resources, go to Lao Luo's blog, very detailed.0. Android Resource Management framework brief introduction and Learning Plan 1. the process of compiling and packaging application resources ; 2. initialization process of application resources ; 3. The application resource discovery process.

jason0539

Blog: http://blog.csdn.net/jason0539 (reprint please indicate the source)

Sweep code Follow me public number


Android APK Packaging Process Overview _android how to pack apk

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.