官方文档
The Android build system is the toolkit you use to build, test, run and package your apps. Android构建系统是一个完整的工具包,你可以用来构建、测试、运行和打包你的代码。
You can use the features of the build system to: 该构建系统有这么几个特点:
Customize, configure, and extend the build PRocess. 自定义、配置和扩展该构建过程Create multiple APKs for your app with different features using the same project and modules. 同样的程序可以被打包出各种不同的apk。Reuse code and resources across source sets. 代码和资源可以被重复利用。The build process involves many tools and processes that generate intermediate files on the way to producing an .apk. Android系统构建一个apk的过程包含很多工具和过程。
The build system merges all the resources from the configured product flavors, build types, and dependencies. 该构建系统会合并所有的资源。包括配置的flavors,build types 和依赖等。
下面这张图可以完整的描述出在构建过程中用到的不同的工具: 绿色的全部为工具 
The Android Asset Packaging Tool(aapt) takes your application resource files, such as the AndroidManifest.xml file and the XML files for your Activities, and compiles them. An R.java is also produced so you can reference your resources from your Java code. aapt编译所有的资源文件,并生成R.java,所以你可以通过R.Java文件去访问你的资源。
The aidl tool converts any .aidl interfaces that you have into Java interfaces. aild Tool 将所有的.aidl文件生成Java Interface。
All of your Java code, including the R.java and .aidl files, are compiled by the Java compiler and .class files are output. Java Compiler将把所有的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 module build are also converted into .dex files so that they can be packaged into the final .apk file. Dex Tool 将把.class文件转换为 .dex文件。所有的三方依赖库也会被转换为.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 .apk file. apkBuild Tool 将所有的未被编译的资源(images等)、已编译资源、.dex文件等编译生成.apk文件
Once the .apk is built, it must be signed with either a debug or release key before it can be installed to a device. JarSinger Tool将会给该.apk文件签名,根据不同的配置使用不同的key
Finally, if the application is being signed in release mode, you must align the .apk with the zipalign tool. Aligning the final .apk decreases memory usage when the application is running on a device. 在打release包时,zipalign Tool 将会对该 .apk文件进行对齐并优化,可以减少该apk的内存占用。必须要配置使用它。 官方文档 zipalign:zipalign is an archive alignment tool that provides important optimization to Android application (.apk) files.
打包后的apk文件放在app/build/outputs/apk/目录下。默认命名为app-flavor-buildtype.apk。 比如app-full-release.apk and app-demo-debug.apk.
新闻热点
疑难解答