1page.title=Building and Running 2@jd:body 3 4<div id="qv-wrapper"> 5 <div id="qv"> 6 <h2>In this document</h2> 7 <ol> 8 <li><a href="#detailed-build">A Detailed Look at the Build Process</a></li> 9 </ol> 10 </div> 11 </div> 12 13 <p>During the build process, your Android projects are compiled and packaged into an .apk file, 14 the container for your application binary. It contains all of the information necessary to run 15 your application on a device or emulator, such as compiled <code>.dex</code> files (<code>.class</code> files 16 converted to Dalvik byte code), a binary version of the <code>AndroidManifest.xml</code> file, compiled 17 resources (<code>resources.arsc</code>) and uncompiled resource files for your application.</p> 18 19 <p>If you are developing in Eclipse, the ADT plugin incrementally builds your project as you 20 make changes to the source code. Eclipse outputs an <code>.apk</code> file automatically to the bin folder of 21 the project, so you do not have to do anything extra to generate the <code>.apk</code>.</p> 22 23 <p>If you are developing in a non-Eclipse environment, you can build your project with the 24 generated <code>build.xml</code> Ant file that is in the project directory. The Ant file calls targets that 25 automatically call the build tools for you.</p> 26 27 <p>To run an application on an emulator or device, the application must be signed using debug or 28 release mode. You typically want to sign your application in debug mode when you develop and test 29 your application, because the build tools use a debug key with a known password so you do not have 30 to enter it every time you build. When you are ready to release the application to Google 31 Play, you must sign the application in release mode, using your own private key.</p> 32 33 <p>Fortunately, Eclipse or your Ant build script signs the application for you in debug mode 34 when you build your application. You can also easily setup Eclipse or your Ant build to sign your 35 application in release mode as well. For more information on signing applications, see <a href= 36 "{@docRoot}tools/publishing/app-signing.html">Signing Your Applications</a>.</p> 37 38 <p>The following diagram depicts the components involved in building and running an application:</p> 39 40 <img src="{@docRoot}images/build-simplified.png" /> 41 42 <h2 id="detailed-build">A Detailed Look at the Build Process</h2> 43 44 <p>The build process involves many tools and processes that generate intermediate files on the 45 way to producing an <code>.apk</code>. If you are developing in Eclipse, the complete build process is 46 automatically done periodically as you develop and save your code changes. If you are using other 47 IDEs, this build process is done every time you run the generated Ant build script for your 48 project. It is useful, however, to understand what is happening under the hood since much of the 49 tools and processes are masked from you. The following diagram depicts the different tools and 50 processes that are involved in a build:</p> 51 52 <img src="{@docRoot}images/build.png" /> 53 54 <p>The general process for a typical build is outlined below:</p> 55 56 <ul> 57 58 <li>The Android Asset Packaging Tool (aapt) takes your application resource files, such as the 59 <code>AndroidManifest.xml</code> file and the XML files for your Activities, and compiles them. An <code>R.java</code> is 60 also produced so you can reference your resources from your Java code.</li> 61 62 <li>The aidl tool converts any <code>.aidl</code> interfaces that you have into Java interfaces.</li> 63 64 <li>All of your Java code, including the <code>R.java</code> and <code>.aidl</code> files, are compiled by the Java 65 compiler and .class files are output.</li> 66 67 <li>The dex tool converts the .class files to Dalvik byte code. Any 3rd party libraries and 68 .class files that you have included in your project are also converted into <code>.dex</code> files so that 69 they can be packaged into the final <code>.apk</code> file.</li> 70 71 <li>All non-compiled resources (such as images), compiled resources, and the .dex files are 72 sent to the apkbuilder tool to be packaged into an <code>.apk</code> file.</li> 73 74 <li>Once the <code>.apk</code> is built, it must be signed with either a debug or release key before it can 75 be installed to a device.</li> 76 77 <li>Finally, if the application is being signed in release mode, you must align the <code>.apk</code> with 78 the zipalign tool. Aligning the final <code>.apk</code> decreases memory usage when the application is 79 running on a device.</li> 80 </ul> 81 82<p class="note"><b>Note:</b> Apps are limited to a 64K method reference limit. If your app reaches 83this limit, the build process outputs the following error message: 84 85<pre>Unable to execute dex: method ID not in [0, 0xffff]: 65536.</pre> 86 87To avoid this, you can 88<a href="http://android-developers.blogspot.com.es/2011/07/custom-class-loading-in-dalvik.html">load 89secondary dex files at runtime</a> and use 90<a href="http://developer.android.com/tools/help/proguard.html">ProGuard</a> to strip out unnecessary 91class references (Proguard only works when building in release mode). 92</p>