Keywords: Android | Gradle | Kotlin
Abstract: This article delves into the common Gradle build error "The Android Gradle plugin supports only Kotlin Gradle plugin version 1.3.0 and higher" in Android development. By analyzing a real-world project case, it uncovers the root cause related to the deprecated kotlin-stdlib-jre7 dependency and its conflict with Kotlin Gradle plugin versions. The core solution involves updating the dependency to kotlin-stdlib-jdk8 in the module-level build.gradle and ensuring proper definition of the ext.kotlin_version variable in the project-level build.gradle. Additional configuration tips, such as updating Android Gradle plugin versions and handling React Native project naming conventions, are provided to offer a comprehensive troubleshooting guide for developers.
Problem Background and Error Analysis
In Android development with Kotlin, Gradle build errors frequently occur, especially when project dependencies on Kotlin versions are incompatible with the Android Gradle plugin. The typical error message discussed here is: ERROR: The Android Gradle plugin supports only Kotlin Gradle plugin version 1.3.0 and higher. The following dependencies do not satisfy the required version: root project 'ui-testing' -> org.jetbrains.kotlin:kotlin-gradle-plugin:1.2.21. This indicates that the Kotlin Gradle plugin version used in the project is below 1.3.0, while the Android Gradle plugin requires at least version 1.3.0.
Core Issue Diagnosis
By examining the provided project code, key issues in the module-level build.gradle file are identified:
implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
Here, kotlin-stdlib-jre7 is used, which is a deprecated dependency. More critically, when the $kotlin_version variable is not properly defined, Gradle may fall back to an older default version (e.g., 1.2.21), leading to version conflicts with the Android Gradle plugin.
Primary Solution
Based on the best answer (Answer 2), the most effective resolution is:
- Update Standard Library Dependency: Replace
kotlin-stdlib-jre7withkotlin-stdlib-jdk8. This is because thejre7version corresponds to the Java 7 standard library, while modern Android development typically requires Java 8 compatibility. Thejdk8version offers better support for Java 8 APIs and is compatible with newer Kotlin plugin versions. - Properly Define Kotlin Version Variable: In the
buildscriptblock of the project-levelbuild.gradle, ensure that theext.kotlin_versionvariable is defined and set to 1.3.0 or higher (e.g., 1.3.10, 1.3.21).
The modified dependency should appear as follows:
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
Supplementary Configuration Tips
Other answers provide valuable additional insights:
- Project-Level Configuration (Answer 1, 3, 5): In the project-level
build.gradle, thebuildscriptblock should include theext.kotlin_versiondefinition and corresponding classpath dependency. For example:classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version". Also, ensure the Android Gradle plugin version (e.g., 3.2.1, 3.4.0) is compatible with the Kotlin version. - React Native Project Special Handling (Answer 4): For React Native projects,
ext.kotlinVersion(camelCase) might be required instead ofext.kotlin_version(snake_case), as some dependencies may referencerootProject.ext.kotlinVersion. - Tool Updates (Answer 3): Update the Kotlin plugin version via Android Studio's SDK Manager to align the local environment with project configurations.
Implementation Steps and Code Examples
Below are the complete resolution steps:
- Modify Project-Level build.gradle:
buildscript {
ext.kotlin_version = '1.3.21' // Use a compatible version
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.4.0' // Ensure version compatibility
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
<ol start="2">
kotlin-gradle-plugin implementation dependencies (this plugin should only be a classpath dependency at the project level).dependencies {
// Other dependencies...
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
// Note: Remove implementation 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.21'
// because kotlin-gradle-plugin is a build tool plugin and should not be a module dependency
}
<ol start="3">
Deep Dive into Version Compatibility
Version compatibility between the Android Gradle plugin and Kotlin Gradle plugin is crucial. For instance, Android Gradle plugin 3.2.0+ typically requires Kotlin plugin 1.3.0+. Developers should regularly consult official documentation to ensure recommended version combinations are used. Moreover, the deprecation of kotlin-stdlib-jre7 reflects Kotlin's evolution towards modern Java versions (e.g., JDK 8). Migrating to kotlin-stdlib-jdk8 not only resolves compatibility issues but also leverages new language features.
Conclusion
The core solution to the "The Android Gradle plugin supports only Kotlin Gradle plugin version 1.3.0 and higher" error lies in updating the Kotlin standard library dependency and properly defining the version variable. By replacing kotlin-stdlib-jre7 with kotlin-stdlib-jdk8 and setting ext.kotlin_version in the project-level configuration, build system compatibility can be ensured. Combined with other best practices, such as using compatible Android Gradle plugin versions and handling framework-specific conventions, developers can effectively avoid such build errors and enhance development efficiency.