Android's compile and run process in-depth analysis _android

Source: Internet
Author: User

First take a look at the entire process of using the Java language for Android applications from the source code to the installation package, which includes compilations, links, and signatures, as follows:

(1) using the AAPT tool to generate R.java files

You can create an automatically android project first by building an eclipse development environment, and be sure to remove the check mark in front of the build option under the Project menu in Eclipse to 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 immediately following parameters are the paths used to specify the Androidmanifest.xml (configuration file),-j and the following parameters are the specified R.java build path,-s and the following parameters are the directory where the specified resource file is located, and-I and the following parameters are the Android platform class libraries that are specified to include; The R.java file is generated in the Gen directory in the process directory. The specific usage of AAPT can be seen when the command line enters AAPT.

The purpose of the R.java file is to provide access to the resource to the program, and for more details, see the post on the file structure and detailed description of the Android project later.

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

Aidl is a way for the Android system to provide an inter-process invocation, similar to an IPC call, through the Aidl tool that will use the Android Interface Definition The. aidl file described by language is compiled into a. java file that contains Java interface classes, and then the processes follow these interfaces to invoke each other. The. aidl file is typically stored with the program source file. For projects that are automatically created 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 isn't used, the input filename is used, with the. aidl extension changed to a. Java Extensio N.
If the-o option is used, the generated files would be placed in the base output folder, under their package folder

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

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 the period, I would like to use *.java to describe the source files that need to compile, but the hint can not find, and then the source file designated as a specific Helloandroid3.java file before compiling, strange.

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

(4) Convert many. class files to 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--output and subsequent paths indicate the build path of the. dex file, and the path of the red callout is the path to. class, and it should be noted that the package path cannot be added here, otherwise a mismatch error may be reported, and a package path might have been added to the batch. In addition, if the Windows system path contains spaces, all use abbreviated form, specific system path and what is the abbreviation, or ask Niang. After success, the. dex file is generated under the specified path. The. dex file is run on Android's Dalvik virtual machine, and the specifics of how it works are mentioned later.

(5) using the AAPT tool to package resource files

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_

In contrast to the generation of R.java files, you can see that the parameters have changed, less---M and-j, and if you look at the description in AAPT usage, you know that-M and-j are pairs that indicate the generation path of the R.java file. -M,-s, and-I have been mentioned before, no longer described here. The role of-F is to indicate the path of the packaged resource file, and at the end of the file, add the filename, preferably with the extension. This refers to the. AP_ suffix name that is developed automatically at compile time in eclipse.

(6) using Apkbuilder to generate unsigned apk installation files

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 followed by the Apkbuilder is the path to the generated apk installation file, the function of the-v parameter is to indicate the necessary information in the execution, and the 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-Z and subsequent paths indicate the path of the packaged resource file, the-F and subsequent paths indicate the path to the. dex file,-RF indicates the directory of the source file.

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

The purpose of the signature is to guarantee the uniqueness of the developer of the application, what the signature needs in addition to the Jarsigner tool and the key file, that is, the. keystore file, we do not produce our own KeyStore files, we use Android The Debug.keystore file provided by the SDK is located under the. Android directory under My Documents. The principle of the signature and the production of the key file are supplemented in the following posting.

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

The-keystore and subsequent paths indicate the location of the key file,-storepass is the password for the KeyStore integrity,-keypass is the password for the private key, and the-signedjar and subsequent paths indicate the path to the APK file for the signed name. The following 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 " <>< body=""><>
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.