page.title=Build System Overview

@jd:body

<div id="qv-wrapper">
<div id="qv">
<h2>In this document</h2>
<ol>
     <li><a href="#detailed-build">A Detailed Look at the Build Process</a> </li>
</ol>
<h2>See also</h2>
<ul>
   <li><a href="{@docRoot}sdk/installing/studio.html">
   Getting Started with Android Studio</a></li>
   <li><a href="{@docRoot}tools/studio/index.html">Android Studio Basics</a></li>
   <li><a href="{@docRoot}sdk/installing/migrate.html">Migrating from Eclipse</a></li>
</div>
</div>

<a class="notice-developers-video" href="https://www.youtube.com/watch?v=LCJAgPkpmR0#t=504">
<div>
    <h3>Video</h3>
    <p>The New Android SDK Build System</p>
</div>
</a>

<p>The Android build system is the toolkit you use to build, test, run and package
your apps. The build system can run as an integrated tool from the Android Studio menu and
independently from the command line. You can use the features of the build system to:</p>

<ul>
    <li>Customize, configure, and extend the build process.</li>
    <li>Create multiple APKs for your app with different features using the same project and
    modules.</li>
    <li>Reuse code and resources across source sets.</li>
</ul>

<p>The flexibility of the Android build system enables you to achieve all of this without
modifying your app's core source files. To build an Android Studio project, see
<a href="{@docRoot}tools/building/building-studio.html">Building and Running from Android Studio</a>.
To configure custom build settings in an Android Studio project, see
<a href="{@docRoot}tools/building/configuring-gradle.html">Configuring Gradle Builds</a>.</p>


<h2 id="detailed-build">A Detailed Look at the Build Process</h2>

<p>The build process involves many tools and processes that generate intermediate files on the
way to producing an <code>.apk</code>. If you are developing in Android Studio, the complete build
process is done every time you run the Gradle build task for your project or modules. The build
process is very flexible so it's useful, however, to understand what is happening under the hood
since much of the build process is configurable and extensible. The following diagram depicts the
different tools and processes that are involved in a build:</p>

  <img src="{@docRoot}images/build.png" />

<p>The general process for a typical build is outlined below. The build system merges all the
resources from the configured product flavors, build types, and dependencies. If different
folders contain resources with the same name or setting, the following override priority order is:
dependencies override build types, which override product flavors, which override the main source
directory.</p>

  <ul>

    <li>The Android Asset Packaging Tool (aapt) takes your application resource files, such as the
    <code>AndroidManifest.xml</code> file and the XML files for your Activities, and compiles them.
    An <code>R.java</code> is also produced so you can reference your resources from your Java code.</li>

    <li>The aidl tool converts any <code>.aidl</code> interfaces that you have into Java interfaces.</li>

    <li>All of your Java code, including the <code>R.java</code> and <code>.aidl</code> files, are
    compiled by the Java compiler and .class files are output.</li>

    <li>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 <code>.dex</code>
    files so that they can be packaged into the final <code>.apk</code> file.</li>

    <li>All non-compiled resources (such as images), compiled resources, and the .dex files are
    sent to the apkbuilder tool to be packaged into an <code>.apk</code> file.</li>

    <li>Once the <code>.apk</code> is built, it must be signed with either a debug or release key
    before it can be installed to a device.</li>

    <li>Finally, if the application is being signed in release mode, you must align the
    <code>.apk</code> with the zipalign tool. Aligning the final <code>.apk</code> decreases memory
    usage when the application is -running on a device.</li>
  </ul>

<p class="note"><b>Note:</b> Apps are limited to a 64K method reference limit. If your app reaches
this limit, the build process outputs the following error message:

<pre>Unable to execute dex: method ID not in [0, 0xffff]: 65536.</pre>

To avoid this error, see
<a href="{@docRoot}tools/building/multidex.html">Building Apps with Over 65K Methods</a>.
</p>


<h3>Build output</h3>

<p>The build generates an APK for each build variant in the <code>app/build</code> folder:
the <code>app/build/outputs/apk/</code> directory contains packages named
<code>app-&lt;flavor>-&lt;buildtype>.apk</code>; for example, <code>app-full-release.apk</code> and
<code>app-demo-debug.apk</code>.</p>


