| Rich Slogar | 4083327 | 2014-11-06 17:15:28 -0800 | [diff] [blame] | 1 | page.title=Build System Overview |
| Ricardo Cervera | 3cd141c | 2014-03-17 11:24:24 -0700 | [diff] [blame] | 2 | |
| 3 | @jd:body |
| 4 | |
| 5 | <div id="qv-wrapper"> |
| 6 | <div id="qv"> |
| 7 | <h2>In this document</h2> |
| 8 | <ol> |
| Rich Slogar | 4083327 | 2014-11-06 17:15:28 -0800 | [diff] [blame] | 9 | <li><a href="#detailed-build">A Detailed Look at the Build Process</a> </li> |
| Ricardo Cervera | 3cd141c | 2014-03-17 11:24:24 -0700 | [diff] [blame] | 10 | </ol> |
| 11 | <h2>See also</h2> |
| 12 | <ul> |
| Rich Slogar | 4083327 | 2014-11-06 17:15:28 -0800 | [diff] [blame] | 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> |
| smain@google.com | 6ae492e | 2014-12-08 11:28:16 -0800 | [diff] [blame] | 16 | <li><a href="{@docRoot}sdk/installing/migrate.html">Migrating from Eclipse</a></li> |
| Ricardo Cervera | 3cd141c | 2014-03-17 11:24:24 -0700 | [diff] [blame] | 17 | </div> |
| 18 | </div> |
| 19 | |
| Rich Slogar | 4083327 | 2014-11-06 17:15:28 -0800 | [diff] [blame] | 20 | <a class="notice-developers-video" href="https://www.youtube.com/watch?v=LCJAgPkpmR0#t=504"> |
| Ricardo Cervera | 3cd141c | 2014-03-17 11:24:24 -0700 | [diff] [blame] | 21 | <div> |
| 22 | <h3>Video</h3> |
| Rich Slogar | 4083327 | 2014-11-06 17:15:28 -0800 | [diff] [blame] | 23 | <p>The New Android SDK Build System</p> |
| Ricardo Cervera | 3cd141c | 2014-03-17 11:24:24 -0700 | [diff] [blame] | 24 | </div> |
| 25 | </a> |
| 26 | |
| Rich Slogar | 4083327 | 2014-11-06 17:15:28 -0800 | [diff] [blame] | 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> |
| Ricardo Cervera | 3cd141c | 2014-03-17 11:24:24 -0700 | [diff] [blame] | 30 | |
| 31 | <ul> |
| 32 | <li>Customize, configure, and extend the build process.</li> |
| Rich Slogar | 4083327 | 2014-11-06 17:15:28 -0800 | [diff] [blame] | 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> |
| Ricardo Cervera | 3cd141c | 2014-03-17 11:24:24 -0700 | [diff] [blame] | 36 | </ul> |
| 37 | |
| Rich Slogar | 4083327 | 2014-11-06 17:15:28 -0800 | [diff] [blame] | 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> |
| Ricardo Cervera | 3cd141c | 2014-03-17 11:24:24 -0700 | [diff] [blame] | 43 | |
| 44 | |
| Rich Slogar | 4083327 | 2014-11-06 17:15:28 -0800 | [diff] [blame] | 45 | <h2 id="detailed-build">A Detailed Look at the Build Process</h2> |
| Ricardo Cervera | 3cd141c | 2014-03-17 11:24:24 -0700 | [diff] [blame] | 46 | |
| Rich Slogar | 4083327 | 2014-11-06 17:15:28 -0800 | [diff] [blame] | 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> |
| Ricardo Cervera | 3cd141c | 2014-03-17 11:24:24 -0700 | [diff] [blame] | 53 | |
| Rich Slogar | 4083327 | 2014-11-06 17:15:28 -0800 | [diff] [blame] | 54 | <img src="{@docRoot}images/build.png" /> |
| Ricardo Cervera | 3cd141c | 2014-03-17 11:24:24 -0700 | [diff] [blame] | 55 | |
| Rich Slogar | 4083327 | 2014-11-06 17:15:28 -0800 | [diff] [blame] | 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> |
| Ricardo Cervera | 3cd141c | 2014-03-17 11:24:24 -0700 | [diff] [blame] | 61 | |
| Rich Slogar | 4083327 | 2014-11-06 17:15:28 -0800 | [diff] [blame] | 62 | <ul> |
| Ricardo Cervera | 3cd141c | 2014-03-17 11:24:24 -0700 | [diff] [blame] | 63 | |
| Rich Slogar | 4083327 | 2014-11-06 17:15:28 -0800 | [diff] [blame] | 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> |
| Ricardo Cervera | 3cd141c | 2014-03-17 11:24:24 -0700 | [diff] [blame] | 67 | |
| Rich Slogar | 4083327 | 2014-11-06 17:15:28 -0800 | [diff] [blame] | 68 | <li>The aidl tool converts any <code>.aidl</code> interfaces that you have into Java interfaces.</li> |
| Ricardo Cervera | 3cd141c | 2014-03-17 11:24:24 -0700 | [diff] [blame] | 69 | |
| Rich Slogar | 4083327 | 2014-11-06 17:15:28 -0800 | [diff] [blame] | 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> |
| Ricardo Cervera | 3cd141c | 2014-03-17 11:24:24 -0700 | [diff] [blame] | 72 | |
| Rich Slogar | 4083327 | 2014-11-06 17:15:28 -0800 | [diff] [blame] | 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> |
| Ricardo Cervera | 3cd141c | 2014-03-17 11:24:24 -0700 | [diff] [blame] | 76 | |
| Rich Slogar | 4083327 | 2014-11-06 17:15:28 -0800 | [diff] [blame] | 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> |
| Ricardo Cervera | 3cd141c | 2014-03-17 11:24:24 -0700 | [diff] [blame] | 79 | |
| Rich Slogar | 4083327 | 2014-11-06 17:15:28 -0800 | [diff] [blame] | 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> |
| Ricardo Cervera | 3cd141c | 2014-03-17 11:24:24 -0700 | [diff] [blame] | 82 | |
| Rich Slogar | 4083327 | 2014-11-06 17:15:28 -0800 | [diff] [blame] | 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> |
| Ricardo Cervera | 3cd141c | 2014-03-17 11:24:24 -0700 | [diff] [blame] | 87 | |
| Rich Slogar | 4083327 | 2014-11-06 17:15:28 -0800 | [diff] [blame] | 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: |
| Ricardo Cervera | 3cd141c | 2014-03-17 11:24:24 -0700 | [diff] [blame] | 90 | |
| Rich Slogar | 4083327 | 2014-11-06 17:15:28 -0800 | [diff] [blame] | 91 | <pre>Unable to execute dex: method ID not in [0, 0xffff]: 65536.</pre> |
| Ricardo Cervera | 3cd141c | 2014-03-17 11:24:24 -0700 | [diff] [blame] | 92 | |
| Rich Slogar | 4083327 | 2014-11-06 17:15:28 -0800 | [diff] [blame] | 93 | To avoid this error, see |
| 94 | <a href="{@docRoot}tools/building/multidex.html">Building Apps with Over 65K Methods</a>. |
| 95 | </p> |
| Ricardo Cervera | 3cd141c | 2014-03-17 11:24:24 -0700 | [diff] [blame] | 96 | |
| Ricardo Cervera | 3cd141c | 2014-03-17 11:24:24 -0700 | [diff] [blame] | 97 | |
| Rich Slogar | 4083327 | 2014-11-06 17:15:28 -0800 | [diff] [blame] | 98 | <h3>Build output</h3> |
| Ricardo Cervera | 3cd141c | 2014-03-17 11:24:24 -0700 | [diff] [blame] | 99 | |
| Rich Slogar | 4083327 | 2014-11-06 17:15:28 -0800 | [diff] [blame] | 100 | <p>The build generates an APK for each build variant in the <code>app/build</code> folder: |
| Rich Slogar | 1dafeb3 | 2014-12-17 17:25:02 -0800 | [diff] [blame] | 101 | the <code>app/build/outputs/apk/</code> directory contains packages named |
| Ricardo Cervera | 3cd141c | 2014-03-17 11:24:24 -0700 | [diff] [blame] | 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 | |