1 page.title=Build System Overview 2 3 @jd:body 4 5 <div id="qv-wrapper"> 6 <div id="qv"> 7 <h2>In this document</h2> 8 <ol> 9 <li><a href="#detailed-build">A Detailed Look at the Build Process</a> </li> 10 </ol> 11 <h2>See also</h2> 12 <ul> 13 <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> 16 <li><a href="{@docRoot}sdk/installing/migrate.html">Migrating from Eclipse</a></li> 17 </div> 18 </div> 19 20 <a class="notice-developers-video" href="https://www.youtube.com/watch?v=LCJAgPkpmR0#t=504"> 21 <div> 22 <h3>Video</h3> 23 <p>The New Android SDK Build System</p> 24 </div> 25 </a> 26 27 <p>The Android build system is the toolkit you use to build, test, run and package 28 your apps. The build system can run as an integrated tool from the Android Studio menu and 29 independently from the command line. You can use the features of the build system to:</p> 30 31 <ul> 32 <li>Customize, configure, and extend the build process.</li> 33 <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> 36 </ul> 37 38 <p>The flexibility of the Android build system enables you to achieve all of this without 39 modifying 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>. 41 To configure custom build settings in an Android Studio project, see 42 <a href="{@docRoot}tools/building/configuring-gradle.html">Configuring Gradle Builds</a>.</p> 43 44 45 <h2 id="detailed-build">A Detailed Look at the Build Process</h2> 46 47 <p>The build process involves many tools and processes that generate intermediate files on the 48 way to producing an <code>.apk</code>. If you are developing in Android Studio, the complete build 49 process is done every time you run the Gradle build task for your project or modules. The build 50 process is very flexible so it's useful, however, to understand what is happening under the hood 51 since much of the build process is configurable and extensible. The following diagram depicts the 52 different tools and processes that are involved in a build:</p> 53 54 <img src="{@docRoot}images/build.png" /> 55 56 <p>The general process for a typical build is outlined below. The build system merges all the 57 resources from the configured product flavors, build types, and dependencies. If different 58 folders contain resources with the same name or setting, the following override priority order is: 59 dependencies override build types, which override product flavors, which override the main source 60 directory.</p> 61 62 <ul> 63 64 <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> 67 68 <li>The aidl tool converts any <code>.aidl</code> interfaces that you have into Java interfaces.</li> 69 70 <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> 72 73 <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> 76 77 <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> 79 80 <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> 82 83 <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> 87 88 <p class="note"><b>Note:</b> Apps are limited to a 64K method reference limit. If your app reaches 89 this limit, the build process outputs the following error message: 90 91 <pre>Unable to execute dex: method ID not in [0, 0xffff]: 65536.</pre> 92 93 To avoid this error, see 94 <a href="{@docRoot}tools/building/multidex.html">Building Apps with Over 65K Methods</a>. 95 </p> 96 97 98 <h3>Build output</h3> 99 100 <p>The build generates an APK for each build variant in the <code>app/build</code> folder: 101 the <code>app/build/outputs/apk/</code> directory contains packages named 102 <code>app-<flavor>-<buildtype>.apk</code>; for example, <code>app-full-release.apk</code> and 103 <code>app-demo-debug.apk</code>.</p> 104 105 106