Building and Running
IN THIS DOCUMENT
During the build process, your Android projects are compiled and packaged into an .apk file, the container for your application binary. It contains all of the information necessary to run your application on a device or emulator, such as compiled .dex files (.class files converted to Dalvik byte code), a binary version of the AndroidManifest.xml file, compiled resources (resources.arsc) and uncompiled resource files for your application.
在整个编译过程中,你的安卓工程将会打包成一个.apk 文件,它是你的程序的二进制代码容器。他包含了在设备或者模拟器上运行你的应用程序的所有必要的信息,例如编译过的.dex文件(.class文件转化形成的Dalvik字节码),一个二进制版本的AndroidManifest.xml文件,编译过的资源文件(resources.arsc)以及其他未编译的资源文件。
If you are developing in Eclipse, the ADT plugin incrementally builds your project as you make changes to the source code. Eclipse outputs an .apk file automatically to the bin folder of the project, so you do not have to do anything extra to generate the .apk.
如果你在Eclipse中进行开发,那么ADT插件将会增量的构建你的应用程序中源码改变的部分。Eclipse自动在你工程的bin目录下,输出了一个.apk文件,所以你无需做其他任何额外的操作。
If you are developing in a non-Eclipse environment, you can build your project with the generated build.xml Ant file that is in the project directory. The Ant file calls targets that automatically call the build tools for you.
如果你在非Eclipse环境中开发,你可以使用Ant的build.xml来编译你的工程。Ant文件能够自动的为你调用编译工具。
To run an application on an emulator or device, the application must be signed using debug or release mode. You typically want to sign your application in debug mode when you develop and test your application, because the build tools use a debug key with a known password so you do not have to enter it every time you build. When you are ready to release the application to Google Play, you must sign the application in release mode, using your own private key.
为了在模拟器或者设备上运行你的应用程序,你的应用程序必须使用调试(debug)或者发布(release)模式进行签名。当你开发和测试你的应用程序的时候,你可能希望用调试模式进行签名,因为构建工具使用的是已知的调试签名,所以你在每次编译时无需键入密码。当你准备在谷歌市场上发布时,你必须使用你的私钥以发布模式进行签名。
Fortunately, Eclipse or your Ant build script signs the application for you in debug mode when you build your application. You can also easily setup Eclipse or your Ant build to sign your application in release mode as well. For more information on signing applications, see Signing Your Applications.
幸运的是,Eclipse或者你的Ant构建脚本能够以调试或者发布模式签名你的应用程序。更多信息,请看Signing Your Applications.
The following diagram depicts the components involved in building and running an application:
下面的图表描述了如何编译和运行:
A Detailed Look at the Build Process
The build process involves many tools and processes that generate intermediate files on the way to producing an .apk. If you are developing in Eclipse, the complete build process is automatically done periodically as you develop and save your code changes. If you are using other IDEs, this build process is done every time you run the generated Ant build script for your project. It is useful, however, to understand what is happening under the hood since much of the tools and processes are masked from you. The following diagram depicts the different tools and processes that are involved in a build:
构建程序包含了很多程序和过程,他们在产生.apk文件的过程中产生了很多中间中间。如果你在Eclipse中开发,在你保存改变的代码之后,完整的编译工具将会周期性的进行。如果你使用其他的IDE工具,构建过程会在你每次执行Ant编译脚本时执行。这很有意义,然而,为了理解构建工具隐藏背后发生了什么事情。下面这张图表将会描述不同的工具在你的构建过程中所进行的操作。
The general process for a typical build is outlined below:
一个典型的程序通过下面这些步骤生成:
- The Android Asset Packaging Tool (aapt) takes your application resource files, such as the
AndroidManifest.xmlfile and the XML files for your Activities, and compiles them. AnR.javais also produced so you can reference your resources from your Java code. - 安卓资源打包工具(aapt)获取你的应用程序的资源文件,例如AndroidManifest.xml文件,XML等,编译他们。一个R.java也是产品,以至于你能够通过Java代码引用他们。
- The aidl tool converts any
.aidlinterfaces that you have into Java interfaces. - aidl个工具用于转化任何的.aidl接口为Java接口
- All of your Java code, including the
R.javaand.aidlfiles, are compiled by the Java compiler and .class files are output. - 所有的Java代码,包括R.java以及.aidl文件,将会通过Java编译器,以.class文件类型输出。
- The dex tool converts the .class files to Dalvik byte code. Any 3rd party libraries and .class files that you have included in your project are also converted into
.dexfiles so that they can be packaged into the final.apkfile. - dex工具将会把.class文件转化为Dalvik字节码。任何第三方的.class文件也会转化为.dex文件,以便能够打包到.apk文件中。
- All non-compiled resources (such as images), compiled resources, and the .dex files are sent to the apkbuilder tool to be packaged into an
.apkfile. - 所有的非编译资源(例如图像),编译资源,以及.dex文件都会发送给apkbuilder工具,以便于打包成为你个.apk文件。
- Once the
.apkis built, it must be signed with either a debug or release key before it can be installed to a device. - 一旦.apk包构建后,你必须使用debug或者release进行签名,然后才能安装到设备上面。
- Finally, if the application is being signed in release mode, you must align the
.apkwith the zipalign tool. Aligning the final.apkdecreases memory usage when the application is running on a device. - 最终,如果应用程序以release进行了签名,你需要使用zipalign工具进行对齐。对齐的.apk将会在运行时节省内存开销。
构建与运行Android应用流程详解

本文详细介绍了Android应用从源代码到apk文件的构建全过程,包括编译、打包及签名步骤。阐述了Eclipse环境下如何自动构建apk,以及非Eclipse环境下使用Ant构建脚本的实现方式。同时,强调了应用程序在模拟器或设备上运行前的签名必要性,以及不同开发模式(debug vs. release)下的具体操作。
2996

被折叠的 条评论
为什么被折叠?



