In-depth analysis of the Android build and run process

Source: Internet
Author: User

Android compiles and runs in-depth analysis of fonts: [Increase decrease] Type: Reprint first look at the Android application written in the Java language from the source to the installation package of the entire process, this process to understand the Android compile and run the process of great help

First look at the entire process of using Android applications written in the Java language from the source to the installation package, which includes compilation, linking, and signing:

(1) Generate R.java files using the AAPT tool

You can create an Android project that is not compiled by building a good eclipse development environment, so be sure to remove the check mark in front of the build automatically option in Eclipse in the Project menu before you create the project. After you have created a project that is not compiled, enter the following command on the command line:

D:\android-sdk-windows\platform-tools>aapt package-f-m-m "C:\Documents and Settings\******\workspace\ Helloandroid3\androidmanifest.xml "-j" C:\Documents and Settings\******\workspace\helloandroid3\gen "-S" c \ Documents and Settings\******\workspace\helloandroid3\res "-I" D:\android-sdk-windows\platforms\android-10\ Android.jar "

Where-M and the following parameters are used to specify the path of the Androidmanifest.xml (configuration file),-j and followed by the parameter is the specified R.java generation path,-s and subsequent parameters is the directory of the specified resource file,- I and subsequent parameters are specified to be included in the Android Platform Class Library, and will be generated in the project directory in the Gen directory under the R.java file. The specific usage of AAPT can be seen after you enter AAPT on the command line.

The role of the R.java file is to provide access to the program's resources, and in more detail, see the post on the file structure and details of the Android project.

(2) Compile the. aidl file into a. java file using the Aidl tool

Aidl is an inter-process call that is provided by the Android system, similar to an IPC call, using the Aidl tool to use the Android Interface Definition The. aidl file described by language is compiled into a. java file that contains the Java interface class, and the processes follow these interfaces to call each other.. aidl files are generally stored with the program source files. For projects created automatically in this example, Aidl is not used, so this step is not done. The use of the Aidl tool is as follows:

Usage:aidl OPTIONS INPUT [OUTPUT]
Aidl--preprocess OUTPUT INPUT ...

OPTIONS:
-i<dir> Search path for import statements.
-d<file> generate dependency FILE.
-p<file> FILE created by--preprocess to import.
-o<folder> base output FOLDER for generated files.
-B fail when trying to compile a parcelable.

INPUT:
An Aidl interface file.

OUTPUT:
The generated interface files.
If omitted and the-o option is not used, the input filename was used, with the. aidl extension changed to a. Java Extensio N.
If the-o option is used, the generated files would be a placed in the base output folder, under their package folder

(3) Compile the. java file into a. class file using the Javac tool

D:\java\jdk1.6.0_25\bin>javac-encoding gb18030-target 1.6-bootclasspath "D:\android-sdk-windows\platforms\ Android-10\android.jar "-D" C:\Documents and Settings\******\workspace\helloandroid3\bin "" C:\Documents and Settings\ \workspace\helloandroid3\src\com\******\helloandroid3\helloandroid3.java "" C:\Documents and settings\****** \workspace\helloandroid3\gen\com\******\helloandroid3\r.java "

During, I originally wanted to use *.java to describe the source code files that need to be compiled, but the hint cannot be found, later the source file is specified as a specific Helloandroid3.java file before compiling through, strange.

The. class file is then generated in the Bin directory under the project directory.

(4) Convert many. class files into a. dex file using Dx.bat batch processing

D:\ANDROID-SDK-WINDOWS\PLATFORM-TOOLS>DX--dex--output=c:\docume~1\******\workspace\helloandroid3\bin\ Classes.dexc:\docume~1\******\workspace\helloandroid3\bin\

