Home | History | Annotate | Download | only in installing
      1 page.title=Building Your Project with Gradle
      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="#overviewBuild">Overview of the Build System</a>
     10         <ol>
     11             <li><a href="#buildConf">Build configuration</a></li>
     12             <li><a href="#buildConv">Build by convention</a></li>
     13             <li><a href="#projectModules">Projects and modules</a></li>
     14             <li><a href="#dependencies">Dependencies</a></li>
     15             <li><a href="#buildTasks">Build tasks</a></li>
     16             <li><a href="#gradleWrapper">The Gradle wrapper</a></li>
     17         </ol>
     18     </li>
     19     <li><a href="#creatingBuilding">Create and Build a Project</a>
     20         <ol>
     21             <li><a href="#createProject">Create a project</a></li>
     22             <li><a href="#projectStructure">Project structure</a></li>
     23             <li><a href="#addLibModule">Add a library module</a></li>
     24             <li><a href="#buildProject">Build the project</a></li>
     25             <li><a href="#buildCmd">Build from the command line</a></li>
     26             <li><a href="#buildRelease">Build a release version</a></li>
     27         </ol>
     28     </li>
     29     <li><a href="#configBuild">Configure the Build</a>
     30         <ol>
     31             <li><a href="#buildFileBasics">Build file basics</a></li>
     32             <li><a href="#declareDeps">Declare dependencies</a></li>
     33             <li><a href="#runProguard">Run ProGuard</a></li>
     34             <li><a href="#configureSigning">Configure signing settings</a></li>
     35             <li><a href="#workBuildVariants">Work with build variants</a></li>
     36         </ol>
     37     </li>
     38     <li><a href="#reference">Reference</a></li>
     39 </ol>
     40 <h2>See also</h2>
     41 <ul>
     42 <li><a href="{@docRoot}sdk/installing/studio.html">
     43 Getting Started with Android Studio</a></li>
     44 <li><a href="{@docRoot}sdk/installing/studio-tips.html">
     45 Android Studio Tips and Tricks</a></li>
     46 <li><a href="{@docRoot}sdk/installing/migrate.html">
     47 Migrating from Eclipse</a></li>
     48 </div>
     49 </div>
     50 
     51 <a class="notice-developers-video"
     52 href="https://developers.google.com/events/io/sessions/324603352">
     53 <div>
     54     <h3>Video</h3>
     55     <p>What's New in Android Developer Tools</p>
     56 </div>
     57 </a>
     58 
     59 <p>The Android Studio build system is the toolkit you use to build, test, run and package
     60 your apps. The build system is independent from Android Studio, so you can invoke it from Android
     61 Studio or from the command line. After you write your application, you can use the features
     62 of the build system to:</p>
     63 
     64 <ul>
     65     <li>Customize, configure, and extend the build process.</li>
     66     <li>Create multiple APKs for your app with different features using the same project.</li>
     67     <li>Reuse code and resources.</li>
     68 </ul>
     69 
     70 <p>The flexibility of the Android Studio build system enables you to achieve all of this without
     71 modifying your app's core project files.</p>
     72 
     73 
     74 <h2 id="overviewBuild">Overview of the Build System</h2>
     75 
     76 <p>The Android Studio build system consists of an Android plugin for <em>Gradle</em>.
     77 <a href="http://www.gradle.org/">Gradle</a> is an advanced build toolkit that manages dependencies
     78 and allows you to define custom build logic. Many software projects use Gradle to manage their
     79 builds. The Android plugin for Gradle does not depend on Android Studio, although Android Studio
     80 is fully integrated with it. This means that:</p>
     81 
     82 <ul>
     83     <li>You can build your Android apps from the command line on your machine or on machines
     84         where Android Studio is not installed (such as continuous integration servers).</li>
     85     <li>You can build your Android apps from Android Studio with the same custom build
     86         configuration and logic as when you build from the command line.</li>
     87 </ul>
     88 
     89 <p>The output of the build is the same whether you are building a project from the command line,
     90 on a remote machine, or using Android Studio.</p>
     91 
     92 <h3 id="buildConf">Build configuration</h3>
     93 
     94 <p>The build configuration for your project is defined inside <em>Gradle build files</em>,
     95 which are plain text files that use the syntax and options from Gradle and the Android plugin
     96 to configure the following aspects of your build:</p>
     97 
     98 <ul>
     99     <li><em>Build variants</em>. The build system can generate multiple APKs with different
    100         configurations for the same project. This is useful when you want to build different
    101         versions of your application without having to create a separate project for each of
    102         them.</li>
    103     <li><em>Dependencies</em>. The build system manages project dependencies and supports
    104         dependencies from your local filesystem and from remote repositories. This prevents you
    105         from having to search, download, and copy binary packages for your dependencies into your
    106         project directory.</li>
    107     <li><em>Manifest entries</em>. The build system enables you to specify values for some
    108         elements of the manifest file in the build configuration. These new values override the
    109         existing values in the manifest file. This is useful if you want to generate multiple APKs
    110         for your project where each of them has a different package name, minimum SDK version, or
    111         target SDK version.</li>
    112     <li><em>Signing</em>. The build system enables you to specify signing settings in the build
    113         configuration, and it can sign your APKs during the build process.</li>
    114     <li><em>ProGuard</em>. The build system enables you to specify a different
    115         <a href="{@docRoot}tools/help/proguard.html">ProGuard</a> rules
    116         file for each build variant. The build system can run ProGuard to obfuscate your classes
    117         during the build process.</li>
    118     <li><em>Testing</em>. The build system generates a test APK from the test sources in your
    119         project, so you do not have to create a separate test project. The build system can run
    120         your tests during the build process.</li>
    121 </ul>
    122 
    123 <p>Gradle build files use <em>Groovy</em> syntax.
    124 <a href="http://groovy.codehaus.org/">Groovy</a> is a dynamic language that you can use to
    125 define custom build logic and to interact with the Android-specific elements provided by the
    126 Android plugin for Gradle.</p>
    127 
    128 <h3 id="buildConv">Build by convention</h3>
    129 
    130 <p>The Android Studio build system assumes <em>sensible defaults</em> for the project structure
    131 and other build options. If your project adheres to these conventions, your Gradle build files are
    132 very simple. When some these conventions do not apply to your project, the flexibility of the
    133 build system allows you to configure almost every aspect of the build process. For example, if
    134 the sources for your project are located in a different directory than the default, you can
    135 specify this location in the build file.</p>
    136 
    137 <h3 id="projectModules">Projects and modules</h3>
    138 
    139 <p>A <em>project</em> in Android Studio represents a complete Android app. Android Studio
    140 projects consist of one or more modules. A <em>module</em> is a component of your app that you can
    141 build, test, or debug independently. Modules contain the source code and resources for your app.
    142 Android Studio projects contain three kinds of modules:</p>
    143 
    144 <ul>
    145     <li><em>Java library modules</em> contain reusable code. The build system generates a
    146         JAR package for Java library modules.</li>
    147     <li><em>Android library modules</em> contain reusable Android-specific code and resources.
    148         The build system generates an AAR (Android ARchive) package for library modules.</li>
    149     <li><em>Android application modules</em> contain application code and may depend on library
    150         modules, although many Android apps consists of only one application module. The build
    151         system generates an APK package for application modules.</li>
    152 </ul>
    153 
    154 <p>Android Studio projects contain a top-level Gradle build file that lists all the modules in
    155 the project, and each module contains its own Gradle build file.</p>
    156 
    157 <h3 id="dependencies">Dependencies</h3>
    158 
    159 <p>The Android Studio build system manages project dependencies and supports module dependencies,
    160 local binary dependencies, and remote binary dependencies.</p>
    161 
    162 <dl>
    163     <dt><em>Module Dependencies</em></dt>
    164     <dd><p>A project module can include in its build file a list of other modules it depends on.
    165         When you build this module, the build system assembles and includes the required
    166         modules.</p></dd>
    167     <dt><em>Local Dependencies</em></dt>
    168     <dd><p>If you have binary archives in your local filesystem that a module depends on, such as
    169         JAR files, you can declare these dependencies in the build file for that
    170         module.</p></dd>
    171     <dt><em>Remote Dependencies</em></dt>
    172     <dd><p>When some of your dependencies are available in a remote repository, you do not have
    173         to download them and copy them into your project. The Android Studio build system supports
    174         remote <em>Maven</em> dependencies. <a href="http://maven.apache.org/">Maven</a> is a
    175         popular software project management tool that helps organize project dependencies using
    176         repositories.</p>
    177         <p>Many popular software libraries and tools are available in public Maven repositories.
    178         For these dependencies you only have to specify their Maven coordinates, which uniquely
    179         identify each element in a remote repository. The format for Maven coordinates used in the
    180         build system is <code>group:name:version</code>. For example, the Maven coordinates for
    181         version 16.0.1 of the Google Guava libraries are
    182         <code>com.google.guava:guava:16.0.1</code>.</p>
    183         <p>The <a href="http://search.maven.org">Maven Central Repository</a> is widely used to
    184         distribute many libraries and tools.</p>
    185     </dd>
    186 </dl>
    187 
    188 <h3 id="buildTasks">Build tasks</h3>
    189 
    190 <p>The Android Studio build system defines a hierarchical set of build tasks: the top-level
    191 tasks invoke the tasks they depend on to produce the necessary outcomes. The build system
    192 provides project tasks to build your app and module tasks to build modules independently.</p>
    193 
    194 <p>You can view the list of available tasks and invoke any task from Android Studio and from
    195 the command line, as described in
    196 <a href="#buildProject">Build the project in Android Studio</a> and and
    197 <a href="#buildCmd">Build the project from the command line</a>.</p>
    198 
    199 <h3 id="gradleWrapper">The Gradle wrapper</h3>
    200 
    201 <p>Android Studio projects contain the <em>Gradle wrapper</em>, which consists of:</p>
    202 
    203 <ul>
    204     <li>A JAR file</li>
    205     <li>A properties file</li>
    206     <li>A shell script for Windows platforms</li>
    207     <li>A shell script for Mac and Linux platforms</li>
    208 </ul>
    209 
    210 <p class="note"><strong>Note:</strong> You should submit all of these files to your source
    211 control system.</p>
    212 
    213 <p>Using the Gradle wrapper (instead of the local Gradle installation) ensures that
    214 you always run the version of Gradle defined in the properties file. To configure your project
    215 to use a newer version of Gradle, edit the properties file and specify the new version there.
    216 
    217 <p>Android Studio reads the properties file from the Gradle wrapper directory inside your project
    218 and runs the wrapper from this directory, so you can seamlessly work with multiple projects
    219 that require different versions of Gradle.</p>
    220 
    221 <p class="note"><strong>Note:</strong> Android Studio does not use the shell scripts, so any
    222 changes you make to them won't work when building from the IDE. You should define your custom
    223 logic inside Gradle build files instead.</p>
    224 
    225 <p>You can run the shell scripts to build your project from the command line on your development
    226 machine and on other machines where Android Studio is not installed.</p>
    227 
    228 
    229 <h2 id="creatingBuilding">Create and Build an Android Studio Project</h2>
    230 
    231 <p>This section builds on the concepts presented above and shows you how to:</p>
    232 
    233 <ul>
    234     <li>Create projects and modules.</li>
    235     <li>Work with the project structure.</li>
    236     <li>Edit build files to configure the build process.</li>
    237     <li>Build and run your app.</li>
    238 </ul>
    239 
    240 <h3 id="createProject">Create a project in Android Studio</h3>
    241 
    242 <p>To create a new project in Android Studio:</p>
    243 
    244 <ol>
    245     <li>Click <strong>File</strong> and select <strong>New Project</strong>.</li>
    246     <li>In the window that appears, enter "BuildSystemExample" in the <em>Application</em>
    247         name field.</li>
    248     <li>Leave the rest of the values unchanged and click <strong>Next</strong>.</li>
    249     <li>Leave the default icon settings unchanged and click <strong>Next</strong>.</li>
    250     <li>Select <em>Blank Activity</em> and click <strong>Next</strong>.</li>
    251     <li>Leave the default activity and layout names unchanged and click
    252         <strong>Finish</strong>.</li>
    253 </ol>
    254 
    255 <p>Figure 1 shows how the Android Studio window looks like after creating the project.</p>
    256 
    257 <img src="{@docRoot}images/tools/as-mainscreen.png" alt="" />
    258 <p class="img-caption"><strong>Figure 1.</strong> Previewing your app.</p>
    259 
    260 <h3 id="projectStructure">The project structure</h3>
    261 
    262 <p>Android Studio projects contain an application module by default (<code>app</code>).
    263 Table 1 lists where the main components of your app are located inside this module.</p>
    264 
    265 <p class="table-caption" id="table1">
    266 <strong>Table 1.</strong> Default location of the components in an application module.</p>
    267 <table>
    268     <tr>
    269         <th scope="col">Component</th>
    270         <th scope="col">Location</th>
    271     </tr>
    272     <tr>
    273         <td>Source files</td>
    274         <td><code>app/src/main/java/&lt;package>/</code></td>
    275     </tr>
    276     <tr>
    277         <td>Resource files</td>
    278         <td><code>app/src/main/res/</code></td>
    279     </tr>
    280     <tr>
    281         <td>Manifest file</td>
    282         <td><code>app/src/main/AndroidManifest.xml</code></td>
    283     </tr>
    284     <tr>
    285         <td>Build file</td>
    286         <td><code>app/build.gradle</code></td>
    287     </tr>
    288 </table>
    289 
    290 <p>When you add additional modules to your project, the directory structure for each module is
    291 similar to the one shown in table 1, replacing <code>app</code> by the name of the module.</p>
    292 
    293 <h3 id="addLibModule">Add a library module</h3>
    294 
    295 <p>This section shows you how to add a library module to your project and how to add this
    296 library as a dependency of an application module.</p>
    297 
    298 <h4>Create a new library module</h4>
    299 
    300 <p>It is good development practice to group functionality that you may reuse in other apps inside
    301 a library module. To create a library module inside the <code>BuildSystemExample</code>
    302 project:</p>
    303 
    304 <ol>
    305     <li>Click <strong>File</strong> and select <strong>New Module</strong>.</li>
    306     <li>On the window that appears, select <strong>Android Library</strong> and click
    307         <strong>Next</strong>.</li>
    308     <li>Leave the default module name (<code>lib</code>) unchanged and click
    309         <strong>Next</strong>.</li>
    310     <li>Select <em>Blank Activity</em> and click <strong>Next</strong>.</li>
    311     <li>Type "LibActivity1" on the <em>Activity Name</em> field and click
    312         <strong>Finish</strong>.</li>
    313 </ol>
    314 
    315 <p>The project now contains two modules, <code>app</code> and <code>lib</code>, with one activity
    316 in each module.</p>
    317 
    318 <h4 id="openActFromLib">Open an activity from a library module</h4>
    319 
    320 <p>Library modules contain activities and other logic that one or more application modules reuse.
    321 In this example, <code>MainActivity</code> in the app module opens <code>LibActivity1</code>
    322 from the <code>lib</code> module. To open <code>LibActivity1</code> from
    323 <code>MainActivity</code>:</p>
    324 
    325 <ol>
    326     <li>
    327         <p>Edit the layout file for <code>MainActivity</code> in the <code>app</code> module.
    328         This file is located in <code>app/src/main/res/layout/activity_main.xml</code>. Replace
    329         the contents of this file with the following:</p>
    330         <p><pre>
    331 &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    332     xmlns:tools="http://schemas.android.com/tools"
    333     android:layout_width="match_parent"
    334     android:layout_height="match_parent"
    335     tools:context="com.buildsystemexample.app.MainActivity">
    336 
    337     &lt;Button
    338         android:id="@+id/button1"
    339         android:layout_width="wrap_content"
    340         android:layout_height="wrap_content"
    341         android:text="@string/button1"
    342         android:onClick="onButton1Clicked"/>
    343 
    344 &lt;/LinearLayout>
    345 </pre></p>
    346     </li>
    347     <li>
    348         In this layout file, click on the line that contains
    349         <code>android:text="@string/button1"</code> and press <strong>Alt+Enter</strong>. Follow
    350         the suggestion from Android Studio to add a string resource with the value
    351         "Open LibActivity1".
    352     </li>
    353     <li>
    354         In this layout file, click on the line that contains
    355         <code>android:onClick="onButton1Clicked"</code> and press <strong>Alt+Enter</strong>.
    356         Follow the suggestion from Android Studio to add the <code>onButton1Clicked</code>
    357         method to <code>MainActivity</code>.
    358     </li>
    359     <li>
    360         <p>Copy the following code inside the <code>onButton1Clicked</code> method in
    361         <code>MainActivity</code>:</p>
    362         <p><pre>
    363 public void onButton1Clicked(View view) {
    364     Intent intent = new Intent(this, LibActivity1.class);
    365     startActivity(intent);
    366 }</pre></p>
    367     </li>
    368     <li>
    369         Click on <code>LibActivity1</code> in the first line inside the
    370         <code>onButton1Clicked</code> method of <code>MainActivity</code> and press
    371         <strong>Alt+Enter</strong>. Follow the suggestion from Android Studio to add an import
    372         for <code>LibActivity1</code> from the lib module.
    373     </li>
    374 </ol>
    375 
    376 <p>When the user taps the <strong>Open LibActivity1</strong> button on <code>MainActivity</code>
    377 (from the <code>app</code> module), <code>LibActivity1</code> (from the <code>lib</code> module)
    378 starts.</p>
    379 
    380 <h4>Add a dependency on a library module</h4>
    381 
    382 <p>The <code>app</code> module now depends on the <code>lib</code> module, but the build system
    383 does not know about this yet. Edit the build file for the <code>app</code> module (
    384 <code>app/build.gradle</code>) and add a dependency on the <code>lib</code> module:</p>
    385 
    386 <pre>
    387 ...
    388 dependencies {
    389     ...
    390     compile project(":lib")
    391 }
    392 </pre>
    393 
    394 <p>The <code>lib</code> module can still be built and tested independently, and the build system
    395 creates an AAR package for it that you could reuse in other projects.</p>
    396 
    397 <h3 id="buildProject">Build the project in Android Studio</h3>
    398 
    399 <p>To build the project on Android Studio, click <strong>Build</strong> and select
    400 <strong>Make Project</strong>. The status bar at the bottom of the window shows the current
    401 progress of the build:</p>
    402 
    403 <p><code>Gradle: Executing tasks: [:app:assembleDebug, :lib:bundleDebug]</code></p>
    404 
    405 <p class="note">If your project uses product flavors, Android Studio invokes the task for the
    406 selected build variant. For more information, see <a href="#workBuildVariants">Work with build
    407 variants.</a></p>
    408 
    409 <p>Click <img src="{@docRoot}images/tools/as-gradlebutton.png" alt=""
    410 style="vertical-align:bottom;margin:0;"/> on the bottom
    411 right part of the window to show the <em>Gradle Console</em>, as shown in figure 2.</p>
    412 
    413 <img src="{@docRoot}images/tools/as-gradleconsole.png" alt="" />
    414 <p class="img-caption"><strong>Figure 2.</strong> The Gradle Console in Android Studio.</p>
    415 
    416 <p>The Gradle Console shows the build tasks and subtasks that the build system runs for
    417 Android Studio. If the build fails, you can find more details on the console. To hide the Gradle
    418 Console, click <img src="{@docRoot}images/tools/as-gradlebutton.png" alt=""
    419 style="vertical-align:bottom;margin:0;"/> again.</p>
    420 
    421 <p>To view the list of all available build tasks in Android Studio, click <strong>Gradle</strong>
    422 on the right side of the IDE window. The <em>Gradle tasks</em> panel appears as shown in
    423 figure 3. Double-click any build task to run it in Android Studio. To hide the <em>Gradle tasks</em>
    424 panel, click <strong>Gradle</strong> again.</p>
    425 
    426 <img src="{@docRoot}images/tools/as-gradlepanel.png" alt="" />
    427 <p class="img-caption"><strong>Figure 3.</strong> The list of build tasks in Android Studio.</p>
    428 
    429 
    430 <h3 id="buildCmd">Build the project from the command line</h3>
    431 
    432 <p>To build the project from the command line, open a terminal window and navigate to the project
    433 root. On Windows platforms, type this command:</p>
    434 
    435 <pre>
    436 > gradlew.bat assembleDebug
    437 </pre>
    438 
    439 <p>On Mac OS and Linux platforms, type these commands:</p>
    440 
    441 <pre>
    442 $ chmod +x gradlew
    443 $ ./gradlew assembleDebug
    444 </pre>
    445 
    446 <p>The first command (<code>chmod</code>) adds the execution permission to the Gradle wrapper
    447 script and is only necessary the first time you build this project from the command line.</p>
    448 
    449 <p>The output of <code>gradlew</code> is similar to the output in the Gradle Console from
    450 figure 2.</p>
    451 
    452 <p>The <code>assembleDebug</code> build task builds the debug version of your app and signs it
    453 with the default local certificate, so that you can install it on the emulator and on real devices
    454 for debugging purposes.</p>
    455 
    456 <p>After you build the project, the output APK for the app module is located in
    457 <code>app/build/outputs/apk/</code>, and the output AAR for the lib module is located in
    458 <code>lib/build/outputs/libs/</code>.</p>
    459 
    460 <p>To see a list of all available build tasks for your project, type this command:</p>
    461 
    462 <pre>
    463 $ ./gradlew tasks
    464 </pre>
    465 
    466 
    467 <h3 id="buildRelease">Build a release version</h3>
    468 
    469 <p>You can build the release version of your application from the command line or using Android
    470 Studio. To build it from the command line, invoke the <code>assembleRelease</code> build task using
    471 the Gradle wrapper script (<code>gradlew assembleRelease</code>). To build it from Android
    472 Studio:</p>
    473 
    474 <ol>
    475     <li>Click <strong>Gradle</strong> on the right side of the IDE window.</li>
    476     <li>On the <em>All tasks</em> section of the sidebar that appears, expand
    477         <strong>BuildSystemExample</strong>.</li>
    478     <li>Expand <strong>:app</strong> and double-click <strong>assembleRelease</strong>.</li>
    479 </ol>
    480 
    481 <p>You can use this procedure to invoke any build task from Android Studio.</p>
    482 
    483 
    484 
    485 <h2 id="configBuild">Configure the Build</h2>
    486 
    487 <p>This section uses the <code>BuildSystemExample</code> project from the previous section and
    488 shows you how to:</p>
    489 
    490 <ul>
    491     <li>Use the syntax from the Android plugin for Gradle in build files.</li>
    492     <li>Declare dependencies.</li>
    493     <li>Configure ProGuard settings.</li>
    494     <li>Configure signing settings.</li>
    495     <li>Work with build variants.</li>
    496 </ul>
    497 
    498 <h3 id="buildFileBasics">Build file basics</h3>
    499 
    500 <p>Android Studio projects contain a top-level build file and a build file for each module. The
    501 build files are called <code>build.gradle</code>, and they are plain text files that use
    502 <a href="http://groovy.codehaus.org">Groovy</a> syntax to configure the build with the elements
    503 provided by the Android plugin for Gradle. In most cases, you only need to edit the build files
    504 at the module level. For example, the build file for the app module in the
    505 <code>BuildSystemExample</code> project looks like this:</p>
    506 
    507 <pre>
    508 apply plugin: 'android'
    509 
    510 android {
    511     compileSdkVersion 19
    512     buildToolsVersion "19.0.0"
    513 
    514     defaultConfig {
    515         minSdkVersion 8
    516         targetSdkVersion 19
    517         versionCode 1
    518         versionName "1.0"
    519     }
    520     buildTypes {
    521         release {
    522             runProguard true
    523             proguardFiles getDefaultProguardFile('proguard-android.txt'), \
    524             'proguard-rules.txt'
    525         }
    526     }
    527 }
    528 
    529 dependencies {
    530     compile project(":lib")
    531     compile 'com.android.support:appcompat-v7:19.0.1'
    532     compile fileTree(dir: 'libs', include: ['*.jar'])
    533 }
    534 </pre>
    535 
    536 <p><code>apply plugin: 'android'</code> applies the Android plugin for Gradle to this build.
    537 This adds Android-specific build tasks to the top-level build tasks and makes the
    538 <code>android {...}</code> element available to specify Android-specific build options.</p>
    539 
    540 <p><code>android {...}</code> configures all the Android-specific build options:</p>
    541 
    542 <ul>
    543     <li>The <code>compileSdkVersion</code> property specifies the compilation target.</li>
    544     <li><p>The <code>buildToolsVersion</code> property specifies what version of the build tools
    545         to use. To install several versions of the build tools, use the SDK Manager.</p>
    546         <p class="note"><strong>Note:</strong> Always use a build tools version whose major
    547         revision number is higher or equal to that of your compilation target and target SDK.</p>
    548     </li>
    549     <li><p>The <code>defaultConfig</code> element configures core settings and
    550         entries in the manifest file (<code>AndroidManifest.xml</code>) dynamically from the
    551         build system. The values in <code>defaultConfig</code> override those in the manifest
    552         file.</p>
    553         <p>The configuration specified in the <code>defaultConfig</code> element applies
    554         to all build variants, unless the configuration for a build variant overrides some
    555         of these values.</p>
    556     </li>
    557     <li>The <code>buildTypes</code> element controls how to build and package your app.
    558         By default, the build system defines two build types: <em>debug</em> and
    559         <em>release</em>. The debug build type includes debugging symbols and is signed with
    560         the debug key. The release build type is not signed by default.
    561         In this example the build file configures the release version to use
    562         ProGuard.</li>
    563 </ul>
    564 
    565 <p>The <code>dependencies</code> element is outside and after the <code>android</code> element.
    566 This element declares the dependencies for this module. Dependencies are covered in the following
    567 sections.</p>
    568 
    569 <p class="note"><strong>Note:</strong> When you make changes to the build files in your project,
    570 Android Studio requires a project sync to import the build configuration changes. Click
    571 <strong>Sync Now</strong> on the yellow notification bar that appears for Android Studio
    572 to import the changes.</p>
    573 
    574 <img src="{@docRoot}images/tools/as-gradlesync.png" alt="" />
    575 <p class="img-caption"><strong>Figure 4.</strong> Sync the project in Android Studio.</p>
    576 
    577 <h3 id="declareDeps">Declare dependencies</h3>
    578 
    579 <p>The <code>app</code> module in <code>BuildSystemExample</code> declares three
    580 dependencies:</p>
    581 
    582 <pre>
    583 ...
    584 dependencies {
    585     // Module dependency
    586     compile project(":lib")
    587 
    588     // Remote binary dependency
    589     compile 'com.android.support:appcompat-v7:19.0.1'
    590 
    591     // Local binary dependency
    592     compile fileTree(dir: 'libs', include: ['*.jar'])
    593 }
    594 </pre>
    595 
    596 <p>Each of these dependencies is described below. The build system adds all the
    597 <code>compile</code> dependencies to the compilation classpath and includes them in the final
    598 package.</p>
    599 
    600 <h4>Module dependencies</h4>
    601 
    602 <p>The <code>app</code> module depends on the <code>lib</code> module, because
    603 <code>MainActivity</code> launches <code>LibActivity1</code> as described in
    604 <a href="#openActFromLib">Open an Activity from a Library Module</a>.</p>
    605 
    606 <p><code>compile project(":lib")</code> declares a dependency on the <code>lib</code>
    607 module of <code>BuildSystemExample</code>. When you build the <code>app</code> module,
    608 the build system assembles and includes the <code>lib</code> module.</p>
    609 
    610 <h4>Remote binary dependencies</h4>
    611 
    612 <p>The <code>app</code> and <code>lib</code> modules both use the <code>ActionBarActivity</code>
    613 class from the Android Support Library, so these modules depend on it.</p>
    614 
    615 <p><code>compile 'com.android.support:appcompat-v7:19.0.1'</code> declares a dependency on
    616 version 19.0.1 of the Android Support Library by specifying its Maven coordinates. The Android Support
    617 Library is available in the <em>Android Repository</em> package of the Android SDK. If your
    618 SDK installation does not have this package, download and install it using the SDK Manager.</p>
    619 
    620 Android Studio configures
    621 projects to use the Maven Central Repository by default. (This configuration is included in the
    622 top-level build file for the project.)</p>
    623 
    624 <h4>Local binary dependencies</h4>
    625 
    626 <p>The modules in <code>BuildSystemExample</code> do not use any binary dependencies from the
    627 local file system. If you have modules that require local binary dependencies, copy the JAR
    628 files for these dependencies into <code>&lt;moduleName>/libs</code> inside your project.</p>
    629 
    630 <p><code>compile fileTree(dir: 'libs', include: ['*.jar'])</code> tells the build system that any
    631 JAR file inside <code>app/libs</code> is a dependency and should be included in the compilation
    632 classpath and in the final package.</p>
    633 
    634 <p>For more information about dependencies in Gradle, see
    635 <a href="http://www.gradle.org/docs/current/userguide/artifact_dependencies_tutorial.html">Dependency
    636 Management Basics</a> in the Gradle User Guide.</p>
    637 
    638 <h3 id="runProguard">Run ProGuard</h3>
    639 
    640 <p>The build system can run
    641 <a href="http://developer.android.com/tools/help/proguard.html">ProGuard</a> to obfuscate your
    642 classes during the build process. In <code>BuildSystemExample</code>, modify the build file for
    643 the app module to run ProGuard for the release build:</p>
    644 
    645 <pre>
    646 ...
    647 android {
    648     ...
    649     buildTypes {
    650         release {
    651             runProguard true
    652             proguardFiles getDefaultProguardFile('proguard-android.txt'), \
    653                           'proguard-rules.txt'
    654         }
    655     }
    656 }
    657 ...
    658 </pre>
    659 
    660 <p><code>getDefaultProguardFile('proguard-android.txt')</code> obtains the default ProGuard
    661 settings from the Android SDK installation. Android Studio adds the module-specific rules file
    662 <code>proguard-rules.txt</code> at the root of the module, where you can add custom ProGuard
    663 rules.</p>
    664 
    665 <h3 id="configureSigning">Configure signing settings</h3>
    666 
    667 <p>The debug and the release versions of the app differ on whether the application can be
    668 debugged on secure devices and on how the APK is signed. The build system signs the debug
    669 version with a default key and certificate using known credentials to avoid a password prompt at
    670 build time. The build system does not sign the release version unless you explicitly define a
    671 signing configuration for this build.</p>
    672 
    673 <p>To sign the release version of <code>BuildSystemExample</code>:</p>
    674 
    675 <ol>
    676     <li><p>Copy your release key to the root directory of the <code>app</code> module
    677         (<code>app/</code>).</p>
    678         <p>This ensures that the build system can find your key when you move the location of your
    679         project or when you build the project on a different machine. If you do not have a release
    680         key, you can generate one as described in
    681         <a href="{@docRoot}tools/publishing/app-signing.html">Signing your Applications</a>.</p>
    682     </li>
    683     <li><p>Add the signing configuration to the build file for the <code>app</code> module:</p>
    684         <p><pre>
    685 ...
    686 android {
    687     ...
    688     defaultConfig { ... }
    689     signingConfigs {
    690         release {
    691             storeFile file("myreleasekey.keystore")
    692             storePassword "password"
    693             keyAlias "MyReleaseKey"
    694             keyPassword "password"
    695         }
    696     }
    697     buildTypes {
    698         release {
    699             ...
    700             signingConfig signingConfigs.release
    701         }
    702     }
    703 }
    704 ...
    705 </pre></p>
    706     </li>
    707     <li>Invoke the <code>assembleRelease</code> build task from Android Studio or from the command
    708         line.</li>
    709 </ol>
    710 
    711 <p>The package in <code>app/build/apk/app-release.apk</code> is now signed with your release key.</p>
    712 
    713 <p class="note"><strong>Note:</strong> Including the passwords for your release key and keystore
    714 inside the build file is not a good security practice. Alternatively, you can configure the build
    715 file to obtain these passwords from environment variables or have the build process prompt you
    716 for these passwords.</p>
    717 
    718 <p>To obtain these passwords from environment variables:</p>
    719 
    720 <pre>
    721 storePassword System.getenv("KSTOREPWD")
    722 keyPassword System.getenv("KEYPWD")
    723 </pre>
    724 
    725 <p>To have the build process prompt you for these passwords if you are invoking the build from
    726 the command line:</p>
    727 
    728 <pre>
    729 storePassword System.console().readLine("\nKeystore password: ")
    730 keyPassword System.console().readLIne("\nKey password: ")
    731 </pre>
    732 
    733 <h3 id="workBuildVariants">Work with build variants</h3>
    734 
    735 <p>This section describes how the build system can help you create different versions of the same
    736 application from a single project. This is useful when you have a demo version and a paid version
    737 of your app, or if you want to distribute multiple APKs for different device configurations on
    738 Google Play.</p>
    739 
    740 <p>The build system uses <em>product flavors</em> to create different versions of your app. Each
    741 version of your app can have different features or device requirements. The build system generates
    742 a different APK for each version of your app.</p>
    743 
    744 <h4>Build variants</h4>
    745 
    746 <p>Each version of your app is represented in the build system by a <em>build variant</em>.
    747 Build variants are combinations of build types and product flavor configurations. Android Studio
    748 projects define two build types (<em>debug</em> and <em>release</em>) and no product flavors by
    749 default. These projects consists of two build variants, debug and release, and the build system
    750 generates an APK for each.</p>
    751 
    752 <p>The exercise in this section defines two product flavors, <em>demo</em> and <em>full</em>.
    753 This generates four build variants:</p>
    754 
    755 <ul>
    756     <li>demo-debug</li>
    757     <li>demo-release</li>
    758     <li>full-debug</li>
    759     <li>full-release</li>
    760 </ul>
    761 
    762 <p>In this case the build system creates four APKs, one for each of these build variants.</p>
    763 
    764 <p>Some projects have complex combinations of features along more than one dimension, but they
    765 still represent the same app. For example, in addition to having a demo and a full version of the
    766 app, some games may contain binaries specific to a particular CPU/ABI. The flexibility of
    767 the build system makes it possible to generate the following build variants for such a project:</p>
    768 
    769 <ul>
    770     <li>x86-demo-debug</li>
    771     <li>x86-demo-release</li>
    772     <li>x86-full-debug</li>
    773     <li>x86-full-release</li>
    774     <li>arm-demo-debug</li>
    775     <li>arm-demo-release</li>
    776     <li>arm-full-debug</li>
    777     <li>arm-full-release</li>
    778     <li>mips-demo-debug</li>
    779     <li>mips-demo-release</li>
    780     <li>mips-full-debug</li>
    781     <li>mips-full-release</li>
    782 </ul>
    783 
    784 <p>This project would consist of two build types (<em>debug</em> and <em>release</em>)
    785 and two <em>dimensions</em> of product flavors, one for app type (demo or full) and one for
    786 CPU/ABI (x86, ARM, or MIPS). For more information on flavor dimensions, see the
    787 <a href="http://tools.android.com/tech-docs/new-build-system/user-guide">Gradle Plugin User
    788 Guide</a>.</p>
    789 
    790 <h4>Source directories</h4>
    791 
    792 <p>To build each version of your app, the build system combines source code and
    793 resources from:</p>
    794 
    795 <ul>
    796     <li><code>src/main/</code> - the main source directory (common to all variants)</li>
    797     <li><code>src/&lt;buildType>/</code> - the build type source directory</li>
    798     <li><code>src/&lt;flavorName>/</code> - the flavor source directory</li>
    799 </ul>
    800 
    801 <p>The number of flavor source directories used in the build depends on the flavor configuration
    802 of your project:</p>
    803 <ul>
    804     <li><p>For projects that do not define any flavors, the build system does not use any
    805         flavor source directories. For example, to generate the <em>release</em> build variant
    806         in projects with no flavors, the build system uses:</p>
    807         <ul>
    808             <li><code>src/main/</code></li>
    809             <li><code>src/release/</code> (build type)</li>
    810         </ul>
    811     </li>
    812     <li><p>For projects that define a set of flavors, the build system uses one flavor source
    813         directory. For example, to generate the <em>full-debug</em> build variant in the example
    814         in this section, the build system uses:</p>
    815         <ul>
    816             <li><code>src/main/</code></li>
    817             <li><code>src/debug/</code> (build type)</li>
    818             <li><code>src/full/</code> (flavor)</li>
    819         </ul>
    820     </li>
    821     <li><p>For projects that use flavor dimensions, the build system uses one flavor source
    822         directory per dimension. For example, to generate the <em>arm-demo-release</em> build
    823         variant in the previous example, the build system uses:</p>
    824         <ul>
    825             <li><code>src/main/</code></li>
    826             <li><code>src/release/</code> (build type)</li>
    827             <li><code>src/demo/</code> (flavor - app type dimension)</li>
    828             <li><code>src/arm/</code> (flavor - ABI dimension)</li>
    829         </ul>
    830     </li>
    831 </ul>
    832 
    833 <p class="note"><strong>Note:</strong> The build type and flavor source directories are optional,
    834 and Android Studio does not create these directories for you. The build system does not use them
    835 if they are not present.</p>
    836 
    837 <p>The source code from these directories is used together to generate the output for a build
    838 variant. You can have classes with the same name in different directories as long as those
    839 directories are not used together in the same variant. The exercise in this section shows you
    840 how to create different versions of the same activity class in different variants.</p>
    841 
    842 <p>The build system merges all the manifests into a single manifest, so each build variant
    843 can define different components or permissions in the final manifest.</p>
    844 
    845 <p>The build system merges all the resources from the all the source directories. If different
    846 folders contain resources with the same name for a build variant, the priority order is the
    847 following: build type resources override those from the product flavor, which override the
    848 resources in the main source directory.</p>
    849 
    850 <p class="note"><strong>Note:</strong> Build variants enable you to reuse common activities,
    851 application logic, and resources across different versions of your app.</p>
    852 
    853 <h4>Product flavors in BuildSystemExample</h4>
    854 
    855 <p>To create different versions of your app:</p>
    856 
    857 <ol>
    858     <li>Define product flavors in the build file.</li>
    859     <li>Create additional source directories for each flavor.</li>
    860     <li>Add the flavor-specific sources to your project.</li>
    861 </ol>
    862 
    863 <p>The rest of this section walks you through these steps in detail using the
    864 <code>BuildSystemExample</code> project. You create two flavors of the
    865 <code>BuildSystemExample</code> app, a demo flavor and a full flavor. Both flavors share
    866 <code>MainActivity</code>, to which you add a new button to launch a new activity,
    867 <code>SecondActivity</code>. This new activity is different for each flavor, so you simulate a
    868 situation where the new activity would have more features in the full flavor than in the demo
    869 flavor. At the end of the exercise, you end up with two different APKs, one for each flavor.</p>
    870 
    871 <h4>Define product flavors in the build file</h4>
    872 
    873 <p>To define two product flavors, edit the build file for the app module to add the following
    874 configuration:</p>
    875 
    876 <pre>
    877 ...
    878 android {
    879     ...
    880     defaultConfig { ... }
    881     signingConfigs { ... }
    882     buildTypes { ... }
    883     productFlavors {
    884         demo {
    885             applicationId "com.buildsystemexample.app.demo"
    886             versionName "1.0-demo"
    887         }
    888         full {
    889             applicationId "com.buildsystemexample.app.full"
    890             versionName "1.0-full"
    891         }
    892     }
    893 }
    894 ...
    895 </pre>
    896 
    897 <p>The product flavor definitions support the same properties as the <code>defaultConfig</code>
    898 element. The base configuration for all flavors is specified in <code>defaultConfig</code>, and each
    899 flavor can override any value. The build file above uses the <code>applicationId</code> property
    900 to assign a different package name to each flavor: since each flavor definition creates a
    901 different app, they each need a distinct package name.</p>
    902 
    903 <p class="note"><strong>Note:</strong> To distribute your app using
    904 <a href="{@docRoot}google/play/publishing/multiple-apks.html">Multiple APK Support</a> in
    905 Google Play, assign the same package name to all variants and give each variant a different
    906 <code>versionCode</code>. To distribute different variants of your app as separate apps in Google
    907 Play, assign a different package name to each variant.</p>
    908 
    909 <h4>Add additional source directories for each flavor</h4>
    910 
    911 <p>Now you create source folders and add a <code>SecondActivity</code> to each flavor. To create
    912 the source directory structure for the demo flavor:</p>
    913 
    914 <ol>
    915     <li>On the <em>Project</em> panel, expand <strong>BuildSystemExample</strong>, and then expand
    916         the <strong>app</strong> directory.</li>
    917     <li>Right click the <strong>src</strong> directory under <em>app</em> and select
    918         <strong>New</strong> > <strong>Directory</strong>.</li>
    919     <li>Enter "demo" as the name of the new directory and click <strong>OK</strong>.</li>
    920     <li><p>Similarly, create the following directories:</p>
    921         <ul>
    922             <li><code>app/src/demo/java</code></li>
    923             <li><code>app/src/demo/res</code></li>
    924             <li><code>app/src/demo/res/layout</code></li>
    925             <li><code>app/src/demo/res/values</code></li>
    926         </ul>
    927     </li>
    928 </ol>
    929 
    930 <p>The resulting directory structure looks like figure 5.</p>
    931 
    932 <img src="{@docRoot}images/tools/as-demoflavordirs.png" alt="" />
    933 <p class="img-caption"><strong>Figure 5.</strong> New source directories for the demo flavor.</p>
    934 
    935 <h4>Add a new activity to each flavor</h4>
    936 
    937 <p>To add <code>SecondActivity</code> to the <code>demo</code> flavor:</p>
    938 
    939 <ol>
    940     <li>On the <em>Project</em> panel, right click on the <strong>app</strong> module and select
    941         <strong>New</strong> > <strong>Activity</strong>.</li>
    942     <li>Select <strong>Blank Activity</strong> and click <strong>Next</strong>.</li>
    943     <li>Enter "SecondActivity" as the activity name.</li>
    944     <li>Enter "com.buildsystemexample.app" as the package name and click
    945         <strong>Finish</strong>.</li>
    946     <li>Right click on the <strong>java</strong> directory under <em>app/src/demo</em> and select
    947         <strong>New</strong> > <strong>Package</strong>.</li>
    948     <li>Enter "com.buildsystemexample.app" as the package name and click <strong>OK</strong>.</li>
    949     <li>Drag <strong>SecondActivity</strong> and drop it under the new package in
    950         <em>app/src/demo/java</em>.</li>
    951     <li>Accept the default values and click <strong>Refactor</strong>.</li>
    952 </ol>
    953 
    954 <p>To add the layout for <code>SecondActivity</code> and a strings resource to the demo flavor:</p>
    955 
    956 <ol>
    957     <li>Drag <strong>activity_second.xml</strong> from <em>app/src/main/res/layout</em> and drop it
    958         inside <em>app/src/demo/res/layout</em>.</li>
    959     <li>Accept the default values on the window that appears and click <code>OK</code>.</li>
    960     <li>Copy <strong>strings.xml</strong> from <em>app/src/main/res</em> into
    961         <em>app/src/demo/res</em>.</li>
    962     <li><p>Replace the contents of the new copy of <code>strings.xml</code> with the
    963         following:</p>
    964         <p><pre>
    965 &lt;?xml version="1.0" encoding="utf-8"?>
    966 &lt;resources>
    967     &lt;string name="hello_world">Demo version only.&lt;/string>
    968 &lt;/resources>
    969 </pre></p>
    970     </li>
    971 </ol>
    972 
    973 <p>Now you add source folders and <code>SecondActivity</code> to the full flavor by making a copy
    974 of the <code>demo</code> flavor:</p>
    975 
    976 <ol>
    977     <li>On the <em>Project</em> panel, right click on the <strong>demo</strong> directory under
    978         <em>app/src</em> and select <strong>Copy</strong>.</li>
    979     <li>Right-click on the <strong>src/</strong> directory under <em>app/</em> and select
    980         <strong>Paste</strong>.</li>
    981     <li>On the window that appears, enter "full" as the new name and click <strong>OK</strong>.</li>
    982     <li><p>Replace the contents of <strong>strings.xml</strong> under <em>src/full/res/values</em>
    983         with the following:</p>
    984         <p><pre>
    985 &lt;?xml version="1.0" encoding="utf-8"?>
    986 &lt;resources>
    987     &lt;string name="hello_world">This is the full version!&lt;/string>
    988 &lt;/resources>
    989 </pre></p>
    990     </li>
    991 </ol>
    992 
    993 <p class="note"><strong>Note:</strong> From this point on, you could develop
    994 <code>SecondActivity</code> independently inside each
    995 flavor. You can add more features to this activity in the <code>full</code> flavor.</p>
    996 
    997 <p>To work on files from a particular flavor, click on <strong>Build Variants</strong> on the left
    998 of the IDE window and select the flavor you want to modify in the <em>Build Variants</em> panel,
    999 as shown in figure 5. Android Studio may show errors in source files from flavors other than the
   1000 one selected in the <em>Build Variants</em> panel, but this does not affect the outcome of the
   1001 build.</p>
   1002 
   1003 <img src="{@docRoot}images/tools/as-buildvariants.png" alt="" />
   1004 <p class="img-caption"><strong>Figure 6.</strong> The Build Variants panel.</p>
   1005 
   1006 <h4>Launch a flavor-specific activity from the main activity</h4>
   1007 
   1008 <p>Since the flavor-specific activity (<code>SecondActivity</code>) has the same package name and
   1009 activity name in both flavors, you can launch it from the main activity, which is common to all
   1010 flavors. To modify the main activity:</p>
   1011 
   1012 <ol>
   1013     <li><p>Edit <code>activity_main.xml</code> and add a new button to
   1014         <code>MainActivity</code>:</p>
   1015         <p><pre>
   1016 &lt;LinearLayout ...>
   1017     ...
   1018     &lt;Button
   1019         android:id="@+id/button2"
   1020         android:layout_width="wrap_content"
   1021         android:layout_height="wrap_content"
   1022         android:text="@string/button2"
   1023         android:onClick="onButton2Clicked"/>
   1024 &lt;/LinearLayout>
   1025 </pre></p>
   1026     </li>
   1027     <li>Click on the areas marked in red in the layout file and press <strong>Alt</strong>+
   1028         <strong>Enter</strong>. Follow the suggestions from Android Studio to add a new string
   1029         resource with value Open Second Activity and an <code>onButton2Clicked</code> method to
   1030         <code>MainActivity</code>.</li>
   1031     <li><p>Add the following code to the <code>onButton2Clicked</code> method of
   1032         <code>MainActivity</code>:</p>
   1033         <p><pre>
   1034 public void onButton2Clicked(View view) {
   1035     Intent intent = new Intent(this, SecondActivity.class);
   1036     startActivity(intent);
   1037 }
   1038 </pre></p>
   1039     </li>
   1040     <li><p>Edit the app's manifest to include a reference to <code>SecondActivity</code>:</p>
   1041         <p><pre>
   1042 &lt;manifest ...>
   1043     &lt;application ...>
   1044         ...
   1045         &lt;activity
   1046             android:name="com.buildsystemexample.app.SecondActivity"
   1047             android:label="@string/title_activity_second" >
   1048         &lt;/activity>
   1049     &lt;/application>
   1050 &lt;/manifest>
   1051 </pre></p>
   1052     </li>
   1053 </ol>
   1054 
   1055 <h4>Build output</h4>
   1056 
   1057 <p>The <code>BuildSystemExample</code> app is now complete. To build it, invoke the
   1058 <code>assemble</code> task from Android Studio or from the command line.</p>
   1059 
   1060 <p>The build generates an APK for each build variant:
   1061 the <code>app/build/apk/</code> directory contains packages named
   1062 <code>app-&lt;flavor>-&lt;buildtype>.apk</code>; for example, <code>app-full-release.apk</code> and
   1063 <code>app-demo-debug.apk</code>.</p>
   1064 
   1065 
   1066 <h2 id="reference">Reference</h2>
   1067 
   1068 <p>The build system is very flexible and has more features than those described here. For a
   1069 complete reference, see the
   1070 <a href="http://tools.android.com/tech-docs/new-build-system/user-guide">Android Plugin for Gradle
   1071 User Guide</a>.</p>
   1072