跳转到相应内容

最常访问

最近访问

navigation

Android Plugin for Gradle Release Notes

The Android Studio build system is based on Gradle, and the Android plugin for Gradle 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 Plugin for Gradle

When you update Android Studio, you may receive a prompt to automatically update the Android plugin for 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.

You can specify the Android plugin for Gradle 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 Android plugin for Gradle to version 3.1.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 plugin 3.0.0 or higher.
        google()
        ...
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.1.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 plugin for Gradle. For the best performance, you should use the latest possible version of both Gradle and the Android plugin.

Plugin versionRequired Gradle version
1.0.0 - 1.1.32.2.1 - 2.3
1.2.0 - 1.3.12.2.1 - 2.9
1.5.02.2.1 - 2.13
2.0.0 - 2.1.22.10 - 2.13
2.1.3 - 2.2.32.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.4 in the gradle-wrapper.properties file.

...
distributionUrl = https\://services.gradle.org/distributions/gradle-4.4-all.zip
...

3.1.0 (March 2018)

This version of the Android plugin requires the following:

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

If you want to build APKs that target these ABIs, you must use NDK r16b or lower and specify the ABIs in your build.gradle file, as shown below:

splits {
    abi {
        include 'armeabi', 'mips', 'mips64'
        ...
    }
}

Fixes

3.0.0 (October 2017)

Android plugin for Gradle 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 Plugin for Gradle 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:

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

New features

Behavior changes

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:
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 cleanBuildCache task 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.
Changes:

2.2.0 (September 2016)

Dependencies:
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=true to your gradle.properties file. While using the new packaging tool, the zipalignDebug task is not available. However, you can create one yourself by calling the createZipAlignTask(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.gradle file:
    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 using buildTypes.proguardFiles, which provides general ProGuard rules for your app and does not specify classes for the main DEX file.
  • Adds support for the android:extractNativeLibs flag, which can reduce the size of your app when you install it on a device. When you set this flag to false in the <application> element of your app manifest, Gradle packages uncompressed and aligned versions of your native libraries with your APK. This prevents PackageManager from 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 versionNameSuffix and applicationIdSuffix for product flavors. (Issue 59614)
Changes:
  • getDefaultProguardFile now 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 testCoverageEnabled to true.
    • Improved support for annotation processors. Annotation processors on your classpath, such as any compile dependencies, are automatically applied to your build. You can also specify an annotation processor in your build and pass arguments by using the javaCompileOptions.annotationProcessorOptions {} DSL in your module-level build.gradle file:
      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 annotationProcessor dependency 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 the jack.incremental parameter to true:
      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.properties file:
      # This sets the daemon heap size to 1.5GB.
      org.gradle.jvmargs=-Xmx1536M
      

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:
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.gradle file:
    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.properties file:

    org.gradle.jvmargs = -Xmx2048m
    

    If you have defined a value for javaMaxHeapSize in your module-level build.gradle file, you need to set org.gradle.jvmargs to the value of javaMaxHeapSize + 1024 MB. For example, if you have set javaMaxHeapSize to "2048m", you need to add the following to your project's gradle.properties file:

    org.gradle.jvmargs = -Xmx3072m
    

    To disable dexing-in-process, add the following code to your module-level build.gradle file:

    android {
      ...
      dexOptions {
          dexInProcess false
      }
    }
    

2.0.0 (April 2016)

Dependencies:
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 maxProcessCount to control how many slave dex processes can be spawned concurrently. The following code, in the module-level build.gradle file, 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.gradle file:
    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.txt file located in the same folder as the Proguard log files.
Changed behavior:
  • When minSdkVersion is 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.

Fixed issues:
  • Fixed an issue that caused duplicate AAR dependencies in both the test and main build configurations.

Older releases

此网站会使用 Cookie 来存储您在此网站上指定的语言和显示选项偏好设置。

获取最新的 Android Developers 资讯和提示,助您在 Google Play 上取得成功。

* 必填字段

成功!

在微信上关注 Google Developers

要以浏览此网站吗?

您请求访问的是网页,但是您为此网站设置的语言偏好为

要更改您的语言偏好设置并以浏览此网站吗?如果以后您想要更改语言偏好设置,请使用每个页面底部的语言菜单。

该类需要 或更高的 API 级别

此文档已被隐藏,因为您为该文档选择的 API 级别是 。您可以使用左侧导航栏上方的选择器来更改文档的 API 级别。

要详细了解如何根据您的应用需求指定 API 级别,请参阅支持不同平台版本

Take a short survey?
Help us improve the Android developer experience. (April 2018 — Developer Survey)