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