The Android Studio build system is based on Gradle, and the Android Gradle plugin adds several features that are specific to building Android apps. Although the Android plugin is typically updated in lock-step with Android Studio, the plugin (and the rest of the Gradle system) can run independent of Android Studio and be updated separately.
This page explains how to keep your Gradle tools up to date and what's in the recent updates.
For details about how to configure your Android builds with Gradle, see the following pages:
For more information about the Gradle build system, see the Gradle user guide.
Update the Android Gradle plugin
When you update Android Studio, you may receive a prompt to automatically update the Android Gradle plugin to the latest available version. You can choose to accept the update or manually specify a version based on your project's build requirements.
You can specify the plugin version in
either the File > Project
Structure > Project menu in Android Studio, or
the top-level build.gradle file. The plugin version applies to
all modules built in that Android Studio project. The following example sets
the plugin to version 3.2.0 from the
build.gradle file:
buildscript {
repositories {
// Gradle 4.1 and higher include support for Google's Maven repo using
// the google() method. And you need to include this repo to download
// Android Gradle plugin 3.0.0 or higher.
google()
...
}
dependencies {
classpath 'com.android.tools.build:gradle:3.2.0'
}
}
Caution: You should not use dynamic dependencies in version
numbers, such as 'com.android.tools.build:gradle:2.+'. Using
this feature can cause unexpected version updates and difficulty resolving
version differences.
If the specified plugin version has not been downloaded, Gradle downloads it the next time you build your project or click Tools > Android > Sync Project with Gradle Files from the Android Studio menu bar.
Update Gradle
When you update Android Studio, you may receive a prompt to also update Gradle to the latest available version. You can choose to accept the update or manually specify a version based on your project's build requirements.
The following table lists which version of Gradle is required for each version of the Android Gradle plugin. For the best performance, you should use the latest possible version of both Gradle and the plugin.
| Plugin version | Required Gradle version |
|---|---|
| 1.0.0 - 1.1.3 | 2.2.1 - 2.3 |
| 1.2.0 - 1.3.1 | 2.2.1 - 2.9 |
| 1.5.0 | 2.2.1 - 2.13 |
| 2.0.0 - 2.1.2 | 2.10 - 2.13 |
| 2.1.3 - 2.2.3 | 2.14.1+ |
| 2.3.0+ | 3.3+ |
| 3.0.0+ | 4.1+ |
| 3.1.0+ | 4.4+ |
You can specify the Gradle version in either the File >
Project Structure > Project menu in
Android Studio, or by editing the Gradle distribution reference in the
gradle/wrapper/gradle-wrapper.properties file. The following
example sets the Gradle version to 4.6 in the
gradle-wrapper.properties file.
... distributionUrl = https\://services.gradle.org/distributions/gradle-4.6-all.zip ...
3.2.0 (September 2018)
This version of the Android plugin requires the following:
- Gradle 4.6 or higher. To learn more, read the section about updating Gradle.
- SDK Build Tools 28.0.3 or higher.
New features
Support for building Android App Bundles: The app bundle is a new upload format that includes all your app’s compiled code and resources while deferring APK generation and signing to the Google Play Store. You no longer have to build, sign, and manage multiple APKs, and users get smaller downloads that are optimized for their device. To learn more, read About Android App Bundles.
Support for improved incremental build speeds when using annotation processors: The
AnnotationProcessorOptionsDSL now extendsCommandLineArgumentProvider, which enables either you or the annotation processor author to annotate arguments for the processor using incremental build property type annotations. Using these annotations improves the correctness and performance of incremental and cached clean builds. To learn more, read Pass arguments to annotation processors.Migration tool for AndroidX: When using Android Gradle plugin 3.2.0 with Android 3.2 and higher, you can migrate your project’s local and Maven dependencies to use the new AndroidX libraries by selecting Refactor > Migrate to AndroidX from the menu bar. Using this migration tool also sets the following flags to
truein yourgradle.propertiesfile:android.useAndroidX: When set totrue, the Android plugin uses the appropriate AndroidX library instead of a Support Library. When this flag is not specified, the plugin sets it tofalseby default.android.enableJetifier: When set totrue, the Android plugin automatically migrates existing third-party libraries to use AndroidX by rewriting their binaries. When this flag is not specified, the plugin sets it tofalseby default. You can set this flag totrueonly whileandroid.useAndroidXis also set totrue, otherwise you get a build error.
To learn more, read the AndroidX overview.
New code shrinker, R8: R8 is a new tool for code shrinking and obfuscation that replaces ProGuard. You can start using the preview version of R8 by including the following in your project’s
gradle.propertiesfile:android.enableR8 = true
Behavior changes
- Desugaring with D8 is now enabled by default.
AAPT2 is now on Google's Maven repo. To use AAPT2, make sure that you have the
google()dependency in yourbuild.gradlefile, as shown below:buildscript { repositories { google() // here jcenter() } dependencies { classpath 'com.android.tools.build:gradle:3.2.0' } } allprojects { repositories { google() // and here jcenter() }Native multidex is now enabled by default. Previous versions of Android Studio enabled native multidex when deploying the debug version of an app to a device running Android API level 21 or higher. Now, whether you’re deploying to a device or building an APK for release, the Android Gradle plugin enables native multidex for all modules that set
minSdkVersion=21or higher.The plugin now enforces a minimum version of the protobuf plugin (0.8.6), Kotlin plugin (1.2.50), and Crashlytics plugin (1.25.4).
The feature module plugin,
com.android.feature, now enforces the use of only letters, digits, and underscores when specifying a module name. For example, if your feature module name includes dashes, you get a build error. This behavior matches that of the dynamic feature module plugin.
Bug fixes
- JavaCompile is now cacheable in projects with data binding. (Issue #69243050)
- Better compile avoidance for library modules with data binding. (Issue #77539932)
- You can now re-enable configure-on-demand if you've disable it in earlier versions due to some unpredictable build errors. (Issue #77910727)
3.1.0 (March 2018)
This version of the Android plugin requires the following:
- Gradle 4.4 or higher. To learn more, read the section about updating Gradle.
- Build Tools 27.0.3 or higher.
Keep in mind, you no longer need to specify a version for the build
tools using the
android.buildToolsVersionproperty—the plugin uses the minimum required version by default.
New DEX compiler, D8
By default, Android Studio now uses a new DEX
compiler called D8. DEX compilation is the process of transforming .class
bytecode into .dex bytecode for the Android Runtime (or Dalvik, for older
versions of Android). Compared to the previous compiler, called DX, D8
compiles faster and outputs smaller DEX files, all while having the same or
better app runtime performance.
D8 shouldn't change your day-to-day app development workflow. However, if you
experience any issues related to the new compiler, please
report a bug. You can temporarily disable D8 and
use DX by including the following in your project's gradle.properties file:
android.enableD8=false
For projects that
use Java 8 language features,
incremental desugaring is enabled by default. You can disable it by
specifying the following in your project's gradle.properties file:
android.enableIncrementalDesugaring=false.
Preview users: If you're already using a preview version of D8, note that it now compiles against libraries included in the SDK build tools—not the JDK. So, if you are accessing APIs that exist in the JDK but not in the SDK build tools libraries, you get a compile error.
Behavior changes
When building multiple APKs that each target a different ABI, the plugin no longer generates APKs for the following ABIs by default:
mips,mips64, andarmeabi.If you want to build APKs that target these ABIs, you must use NDK r16b or lower and specify the ABIs in your
build.gradlefile, as shown below:splits { abi { include 'armeabi', 'mips', 'mips64' ... } }When building configuration APKs for an Android Instant App, language configuration splits are now grouped by the root language by default. For example, if your app includes resources for
zh-TWorzh-CNlocales, Gradle will package those resources in azhlanguage configuration split. You can override this behavior by defining your own groups using theincludeproperty, as shown below:splits { language { enable true // Each string defines a group of locales that // Gradle should package together. include "in,id", "iw,he", "fil,tl,tgl", "yue,zh,zh-TW,zh-CN" } }The Android plugin's build cache now evicts cache entries that are older than 30 days.
Passing
"auto"toresConfigno longer automatically picks string resources to package into your APK. If you continue to use"auto", the plugin packages all string resources your app and its dependencies provide. So, you should instead specify each locale that you want the plugin to package into your APK.Because local modules can't depend on your app's test APK, adding dependencies to your instrumented tests using the
androidTestApiconfiguration, instead ofandroidTestImplementation, causes Gradle to issue the following warning:WARNING: Configuration 'androidTestApi' is obsolete and has been replaced with 'androidTestImplementation'
Fixes
- Fixes an issue where Android Studio doesn't properly recognize dependencies in composite builds.
- Fixes an issue where you get a project sync error when loading the Android plugin multiple times in a single build–for example, when multiple subprojects each include the Android plugin in their buildscript classpath.
3.0.0 (October 2017)
Android Gradle plugin 3.0.0 includes a variety of changes that aim to address performance issues of large projects.
For example, on a sample skeleton project with ~130 modules and a large number of external dependencies (but no code or resources), you can experience performance improvements similar to the following:
| Android plugin version + Gradle version | Android plugin 2.2.0 + Gradle 2.14.1 | Android plugin 2.3.0 + Gradle 3.3 | Android plugin 3.0.0 + Gradle 4.1 |
|---|---|---|---|
Configuration (e.g. running ./gradlew --help) |
~2 mins | ~9 s | ~2.5 s |
| 1-line Java change (implementation change) | ~2 mins 15 s | ~29 s | ~6.4 s |
Some of these changes break existing builds. So, you should consider the effort of migrating your project before using the new plugin. To learn more, read Migrate to Android Gradle Plugin 3.0.0.
If you don't experience the performance improvements described above, please file a bug and include a trace of your build using the Gradle Profiler.
This version of the Android plugin requires the following:
- Gradle 4.1 or higher. To learn more, read the section about updating Gradle.
- Build Tools 26.0.2 or higher.
With this update, you no longer need to specify a version for the build
tools—the plugin uses the minimum required version by default.
So, you can now remove the
android.buildToolsVersionproperty.
3.0.1 (November 2017)
This is a minor update to support Android Studio 3.0.1, and includes general bug fixes and performance improvements.
Optimizations
- Better parallelism for multi-module projects through a fine grained task graph.
- When making changes to dependency, Gradle performs faster builds by not
re-compiling modules that do not have access to that dependency's API.
You should restrict which dependencies leak their APIs to other modules by
using Gradle's new dependency configurations:
implementation,api,compileOnly, andruntimeOnly. - Faster incremental build speed due to per-class dexing. Each class is now
compiled into separate DEX files, and only the classes that are
modified are re-dexed. You should also expect improved build speeds for
apps that set
minSdkVersionto 20 or lower, and use legacy multi-dex. - Improved build speeds by optimizing certain tasks to use chached outputs. To benefit from this optimization, you need to first enable the Gradle build cache.
- Improved incremental resource processing using AAPT2, which is now enabled by
default.
If you are experiencing issues while using AAPT2, please
report a bug. You can also disable AAPT2 by
setting
android.enableAapt2=falsein yourgradle.propertiesfile and restarting the Gradle daemon by running./gradlew --stopfrom the command line.
New features
- Variant-aware dependency management. When building a certain variant of a module, the plugin now automatically matches variants of local library module dependencies to the variant of the module you are building.
- Includes a new Feature module plugin to support Android Instant Apps and the Android Instant Apps SDK (which you can download using the SDK manager). To learn more about creating Feature modules with the new plugin, read Structure of an instant app with multiple features.
- Built-in support for using certain Java 8 language features and Java 8 libraries. Jack is now deprecated and no longer required, and you should first disable Jack to use the improved Java 8 support built into the default toolchain. For more information, read Use Java 8 language features.
- Added support for running tests with
Android Test Orchestrator,
which allows you to run each of your app's tests within
its own invocation of
Instrumentation. Because each test runs in its ownInstrumentationinstance, any shared state between tests doesn't accumulate on your device's CPU or memory. And, even if one test crashes, it takes down only its own instance ofInstrumentation, so your other tests still run.- Added
testOptions.executionto determine whether to use on-device test orchestration. If you want to use Android Test Orchestrator, you need to specifyANDROID_TEST_ORCHESTRATOR, as shown below. By default, this property is set toHOST, which disables on-device orchestration and is the standard method of running tests.android { testOptions { execution 'ANDROID_TEST_ORCHESTRATOR' } } - New
androidTestUtildependency configuration allows you to install another test helper APK before running your instrumentation tests, such as Android Test Orchestrator:dependencies { androidTestUtil 'com.android.support.test:orchestrator:1.0.0' ... }
- Added
- Added
testOptions.unitTests.includeAndroidResourcesto support unit tests that require Android resources, such as Roboelectric. When you set this property totrue, the plugin performs resource, asset, and manifest merging before running your unit tests. Your tests can then inspectcom/android/tools/test_config.propertieson the classpath for the following keys:android_merged_assets: the absolute path to the merged assets directory.Note: For library modules, the merged assets do not contain the assets of dependencies (see issue #65550419).
android_merged_manifest: the absolute path to the merged manifest file.android_merged_resources: the absolute path to the merged resources directory, which contains all the resources from the module and all its dependencies.android_custom_package: the package name of the final R class. If you dynamically modify the application ID, this package name may not match thepackageattribute in the app's manifest.
- Support for fonts as resources (which is a new feature introduced in Android 8.0 (API level 26)).
- Support for language-specific APKs with Android Instant Apps SDK 1.1 and higher. For more information, see Configure your build for pure splits.
- You can now change the output directory for your external native build
project, as shown below:
android { ... externalNativeBuild { // For ndk-build, instead use the ndkBuild block. cmake { ... // Specifies a relative path for outputs from external native // builds. You can specify any path that's not a subdirectory // of your project's temporary build/ directory. buildStagingDirectory "./outputs/cmake" } } } - You can now use CMake 3.7 or higher when building native projects from Android Studio.
- New
lintChecksdependency configuration allows you to build a JAR that defines custom lint rules, and package it into your AAR and APK projects. Your custom lint rules must belong to a separate project that outputs a single JAR and includes onlycompileOnlydependencies. Other app and library modules can then depend on your lint project using thelintChecksconfiguration:dependencies { // This tells the Gradle plugin to build ':lint-checks' into a lint.jar file // and package it with your module. If the module is an Android library, // other projects that depend on it automatically use the lint checks. // If the module is an app, lint includes these rules when analyzing the app. lintChecks project(':lint-checks') }
Behavior changes
- Android plugin 3.0.0 removes certain APIs, and your build will break if you
use them. For example, you can no longer use the Variants API to access
outputFile()objects or useprocessManifest.manifestOutputFile()to get the manifest file for each variant. To learn more, read API changes in the migration guide. - You no longer need to specify a version for the build tools (so, you can now
remove the
android.buildToolsVersionproperty). By default, the plugin automatically uses the minimum required build tools version for the version of Android plugin you're using. - You now enable PNG crunching in the
buildTypesblock, as shown below. PNG crunching is enabled by default for all builds except debug builds because it increases build times for projects that include many PNG files. So, to improve build times for other build types, you should either disable PNG crunching or convert your images to WebP.android { buildTypes { release { // Disables PNG crunching for the release build type. crunchPngs false } } } - The Android plugin now automatically builds executable targets that you configure in your external CMake projects.
- You must now add annotation processors to the processor classpath
using the
annotationProcessordependency configuration. - Using the deprecated
ndkCompileis now more restricted. You should instead migrate to using either CMake or ndk-build to compile native code that you want to package into your APK. To learn more, read Migrate from ndkcompile.
To learn more about issues that are still being addressed, see the Known issues.
2.3.0 (February 2017)
2.3.3 (June 2017)
This is a minor update that adds compatibility with Android Studio 2.3.3.
2.3.2 (May 2017)
This is a minor update that adds compatibility with Android Studio 2.3.2.
2.3.1 (April 2017)
This is a minor update to Android plugin 2.3.0 that fixes an issue where some physical Android devices did not work properly with Instant Run (see Issue #235879).
- Dependencies:
-
- Gradle 3.3 or higher.
- Build Tools 25.0.0 or higher.
- New:
-
- Uses Gradle 3.3, which includes performance improvements and new features. For more details, see the Gradle release notes.
-
Build cache: stores certain outputs that the Android plugin generates
when building your project (such as unpackaged AARs and pre-dexed remote
dependencies). Your clean builds are much faster while using the cache because
the build system can simply reuse those cached files during subsequent builds,
instead of recreating them. Projects using Android plugin 2.3.0 and higher use
the build cache by default. To learn more, read Improve Build Speed with Build Cache.
-
Includes a
cleanBuildCachetask that clears the build cache. - If you are using the experimental version of build cache (included in earlier versions of the plugin), you should update your plugin to the latest version.
-
Includes a
- Changes:
-
- Supports changes to Instant Run included in Android Studio 2.3.
- Configuration times for very large projects should be significantly faster.
- Fixed issues with auto-downloading for the constraint layout library.
- Plugin now uses ProGuard version 5.3.2.
- Includes many fixes for reported bugs. Please continue to file bug reports when you encounter issues.
2.2.0 (September 2016)
- Dependencies:
-
- Gradle 2.14.1 or higher.
- Build Tools 23.0.2 or higher.
- New:
-
- Uses Gradle 2.14.1, which includes performance improvements and new features, and fixes a security vulnerability that allows local privilege escalation when using the Gradle daemon. For more details, see the Gradle release notes.
- Using the
externalNativeBuild {}DSL, Gradle now lets you link to your native sources and compile native libraries using CMake or ndk-build. After building your native libraries, Gradle packages them into your APK. To learn more about using CMake and ndk-build with Gradle, read Add C and C++ Code to Your Project. - When you run a build from the command line, Gradle now attempts to auto-download any missing SDK components or updates that your project depends on. To learn more, read Auto-download missing packages with Gradle.
- A new experimental caching feature lets Gradle speed up build times by pre-dexing, storing, and reusing the pre-dexed versions of your libraries. To learn more about using this experimental feature, read the Build Cache guide.
- Improves build performance by adopting a new default packaging
pipeline which handles zipping, signing, and zipaligning in one task. You
can revert to using the older packaging tools by adding
android.useOldPackaging=trueto yourgradle.propertiesfile. While using the new packaging tool, thezipalignDebugtask is not available. However, you can create one yourself by calling thecreateZipAlignTask(String taskName, File inputFile, File outputFile)method. - APK signing now uses APK Signature Scheme
v2 in addition to traditional JAR signing. All Android platforms accept the
resulting APKs. Any modification to these APKs after signing invalidates their
v2 signatures and prevents installation on a device. To disable this feature,
add the following to your module-level
build.gradlefile:android { ... signingConfigs { config { ... v2SigningEnabled false } } } - For multidex builds, you can now use ProGuard rules to determine which
classes Gradle should compile into your app’s main DEX file. Because
the Android system loads the main DEX file first when starting your app, you
can prioritize certain classes at startup by compiling them into the main DEX
file. After you create a ProGuard configuration file specifically for your
main DEX file, pass the configuration file’s path to Gradle using
buildTypes.multiDexKeepProguard. Using this DSL is different from usingbuildTypes.proguardFiles, which provides general ProGuard rules for your app and does not specify classes for the main DEX file. - Adds support for the
android:extractNativeLibsflag, which can reduce the size of your app when you install it on a device. When you set this flag tofalsein the<application>element of your app manifest, Gradle packages uncompressed and aligned versions of your native libraries with your APK. This preventsPackageManagerfrom copying out your native libraries from the APK to the device's file system during installation and has the added benefit of making delta updates of your app smaller. - You can now specify
versionNameSuffixandapplicationIdSuffixfor product flavors. (Issue 59614)
- Changes:
-
-
getDefaultProguardFilenow returns the default ProGuard files that Android plugin for Gradle provides and no longer uses the ones in the Android SDK. - Improved Jack compiler performance and features:
- Jack now supports Jacoco test coverage when setting
testCoverageEnabledtotrue. - Improved support for annotation processors. Annotation
processors on your classpath, such as any
compiledependencies, are automatically applied to your build. You can also specify an annotation processor in your build and pass arguments by using thejavaCompileOptions.annotationProcessorOptions {}DSL in your module-levelbuild.gradlefile:android { ... defaultConfig { ... javaCompileOptions { annotationProcessorOptions { className 'com.example.MyProcessor' // Arguments are optional. arguments = [ foo : 'bar' ] } } } }If you want to apply an annotation processor at compile time but not include it in your APK, use the
annotationProcessordependency scope:dependencies { compile 'com.google.dagger:dagger:2.0' annotationProcessor 'com.google.dagger:dagger-compiler:2.0' // or use buildVariantAnnotationProcessor to target a specific build variant } - You can set additional flags for Jack using
jackOptions.additionalParameters(). The following code snippet sets thejack.incrementalparameter totrue:android { defaultConfig { ... jackOptions { enabled true additionalParameters("jack.incremental" : true) } } }For a list of parameters you can set, run the following from the command line:
java -jar /build-tools/jack.jar --help-properties
- By default, if the Gradle daemon's heap size is at least 1.5
GB, Jack now runs in the same process as Gradle. To adjust the
daemon heap size, add the following to your
gradle.propertiesfile:# This sets the daemon heap size to 1.5GB. org.gradle.jvmargs=-Xmx1536M
- Jack now supports Jacoco test coverage when setting
-
2.1.0 (April 2016)
2.1.3 (August 2016)
This update requires Gradle 2.14.1 and higher. Gradle 2.14.1 includes performance improvements, new features, and an important security fix. For more details, see the Gradle release notes.
- Dependencies:
-
- Gradle 2.10 or higher.
- Build Tools 23.0.2 or higher.
- New:
-
- Added support for the N Developer Preview, JDK 8, and Java 8 language features using the Jack
toolchain. To find out more, read the N Preview guide.
Note: Instant Run does not currently work with Jack and will be disabled while using the new toolchain. You only need to use Jack if you are developing for the N Preview and want to use the supported Java 8 language features.
- Added default support for incremental Java compilation to reduce
compilation time during development. It does this by only recompiling
portions of the source that have changed or need to be recompiled. To disable
this feature, add the following code to your module-level
build.gradlefile:android { ... compileOptions { incremental false } } -
Added support for dexing-in-process which performs dexing within the build process rather than in a separate, external VM processes. This not only makes incremental builds faster, but also speeds up full builds. The feature is enabled by default for projects that have set the Gradle daemon's maximum heap size to at least 2048 MB. You can do this by including the following in your project's
gradle.propertiesfile:org.gradle.jvmargs = -Xmx2048m
If you have defined a value for
javaMaxHeapSizein your module-levelbuild.gradlefile, you need to setorg.gradle.jvmargsto the value ofjavaMaxHeapSize+ 1024 MB. For example, if you have setjavaMaxHeapSizeto "2048m", you need to add the following to your project'sgradle.propertiesfile:org.gradle.jvmargs = -Xmx3072m
To disable dexing-in-process, add the following code to your module-level
build.gradlefile:android { ... dexOptions { dexInProcess false } }
- Added support for the N Developer Preview, JDK 8, and Java 8 language features using the Jack
toolchain. To find out more, read the N Preview guide.
2.0.0 (April 2016)
- Dependencies:
-
- Gradle 2.10 or higher.
- Build Tools 21.1.1 or higher.
- New:
-
- Enables Instant Run by supporting bytecode injection, and pushing code and resource updates to a running app on the emulator or a physical device.
- Added support for incremental builds, even when the app isn’t running. Full build times are improved by pushing incremental changes through the Android Debug Bridge to the connected device.
- Added
maxProcessCountto control how many slave dex processes can be spawned concurrently. The following code, in the module-levelbuild.gradlefile, sets the maximum number of concurrent processes to 4:android { ... dexOptions { maxProcessCount = 4 // this is the default value } } - Added an experimental code shrinker to support pre-dexing and reduce re-dexing
of dependencies, which are not supported with Proguard. This improves the build
speed of your debug build variant. Because the experimental shrinker does not
support optimization and obfuscation, you should enable Proguard for your
release builds. To enable the experimental shrinker for your debug builds, add
the following to your module-level
build.gradlefile:android { ... buildTypes { debug { minifyEnabled true useProguard false } release { minifyEnabled true useProguard true // this is a default setting } } } - Added logging support and improved performance for the resource shrinker.
The resource shrinker now logs all of its operations into a
resources.txtfile located in the same folder as the Proguard log files.
- Changed behavior:
-
- When
minSdkVersionis set to 18 or higher, APK signing uses SHA256. - DSA and ECDSA keys can now sign APK packages.
Note: The Android keystore provider no longer supports DSA keys on Android 6.0 (API level 23) and higher.
- When
- Fixed issues:
-
- Fixed an issue that caused duplicate AAR dependencies in both the test and main build configurations.