The path to the. dex file is indicated by the--output and subsequent paths; the path to the red callout is the path to the. class, and it is important to note that the package path cannot be added here, otherwise it will be reported as mismatched errors, and the package path may have been appended in the batch. In addition, if the Windows system path contains spaces, the use of abbreviated form, specifically what system path and its abbreviation is what, or ask Niang bar. After success, the. dex file is generated under the specified path, and the. dex file is run on the Android Dalvik virtual machine, which is described later in this section.

(5) Packaging resource files with the AAPT tool

D:\android-sdk-windows\platform-tools>aapt package-f-M c:\docume~1\******\workspace\helloandroid3\ Androidmanifest.xml-s c:\docume~1\******\workspace\helloandroid3\res-a c:\docume~1\******\workspace\ Helloandroid3\assets-i D:\android-sdk-windows\platforms\android-10\android.jar-F c:\docume~1\******\workspace\ Helloandroid3\bin\resources.ap_

Control the generation of R.java files, you can see the parameters have changed, less than-M and-j, if you look at the description of AAPT usage to know, M and-j are pairs appear to indicate the path of the R.java file generation. -M,-s,-I have been mentioned before, here no longer introduced. The role of-F is to indicate the path of the packaged resource file, and in the end, add the file name, preferably with the extension. Here is a reference to the. Ap_ suffix that was set up automatically at compile time in eclipse.

(6) using Apkbuilder to generate an unsigned apk installation file

D:\android-sdk-windows\tools>apkbuilder c:\docume~1\******\workspace\helloandroid3\bin\helloandroid3.apk-v-U -Z c:\docume~1\******\workspace\helloandroid3\bin\resources.ap_-F c:\docume~1\******\workspace\helloandroid3\bin \CLASSES.DEX-RF C:\DOCUME~1\******\WORKSPACE\HELLOANDROID3\SRC

Where the path immediately following the Apkbuilder is the path of the generated apk installation file, the function of the-v parameter is to indicate the output of the necessary information in the execution, the specific output is as follows:

Packaging helloandroid3.apk
C:\DOCUME~1\******\WORKSPACE\HELLOANDROID3\BIN\RESOURCES.AP_:
= Res/layout/main.xml
= Androidmanifest.xml
= RESOURCES.ARSC
= Res/drawable-hdpi/icon.png
= Res/drawable-ldpi/icon.png
= Res/drawable-mdpi/icon.png
C:\docume~1\******\workspace\helloandroid3\bin\classes.dex = Classes.dex

The-u parameter indicates that an unsigned installation package was generated, and the path to the resource file is indicated by the-Z and subsequent paths, and the-F and subsequent paths indicate the path to the. dex file, and-RF indicates the directory of the source files.

(7) Sign the APK installation file using Jarsigner in the JDK

The purpose of the signature is to ensure the uniqueness of the application's developers, and the signature requires something other than the Jarsigner tool with a key file, the. keystore file, where we don't produce our own keystore files, but instead use Android The Debug.keystore file provided by the SDK is located under the. Android directory under My Documents. The principle of signature and the generation of key files are supplemented in the following blog post.

D:\java\jdk1.6.0_25\bin>jarsigner-keystore C:\docume~1\******\.android\debug.keystore-storepass Android- Keypass Android-signedjar c:\docume~1\******\workspace\helloandroid3\bin\hello3.apk C:\Docume~1\******\workspace\ helloandroid3\bin\helloandroid3.apk Androiddebugkey

-keystore and subsequent paths indicate the location of the key file,-storepass is the password used for the integrity of the KeyStore,-keypass is the password for the private key,-signedjar and the following path indicate the path of the APK file for the signed name, Next is the path of the apk that needs to be signed, followed by the alias of the key. Debug.keystore's name and passwords information is found in the SDK documentation, as follows:

The SDK tools create the debug Keystore/key with predetermined names/passwords:

Keystore Name: "Debug.keystore" Keystore Password: "Android" key alias: "Androiddebugkey" Key password: "Android" CN: "cn= Android Debug,o=android,c=us "

In-depth analysis of the Android build and run process

Related Article

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.