blob: b68b1764d86bf56a74471450037f9571b17e9481 [file] [log] [blame]
Rich Slogar40833272014-11-06 17:15:28 -08001page.title=Build System Overview
Ricardo Cervera3cd141c2014-03-17 11:24:24 -07002
3@jd:body
4
5<div id="qv-wrapper">
6<div id="qv">
7<h2>In this document</h2>
8<ol>
Rich Slogar40833272014-11-06 17:15:28 -08009 <li><a href="#detailed-build">A Detailed Look at the Build Process</a> </li>
Ricardo Cervera3cd141c2014-03-17 11:24:24 -070010</ol>
11<h2>See also</h2>
12<ul>
Rich Slogar40833272014-11-06 17:15:28 -080013 <li><a href="{@docRoot}sdk/installing/studio.html">
14 Getting Started with Android Studio</a></li>
15 <li><a href="{@docRoot}tools/studio/index.html">Android Studio Basics</a></li>
smain@google.com6ae492e2014-12-08 11:28:16 -080016 <li><a href="{@docRoot}sdk/installing/migrate.html">Migrating from Eclipse</a></li>
Ricardo Cervera3cd141c2014-03-17 11:24:24 -070017</div>
18</div>
19
Rich Slogar40833272014-11-06 17:15:28 -080020<a class="notice-developers-video" href="https://www.youtube.com/watch?v=LCJAgPkpmR0#t=504">
Ricardo Cervera3cd141c2014-03-17 11:24:24 -070021<div>
22 <h3>Video</h3>
Rich Slogar40833272014-11-06 17:15:28 -080023 <p>The New Android SDK Build System</p>
Ricardo Cervera3cd141c2014-03-17 11:24:24 -070024</div>
25</a>
26
Rich Slogar40833272014-11-06 17:15:28 -080027<p>The Android build system is the toolkit you use to build, test, run and package
28your apps. The build system can run as an integrated tool from the Android Studio menu and
29independently from the command line. You can use the features of the build system to:</p>
Ricardo Cervera3cd141c2014-03-17 11:24:24 -070030
31<ul>
32 <li>Customize, configure, and extend the build process.</li>
Rich Slogar40833272014-11-06 17:15:28 -080033 <li>Create multiple APKs for your app with different features using the same project and
34 modules.</li>
35 <li>Reuse code and resources across source sets.</li>
Ricardo Cervera3cd141c2014-03-17 11:24:24 -070036</ul>
37
Rich Slogar40833272014-11-06 17:15:28 -080038<p>The flexibility of the Android build system enables you to achieve all of this without
39modifying your app's core source files. To build an Android Studio project, see
40<a href="{@docRoot}tools/building/building-studio.html">Building and Running from Android Studio</a>.
41To configure custom build settings in an Android Studio project, see
42<a href="{@docRoot}tools/building/configuring-gradle.html">Configuring Gradle Builds</a>.</p>
Ricardo Cervera3cd141c2014-03-17 11:24:24 -070043
44
Rich Slogar40833272014-11-06 17:15:28 -080045<h2 id="detailed-build">A Detailed Look at the Build Process</h2>
Ricardo Cervera3cd141c2014-03-17 11:24:24 -070046
Rich Slogar40833272014-11-06 17:15:28 -080047<p>The build process involves many tools and processes that generate intermediate files on the
48way to producing an <code>.apk</code>. If you are developing in Android Studio, the complete build
49process is done every time you run the Gradle build task for your project or modules. The build
50process is very flexible so it's useful, however, to understand what is happening under the hood
51since much of the build process is configurable and extensible. The following diagram depicts the
52different tools and processes that are involved in a build:</p>
Ricardo Cervera3cd141c2014-03-17 11:24:24 -070053
Rich Slogar40833272014-11-06 17:15:28 -080054 <img src="{@docRoot}images/build.png" />
Ricardo Cervera3cd141c2014-03-17 11:24:24 -070055
Rich Slogar40833272014-11-06 17:15:28 -080056<p>The general process for a typical build is outlined below. The build system merges all the
57resources from the configured product flavors, build types, and dependencies. If different
58folders contain resources with the same name or setting, the following override priority order is:
59dependencies override build types, which override product flavors, which override the main source
60directory.</p>
Ricardo Cervera3cd141c2014-03-17 11:24:24 -070061
Rich Slogar40833272014-11-06 17:15:28 -080062 <ul>
Ricardo Cervera3cd141c2014-03-17 11:24:24 -070063
Rich Slogar40833272014-11-06 17:15:28 -080064 <li>The Android Asset Packaging Tool (aapt) takes your application resource files, such as the
65 <code>AndroidManifest.xml</code> file and the XML files for your Activities, and compiles them.
66 An <code>R.java</code> is also produced so you can reference your resources from your Java code.</li>
Ricardo Cervera3cd141c2014-03-17 11:24:24 -070067
Rich Slogar40833272014-11-06 17:15:28 -080068 <li>The aidl tool converts any <code>.aidl</code> interfaces that you have into Java interfaces.</li>
Ricardo Cervera3cd141c2014-03-17 11:24:24 -070069
Rich Slogar40833272014-11-06 17:15:28 -080070 <li>All of your Java code, including the <code>R.java</code> and <code>.aidl</code> files, are
71 compiled by the Java compiler and .class files are output.</li>
Ricardo Cervera3cd141c2014-03-17 11:24:24 -070072
Rich Slogar40833272014-11-06 17:15:28 -080073 <li>The dex tool converts the .class files to Dalvik byte code. Any 3rd party libraries and
74 .class files that you have included in your module build are also converted into <code>.dex</code>
75 files so that they can be packaged into the final <code>.apk</code> file.</li>
Ricardo Cervera3cd141c2014-03-17 11:24:24 -070076
Rich Slogar40833272014-11-06 17:15:28 -080077 <li>All non-compiled resources (such as images), compiled resources, and the .dex files are
78 sent to the apkbuilder tool to be packaged into an <code>.apk</code> file.</li>
Ricardo Cervera3cd141c2014-03-17 11:24:24 -070079
Rich Slogar40833272014-11-06 17:15:28 -080080 <li>Once the <code>.apk</code> is built, it must be signed with either a debug or release key
81 before it can be installed to a device.</li>
Ricardo Cervera3cd141c2014-03-17 11:24:24 -070082
Rich Slogar40833272014-11-06 17:15:28 -080083 <li>Finally, if the application is being signed in release mode, you must align the
84 <code>.apk</code> with the zipalign tool. Aligning the final <code>.apk</code> decreases memory
85 usage when the application is -running on a device.</li>
86 </ul>
Ricardo Cervera3cd141c2014-03-17 11:24:24 -070087
Rich Slogar40833272014-11-06 17:15:28 -080088<p class="note"><b>Note:</b> Apps are limited to a 64K method reference limit. If your app reaches
89this limit, the build process outputs the following error message:
Ricardo Cervera3cd141c2014-03-17 11:24:24 -070090
Rich Slogar40833272014-11-06 17:15:28 -080091<pre>Unable to execute dex: method ID not in [0, 0xffff]: 65536.</pre>
Ricardo Cervera3cd141c2014-03-17 11:24:24 -070092
Rich Slogar40833272014-11-06 17:15:28 -080093To avoid this error, see
94<a href="{@docRoot}tools/building/multidex.html">Building Apps with Over 65K Methods</a>.
95</p>
Ricardo Cervera3cd141c2014-03-17 11:24:24 -070096
Ricardo Cervera3cd141c2014-03-17 11:24:24 -070097
Rich Slogar40833272014-11-06 17:15:28 -080098<h3>Build output</h3>
Ricardo Cervera3cd141c2014-03-17 11:24:24 -070099
Rich Slogar40833272014-11-06 17:15:28 -0800100<p>The build generates an APK for each build variant in the <code>app/build</code> folder:
Rich Slogar1dafeb32014-12-17 17:25:02 -0800101the <code>app/build/outputs/apk/</code> directory contains packages named
Ricardo Cervera3cd141c2014-03-17 11:24:24 -0700102<code>app-&lt;flavor>-&lt;buildtype>.apk</code>; for example, <code>app-full-release.apk</code> and
103<code>app-demo-debug.apk</code>.</p>
104
105