Question
How to Update the Kotlin Gradle Plugin in a Flutter Android Project
Question
I updated my Flutter project dependencies to support null safety, and now Android Studio says the project needs a newer version of the Kotlin Gradle plugin.
I tried changing this dependency:
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
to:
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.6.10"
but that did not solve the issue.
Here is my android/app/build.gradle file:
def localProperties = new Properties()
def localPropertiesFile = rootProject.file('local.properties')
if (localPropertiesFile.exists()) {
localPropertiesFile.withReader('UTF-8') { reader ->
localProperties.load(reader)
}
}
def flutterRoot = localProperties.getProperty('flutter.sdk')
if (flutterRoot == null) {
throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.")
}
def flutterVersionCode = localProperties.getProperty('flutter.versionCode')
if (flutterVersionCode == null) {
flutterVersionCode = '1'
}
def flutterVersionName = localProperties.getProperty('flutter.versionName')
if (flutterVersionName == null) {
flutterVersionName = '1.0'
}
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"
def keystoreProperties = new Properties()
def keystorePropertiesFile = rootProject.file('key.properties')
if (keystorePropertiesFile.exists()) {
keystoreProperties.load(new FileInputStream(keystorePropertiesFile))
}
android {
compileSdkVersion 31
sourceSets {
main.java.srcDirs += 'src/main/kotlin'
}
lintOptions {
disable 'InvalidPackage'
}
defaultConfig {
applicationId "*********"
minSdkVersion 30
targetSdkVersion 30
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
signingConfigs {
release {
keyAlias keystoreProperties['keyAlias']
keyPassword keystoreProperties['keyPassword']
storeFile keystoreProperties['storeFile'] ? file(keystoreProperties['storeFile']) : null
storePassword keystoreProperties['storePassword']
}
}
buildTypes {
release {
signingConfig signingConfigs.release
}
}
}
flutter {
source '../..'
}
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
implementation 'com.google.firebase:firebase-analytics:17.2.2'
}
apply plugin: 'com.google.gms.google-services'
The build output says:
[!] Your project requires a newer version of the Kotlin Gradle plugin.
Find the latest version on https://kotlinlang.org/docs/gradle.html#plugin-and-versions, then update project/android/build.gradle:
ext.kotlin_version = '<latest-version>'
Where exactly should the Kotlin Gradle plugin version be updated in a Flutter Android project, and why does changing kotlin-stdlib-jdk7 not fix it?
Short Answer
By the end of this page, you will understand the difference between the Kotlin Gradle plugin and the Kotlin standard library, where Flutter Android projects usually define the Kotlin plugin version, and how to update it safely when Gradle reports that a newer version is required.
Concept
The main idea here is that the Kotlin Gradle plugin version and the Kotlin runtime library version are related, but they are not the same thing.
In an Android or Flutter project, Gradle uses plugins to control how the project is built. The Kotlin Gradle plugin is one of those build tools. It tells Gradle how to compile Kotlin code, configure tasks, and integrate Kotlin into the Android build process.
The dependency below is not the plugin itself:
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
That line only adds the Kotlin standard library to your app at compile/runtime.
When the build error says:
update project/android/build.gradle:
ext.kotlin_version = '<latest-version>'
it means the version must usually be changed in the project-level Gradle file, not the app-level file.
In older Flutter project structures, this is typically in:
android/build.gradle
and it often looks like this:
buildscript {
ext.kotlin_version = '1.5.31'
repositories {
google()
mavenCentral()
}
dependencies {
classpath "com.android.tools.build:gradle:..."
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
That ext.kotlin_version value is what controls the Kotlin Gradle plugin version.
Why this matters:
- Flutter plugins may require newer Kotlin tooling.
Mental Model
Think of your Android build like a kitchen:
- The Kotlin Gradle plugin is the chef who knows how to prepare Kotlin code.
- The Kotlin standard library is the ingredient set the app uses.
If the chef is too old and does not know the new recipe, buying fresher ingredients will not solve the problem.
Changing kotlin-stdlib-jdk7 is like replacing ingredients. But the error is telling you the chef itself needs an upgrade. That is why you must update the Kotlin Gradle plugin version in the project-level Gradle configuration.
Syntax and Examples
In a typical older Flutter Android project, the Kotlin Gradle plugin version is set in android/build.gradle.
Common project-level setup
buildscript {
ext.kotlin_version = '1.6.10'
repositories {
google()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:7.1.2'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
allprojects {
repositories {
google()
mavenCentral()
}
}
App-level usage
Then the app module may use the same variable:
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
}
What to change
If Gradle says the Kotlin plugin is too old, update this line:
ext.kotlin_version = '1.6.10'
not just this line:
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
Why the app-level dependency is not enough
This dependency only affects the libraries available to your application code. It does not replace the build plugin Gradle uses to compile Kotlin and configure the Android build.
Step by Step Execution
Consider this simplified setup.
android/build.gradle
buildscript {
ext.kotlin_version = '1.4.32'
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
android/app/build.gradle
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
}
What happens during the build
-
Gradle starts reading the project-level build files.
-
It loads the Kotlin Gradle plugin from:
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" -
That plugin controls Kotlin compilation tasks.
-
Later, Gradle reads the app module dependencies.
-
The app gets
kotlin-stdlib-jdk7as a normal library dependency.
If you only change the stdlib version
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.6.10"
then this happens:
- Your app dependency changes.
- But Gradle still uses the old Kotlin plugin from the project-level classpath.
Real World Use Cases
This concept appears often in real Android and Flutter development.
1. Upgrading Flutter plugins
A package such as Firebase, camera, or other Android-backed plugins may require a newer Kotlin Gradle plugin.
2. Updating Android Studio or Gradle
Newer Android tooling may warn that your project uses outdated plugin versions.
3. Migrating older Flutter projects
A project created months or years ago may still use old Gradle and Kotlin versions. Modern dependencies can expose that mismatch.
4. Team projects with mixed environments
One developer upgrades dependencies and another developer suddenly gets build errors because project-level plugin versions are pinned too low.
5. CI/CD build failures
A project may build locally on one machine but fail in continuous integration if the checked-in Gradle configuration uses incompatible plugin versions.
Real Codebase Usage
In real projects, developers usually manage build versions centrally rather than hardcoding them in random places.
Common pattern: central version variable
buildscript {
ext.kotlin_version = '1.7.10'
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
Benefits:
- easier upgrades
- one source of truth
- fewer version mismatches
Common pattern: keep plugin and tooling compatible
Developers often update these together when needed:
- Kotlin Gradle plugin
- Android Gradle plugin
- Gradle wrapper
- compile SDK version
Guarding against version drift
Teams usually avoid changing only one dependency without checking build tool compatibility. For example:
- package upgrade requires newer Kotlin plugin
- newer Kotlin plugin may require newer Gradle
- newer Android Gradle plugin may require a newer Java version
Practical workflow
A common workflow is:
- read the exact error
- update the project-level Kotlin plugin version
- sync Gradle
- run
flutter clean - rebuild with
flutter pub getandflutter run
Why this matters in codebases
Build configuration is part of the codebase. If it is outdated, even correct application code may fail to compile.
Common Mistakes
1. Editing the wrong build.gradle
A Flutter Android project usually has at least two Gradle files:
android/build.gradle→ project-levelandroid/app/build.gradle→ app-level
The Kotlin Gradle plugin version is commonly set in the project-level file.
Wrong place
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.6.10"
}
Better fix
buildscript {
ext.kotlin_version = '1.6.10'
}
2. Confusing plugin version with library version
These are different:
org.jetbrains.kotlin:kotlin-gradle-plugin→ build pluginorg.jetbrains.kotlin:kotlin-stdlib-jdk7→ app library
Changing one does not automatically change the other.
3. Forgetting to sync and clean
After changing Gradle files, old cached state can remain.
Useful commands:
flutter clean
flutter pub get
flutter run
4. Updating Kotlin without checking Gradle compatibility
Comparisons
| Item | What it is | Where it is usually defined | Purpose |
|---|---|---|---|
kotlin-gradle-plugin | Build plugin | android/build.gradle | Tells Gradle how to build Kotlin code |
kotlin-stdlib-jdk7 | Runtime/compile library | android/app/build.gradle | Provides Kotlin standard library classes to the app |
ext.kotlin_version | Shared version variable | android/build.gradle | Often used to control plugin and related Kotlin dependencies |
Project-level vs app-level Gradle files
| File |
|---|
Cheat Sheet
Quick reference
Typical fix location
android/build.gradle
What to update
buildscript {
ext.kotlin_version = '1.6.10'
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
Do not confuse these
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
- build plugin
- affects Gradle build process
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
- app dependency
- affects app code, not Gradle plugin loading
Usual Flutter cleanup steps
flutter clean
flutter pub get
flutter run
Common file locations
android/build.gradle→ project-level settingsandroid/app/build.gradle→ app module settings
Rule of thumb
If the error mentions Kotlin Gradle plugin, update the project-level plugin version, not only the Kotlin stdlib dependency.
FAQ
Where is ext.kotlin_version in a Flutter project?
Usually in android/build.gradle, inside the buildscript block of older Flutter project templates.
Why did changing kotlin-stdlib-jdk7 not solve the build error?
Because that changes the app library dependency, not the Kotlin Gradle plugin that Gradle uses during the build.
Which Gradle file should I edit in Flutter: android/build.gradle or android/app/build.gradle?
For the Kotlin Gradle plugin version, usually android/build.gradle.
What if ext.kotlin_version is missing?
Look for org.jetbrains.kotlin:kotlin-gradle-plugin in the project-level Gradle files. In newer project structures, plugin versions may be defined differently, such as in a plugins block or settings file.
Do I need to update Android Gradle Plugin too?
Sometimes yes. Kotlin, Android Gradle Plugin, and Gradle wrapper versions must be compatible.
Should I hardcode the stdlib version directly?
Usually it is better to keep versions centralized to avoid mismatches, unless you have a specific reason to pin it separately.
Is this caused by Flutter null safety itself?
Not directly. The issue usually comes from package upgrades that require newer Android/Kotlin build tooling.
Mini Project
Description
You have inherited an older Flutter app that builds for iOS but fails on Android with a Kotlin Gradle plugin version error. The project still uses the old Flutter Android Gradle structure. Your task is to locate the correct file, update the build plugin version, and confirm that the app-level dependency is not the main fix.
Goal
Update the Flutter Android project so Gradle uses a newer Kotlin plugin version from the correct configuration file.
Requirements
- Find the project-level Gradle file for the Android part of the Flutter app.
- Define or update
ext.kotlin_versionto a newer version. - Ensure the Kotlin Gradle plugin classpath uses that version.
- Keep the app-level Kotlin stdlib dependency valid.
- Run a clean rebuild using Flutter commands.
Keep learning
Related questions
Accessing Kotlin Extension Functions from Java
Learn how Kotlin extension functions are compiled and how to call them correctly from Java with clear examples and common pitfalls.
Allow HTTP and HTTPS in Android 9 Pie with Network Security Configuration
Learn how Android 9 Pie handles cleartext HTTP traffic and how to allow HTTP and HTTPS safely using network security config.
Android AlarmManager Example: Scheduling Tasks with AlarmManager
Learn how to use Android AlarmManager to schedule tasks, set alarms, and handle broadcasts with a simple beginner example.