Home | History | Annotate | Download | only in building
      1 page.title=Configuring Gradle Builds
      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="#buildFileBasics">Build Configuration Basics</a>
     10     <ol>
     11       <li><a href="#buildFileBasics">Declare dependencies</a></li>
     12       <li><a href="#buildFileBasics">Run ProGuard</a></li>
     13       <li><a href="#configureSigning">Configure signing settings</a></li>
     14     </ol>
     15   </li>
     16 
     17 
     18   <li><a href="#workBuildVariants">Work with build variants</a></li>
     19 </ol>
     20 
     21 
     22 <h2>See also</h2>
     23 <ul>
     24 <li><a href="{@docRoot}tools/building/plugin-for-gradle.html">
     25 Android Plugin for Gradle</a></li>
     26 </ul>
     27 </div>
     28 </div>
     29 
     30 
     31 <p>This section builds on the
     32 <a href="{@docRoot}sdk/installing/studio-build.html">Build System Overview</a> and
     33 <a href="{@docRoot}tools/building/building-studio.html">Build and Running from Android Studio</a>
     34 to show you how to use build variants based on product flavors and build types.</p>
     35 
     36 
     37 <h2 id="buildFileBasics">Build Configuration Basics</h2>
     38 
     39 <p>Android Studio projects contain a top-level build file and a build file for each module. The
     40 build files are called <code>build.gradle</code>, and they are plain text files that use
     41 <a href="http://groovy.codehaus.org">Groovy</a> syntax to configure the build with the elements
     42 provided by the Android plugin for Gradle. In most cases, you only need to edit the build files
     43 at the module level. For example, the build file for the app module in the
     44 <code>BuildSystemExample</code> project looks like this:</p>
     45 
     46 <pre>
     47 apply plugin: 'com.android.application'
     48 
     49 android {
     50     compileSdkVersion 19
     51     buildToolsVersion "19.0.0"
     52 
     53     defaultConfig {
     54         minSdkVersion 8
     55         targetSdkVersion 19
     56         versionCode 1
     57         versionName "1.0"
     58     }
     59     buildTypes {
     60         release {
     61             minifyEnabled true
     62             proguardFiles getDefaultProguardFile('proguard-android.txt'), \
     63             'proguard-rules.txt'
     64         }
     65     }
     66 }
     67 
     68 dependencies {
     69     compile project(":lib")
     70     compile 'com.android.support:appcompat-v7:19.0.1'
     71     compile fileTree(dir: 'libs', include: ['*.jar'])
     72 }
     73 </pre>
     74 
     75 <p><code>apply plugin: 'com.android.application'</code> applies the Android plugin for Gradle to this build.
     76 This adds Android-specific build tasks to the top-level build tasks and makes the
     77 <code>android {...}</code> element available to specify Android-specific build options.</p>
     78 
     79 <p><code>android {...}</code> configures all the Android-specific build options:</p>
     80 
     81 <ul>
     82     <li>The <code>compileSdkVersion</code> property specifies the compilation target.</li>
     83     <li><p>The <code>buildToolsVersion</code> property specifies what version of the build tools
     84         to use. To install several versions of the build tools, use the SDK Manager.</p>
     85         <p class="note"><strong>Note:</strong> Always use a build tools version whose major
     86         revision number is higher or equal to that of your compilation target and target SDK.</p>
     87     </li>
     88     <li><p>The <code>defaultConfig</code> element configures core settings and
     89         entries in the manifest file (<code>AndroidManifest.xml</code>) dynamically from the
     90         build system. The values in <code>defaultConfig</code> override those in the manifest
     91         file.</p>
     92         <p>The configuration specified in the <code>defaultConfig</code> element applies
     93         to all build variants, unless the configuration for a build variant overrides some
     94         of these values.</p>
     95     </li>
     96     <li>The <code>buildTypes</code> element controls how to build and package your app.
     97         By default, the build system defines two build types: <em>debug</em> and
     98         <em>release</em>. The debug build type includes debugging symbols and is signed with
     99         the debug key. The release build type is not signed by default.
    100         In this example the build file configures the release version to use
    101         ProGuard.</li>
    102 </ul>
    103 
    104 <p>The <code>dependencies</code> element is outside and after the <code>android</code> element.
    105 This element declares the dependencies for this module. Dependencies are covered in the following
    106 sections.</p>
    107 
    108 <p class="note"><strong>Note:</strong> When you make changes to the build files in your project,
    109 Android Studio requires a project sync to import the build configuration changes. Click
    110 <strong>Sync Now</strong> on the yellow notification bar that appears for Android Studio
    111 to import the changes.</p>
    112 
    113 <img src="{@docRoot}images/tools/as-gradlesync.png" alt="" />
    114 <p class="img-caption"><strong>Figure 1.</strong> Sync the project in Android Studio.</p>
    115 
    116 <h3 id="declareDeps">Declare dependencies</h3>
    117 
    118 <p>The <code>app</code> module in this example declares three
    119 dependencies:</p>
    120 
    121 <pre>
    122 ...
    123 dependencies {
    124     // Module dependency
    125     compile project(":lib")
    126 
    127     // Remote binary dependency
    128     compile 'com.android.support:appcompat-v7:19.0.1'
    129 
    130     // Local binary dependency
    131     compile fileTree(dir: 'libs', include: ['*.jar'])
    132 }
    133 </pre>
    134 
    135 <p>Each of these dependencies is described below. The build system adds all the
    136 <code>compile</code> dependencies to the compilation classpath and includes them in the final
    137 package.</p>
    138 
    139 <h4>Module dependencies</h4>
    140 
    141 <p>The <code>app</code> module depends on the <code>lib</code> module, because
    142 <code>MainActivity</code> launches <code>LibActivity1</code> as described in
    143 <a href="#openActFromLib">Open an Activity from a Library Module</a>.</p>
    144 
    145 <p><code>compile project(":lib")</code> declares a dependency on the <code>lib</code>
    146 module of <code>BuildSystemExample</code>. When you build the <code>app</code> module,
    147 the build system assembles and includes the <code>lib</code> module.</p>
    148 
    149 <h4>Remote binary dependencies</h4>
    150 
    151 <p>The <code>app</code> and <code>lib</code> modules both use the <code>ActionBarActivity</code>
    152 class from the Android Support Library, so these modules depend on it.</p>
    153 
    154 <p><code>compile 'com.android.support:appcompat-v7:19.0.1'</code> declares a dependency on
    155 version 19.0.1 of the Android Support Library by specifying its Maven coordinates. The Android Support
    156 Library is available in the <em>Android Repository</em> package of the Android SDK. If your
    157 SDK installation does not have this package, download and install it using the SDK Manager.</p>
    158 
    159 Android Studio configures projects to use the Maven Central Repository by default. (This
    160 configuration is included in the top-level build file for the project.)</p>
    161 
    162 <h4>Local binary dependencies</h4>
    163 
    164 <p>Some modules do not use any binary dependencies from the
    165 local file system. If you have modules that require local binary dependencies, copy the JAR
    166 files for these dependencies into <code>&lt;moduleName>/libs</code> inside your project.</p>
    167 
    168 <p><code>compile fileTree(dir: 'libs', include: ['*.jar'])</code> tells the build system that any
    169 JAR file inside <code>app/libs</code> is a dependency and should be included in the compilation
    170 classpath and in the final package.</p>
    171 
    172 <p>For more information about dependencies in Gradle, see
    173 <a href="http://www.gradle.org/docs/current/userguide/artifact_dependencies_tutorial.html">Dependency
    174 Management Basics</a> in the Gradle User Guide.</p>
    175 
    176 <h3 id="runProguard">Run ProGuard</h3>
    177 
    178 <p>The build system can run
    179 <a href="http://developer.android.com/tools/help/proguard.html">ProGuard</a> to obfuscate your
    180 classes during the build process. In <code>BuildSystemExample</code>, modify the build file for
    181 the app module to run ProGuard for the release build:</p>
    182 
    183 <pre>
    184 ...
    185 android {
    186     ...
    187     buildTypes {
    188         release {
    189             minifyEnabled true
    190             proguardFiles getDefaultProguardFile('proguard-android.txt'), \
    191                           'proguard-rules.txt'
    192         }
    193     }
    194 }
    195 ...
    196 </pre>
    197 
    198 <p><code>getDefaultProguardFile('proguard-android.txt')</code> obtains the default ProGuard
    199 settings from the Android SDK installation. Android Studio adds the module-specific rules file
    200 <code>proguard-rules.txt</code> at the root of the module, where you can add custom ProGuard
    201 rules.</p>
    202 
    203 <h3 id="configureSigning">Configure signing settings</h3>
    204 
    205 <p>The debug and the release versions of the app differ on whether the application can be
    206 debugged on secure devices and on how the APK is signed. The build system signs the debug
    207 version with a default key and certificate using known credentials to avoid a password prompt at
    208 build time. The build system does not sign the release version unless you explicitly define a
    209 signing configuration for this build. If you do not have a release key, you can generate one as
    210 described in <a href="{@docRoot}tools/publishing/app-signing.html">Signing your Applications</a>.</p>
    211 
    212 
    213 <h2 id="workBuildVariants">Work with build variants</h2>
    214 
    215 <p>This section describes how the build system can help you create different versions of the same
    216 application from a single project. This is useful when you have a demo version and a paid version
    217 of your app, or if you want to distribute multiple APKs for different device configurations on
    218 Google Play.</p>
    219 
    220 <p>The build system uses <em>product flavors</em> to create different product versions of your app.
    221 Each product version of your app can have different features or device requirements. The build
    222 system also uses build types to apply different build and packaging settings to each product version.
    223 Each product flavor and build type combination forms a build variant. The build system generates a
    224 different APK for each build variant of your app. </p>
    225 
    226 <h3>Build variants</h3>
    227 
    228 <p>This example project consists of the two default build types (<em>debug</em> and <em>release</em>)
    229 and two product flavors for app type (demo and full). For more information on advanced uses of
    230 build variants, see
    231 <a href="{@docRoot}sdk/installing/studio-build.html"> Build System Overview</a> .</p>
    232 
    233 
    234 <h4>Product flavors </h4>
    235 
    236 <p>To create different product versions of your app:</p>
    237 
    238 <ol>
    239     <li>Define product flavors in the build file.</li>
    240     <li>Create additional source directories for each flavor.</li>
    241     <li>Add the flavor-specific sources to your project.</li>
    242 </ol>
    243 
    244 <p>The rest of this section walks you through these steps in detail using a
    245 <code>BuildSystemExample</code> project. You create two flavors of the
    246 <code>BuildSystemExample</code> app, a demo flavor and a full flavor. Both flavors share
    247 <code>MainActivity</code>, to which you add a new button to launch a new activity,
    248 <code>SecondActivity</code>. This new activity is different for each flavor, so you simulate a
    249 situation where the new activity would have more features in the full flavor than in the demo
    250 flavor. At the end of the exercise, you end up with two different APKs, one for each flavor.</p>
    251 
    252 <h3>Define product flavors in the build file</h3>
    253 
    254 <p>To define two product flavors, edit the build file for the app module to add the following
    255 configuration:</p>
    256 
    257 <pre>
    258 ...
    259 android {
    260     ...
    261     defaultConfig { ... }
    262     signingConfigs { ... }
    263     buildTypes { ... }
    264     productFlavors {
    265         demo {
    266             applicationId "com.buildsystemexample.app.demo"
    267             versionName "1.0-demo"
    268         }
    269         full {
    270             applicationId "com.buildsystemexample.app.full"
    271             versionName "1.0-full"
    272         }
    273     }
    274 }
    275 ...
    276 </pre>
    277 
    278 <p>The product flavor definitions support the same properties as the <code>defaultConfig</code>
    279 element. The base configuration for all flavors is specified in <code>defaultConfig</code>, and each
    280 flavor overrides any default values. The build file above uses the <code>applicationId</code>
    281 property to assign a different package name to each flavor: since each flavor definition creates a
    282 different app, they each need a distinct package name.</p>
    283 
    284 <p class="note"><strong>Note:</strong> To distribute your app using
    285 <a href="{@docRoot}google/play/publishing/multiple-apks.html">Multiple APK Support</a> in
    286 Google Play, assign the same package name to all variants and give each variant a different
    287 <code>versionCode</code>. To distribute different variants of your app as separate apps in Google
    288 Play, assign a different package name to each variant.</p>
    289 
    290 <h4>Add additional source directories for each flavor</h4>
    291 
    292 <p>Now you create source folders and add a <code>SecondActivity</code> to each flavor. To create
    293 the source directory structure for the demo flavor:</p>
    294 
    295 <ol>
    296     <li>On the <em>Project</em> panel, expand <strong>BuildSystemExample</strong>, and then expand
    297         the <strong>app</strong> directory.</li>
    298     <li>Right-click the <strong>src</strong> directory under <em>app</em> and select
    299         <strong>New</strong> > <strong>Directory</strong>.</li>
    300     <li>Enter "demo" as the name of the new directory and click <strong>OK</strong>.</li>
    301     <li><p>Similarly, create the following directories:</p>
    302         <ul>
    303             <li><code>app/src/demo/java</code></li>
    304             <li><code>app/src/demo/res</code></li>
    305             <li><code>app/src/demo/res/layout</code></li>
    306             <li><code>app/src/demo/res/values</code></li>
    307         </ul>
    308     </li>
    309 </ol>
    310 
    311 <p>The resulting directory structure looks like figure 1.</p>
    312 
    313 <img src="{@docRoot}images/tools/as-demoflavordirs.png" alt="" />
    314 <p class="img-caption"><strong>Figure 1.</strong> New source directories for the demo flavor.</p>
    315 
    316 <h4>Add a new activity to each flavor</h4>
    317 
    318 <p>To add <code>SecondActivity</code> to the <code>demo</code> flavor:</p>
    319 
    320 <ol>
    321     <li>On the <em>Project</em> panel, right click on the <strong>app</strong> module and select
    322         <strong>New</strong> > <strong>Activity</strong>.</li>
    323     <li>Select <strong>Blank Activity</strong> and click <strong>Next</strong>.</li>
    324     <li>Enter "SecondActivity" as the activity name.</li>
    325     <li>Enter "com.buildsystemexample.app" as the package name and click
    326         <strong>Finish</strong>.</li>
    327     <li>Right click on the <strong>java</strong> directory under <em>app/src/demo</em> and select
    328         <strong>New</strong> > <strong>Package</strong>.</li>
    329     <li>Enter "com.buildsystemexample.app" as the package name and click <strong>OK</strong>.</li>
    330     <li>Drag <strong>SecondActivity</strong> and drop it under the new package in
    331         <em>app/src/demo/java</em>.</li>
    332     <li>Accept the default values and click <strong>Refactor</strong>.</li>
    333 </ol>
    334 
    335 <p>To add the layout for <code>SecondActivity</code> and a strings resource to the demo flavor:</p>
    336 
    337 <ol>
    338     <li>Drag <strong>activity_second.xml</strong> from <em>app/src/main/res/layout</em> and drop it
    339         inside <em>app/src/demo/res/layout</em>.</li>
    340     <li>Accept the default values on the window that appears and click <code>OK</code>.</li>
    341     <li>Copy <strong>strings.xml</strong> from <em>app/src/main/res</em> into
    342         <em>app/src/demo/res</em>.</li>
    343     <li><p>Replace the contents of the new copy of <code>strings.xml</code> with the
    344         following:</p>
    345         <p><pre>
    346 &lt;?xml version="1.0" encoding="utf-8"?>
    347 &lt;resources>
    348     &lt;string name="hello_world">Demo version only.&lt;/string>
    349 &lt;/resources>
    350 </pre></p>
    351     </li>
    352 </ol>
    353 
    354 <p>Now you add source folders and <code>SecondActivity</code> to the full flavor by making a copy
    355 of the <code>demo</code> flavor:</p>
    356 
    357 <ol>
    358     <li>On the <em>Project</em> panel, right click on the <strong>demo</strong> directory under
    359         <em>app/src</em> and select <strong>Copy</strong>.</li>
    360     <li>Right-click on the <strong>src/</strong> directory under <em>app/</em> and select
    361         <strong>Paste</strong>.</li>
    362     <li>On the window that appears, enter "full" as the new name and click <strong>OK</strong>.</li>
    363     <li><p>Replace the contents of <strong>strings.xml</strong> under <em>src/full/res/values</em>
    364         with the following:</p>
    365         <p><pre>
    366 &lt;?xml version="1.0" encoding="utf-8"?>
    367 &lt;resources>
    368     &lt;string name="hello_world">This is the full version!&lt;/string>
    369 &lt;/resources>
    370 </pre></p>
    371     </li>
    372 </ol>
    373 
    374 <p class="note"><strong>Note:</strong> From this point on, you could develop
    375 <code>SecondActivity</code> independently inside each
    376 flavor. For example, you could add more features to this activity in the <code>full</code> flavor.</p>
    377 
    378 <p>To work on files from a particular flavor, click on <strong>Build Variants</strong> on the left
    379 of the IDE window and select the flavor you want to modify in the <em>Build Variants</em> panel,
    380 as shown in figure 2. Android Studio may show errors in source files from flavors other than the
    381 one selected in the <em>Build Variants</em> panel, but this does not affect the outcome of the
    382 build.</p>
    383 
    384 <img src="{@docRoot}images/tools/as-buildvariants.png" alt="" />
    385 <p class="img-caption"><strong>Figure 2.</strong> The Build Variants panel.</p>
    386 
    387 <h4>Launch a flavor-specific activity from the main activity</h4>
    388 
    389 <p>Since the flavor-specific activity (<code>SecondActivity</code>) has the same package name and
    390 activity name in both flavors, you can launch it from the main activity, which is common to all
    391 flavors. To modify the main activity:</p>
    392 
    393 <ol>
    394     <li><p>Edit <code>activity_main.xml</code> and add a new button to
    395         <code>MainActivity</code>:</p>
    396         <p><pre>
    397 &lt;LinearLayout ...>
    398     ...
    399     &lt;Button
    400         android:id="@+id/button2"
    401         android:layout_width="wrap_content"
    402         android:layout_height="wrap_content"
    403         android:text="@string/button2"
    404         android:onClick="onButton2Clicked"/>
    405 &lt;/LinearLayout>
    406 </pre></p>
    407     </li>
    408     <li>Click on the areas marked in red in the layout file and press <strong>Alt</strong>+
    409         <strong>Enter</strong>. Follow the suggestions from Android Studio to add a new string
    410         resource with value Open Second Activity and an <code>onButton2Clicked</code> method to
    411         <code>MainActivity</code>.</li>
    412     <li><p>Add the following code to the <code>onButton2Clicked</code> method of
    413         <code>MainActivity</code>:</p>
    414         <p><pre>
    415 public void onButton2Clicked(View view) {
    416     Intent intent = new Intent(this, SecondActivity.class);
    417     startActivity(intent);
    418 }
    419 </pre></p>
    420     </li>
    421     <li><p>Edit the app's manifest to include a reference to <code>SecondActivity</code>:</p>
    422         <p><pre>
    423 &lt;manifest ...>
    424     &lt;application ...>
    425         ...
    426         &lt;activity
    427             android:name="com.buildsystemexample.app.SecondActivity"
    428             android:label="@string/title_activity_second" >
    429         &lt;/activity>
    430     &lt;/application>
    431 &lt;/manifest>
    432 </pre></p>
    433     </li>
    434 </ol>
    435 
    436 
    437 <h4>Build types </h4>
    438 <p>Build types represent the build packaging versions generated for each app package. By default,
    439 the debug and release build types are provided.
    440 </p>
    441 
    442 <pre>
    443 ...
    444 android {
    445     ...
    446     defaultConfig { ... }
    447     signingConfigs { ... }
    448     buildTypes { ... }
    449     productFlavors {...}
    450     buildTypes {
    451         release {
    452             minifyEnabled false
    453             proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    454         }
    455          debug {
    456             debuggable true
    457         }
    458     }
    459 }
    460 ...
    461 </pre>
    462 
    463 <p class="note"><strong>Note:</strong> Although only the <em>release</em> build type appears in
    464 the default <strong>build.gradle</strong> file, both the release and debug build types are
    465 applied to each build. </p>
    466 
    467 <p>In this example, the product flavors and build types create the following build variants:
    468 <ul>
    469 <li>demoDebug</li>
    470 <li>demoRelease</li>
    471 <li>fullDebug</li>
    472 <li>fullRelease</li>
    473 </ul>
    474 
    475 <p>To build this example, invoke the <code>assemble</code> task from Android Studio or from the
    476 command line.</p>
    477 
    478 <p>Separate output folders are created for each build variant. </p>
    479