Question

How to Update the Kotlin Gradle Plugin in a Flutter Android Project

kotlinflutterandroid-studiogradle-kotlin-dsl

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.
  • New Android Gradle Plugin versions may expect newer Kotlin versions.
  • Null-safety migrations can indirectly cause package upgrades that raise the minimum required Kotlin plugin version.

So the fix is usually to update the Kotlin plugin version at the project level, then sync and rebuild.

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.

Take Quiz

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

  1. Gradle starts reading the project-level build files.

  2. It loads the Kotlin Gradle plugin from:

    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    
  3. That plugin controls Kotlin compilation tasks.

  4. Later, Gradle reads the app module dependencies.

  5. The app gets kotlin-stdlib-jdk7 as 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.
  • The build still fails if the plugin is too old.

Correct fix

Update the plugin version source:

buildscript {
    ext.kotlin_version = '1.6.10'
    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

Now Gradle loads the newer Kotlin build plugin, and the project can build if the rest of the tooling is compatible.

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.

Take Quiz

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:

  1. read the exact error
  2. update the project-level Kotlin plugin version
  3. sync Gradle
  4. run flutter clean
  5. rebuild with flutter pub get and flutter 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-level
  • android/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 plugin
  • org.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

If you update Kotlin too far without updating Android Gradle Plugin or Gradle wrapper, new errors may appear.

5. Typos in version variables

For example:

ext.kotlin_version = '1.6.10'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotln_version"

$kotln_version is misspelled, so Gradle will fail.

6. Assuming Android Studio warnings always point to the open file

The error may mention project/android/build.gradle, but many beginners only inspect android/app/build.gradle. Always verify which Gradle file the error refers to.

Comparisons

ItemWhat it isWhere it is usually definedPurpose
kotlin-gradle-pluginBuild pluginandroid/build.gradleTells Gradle how to build Kotlin code
kotlin-stdlib-jdk7Runtime/compile libraryandroid/app/build.gradleProvides Kotlin standard library classes to the app
ext.kotlin_versionShared version variableandroid/build.gradleOften used to control plugin and related Kotlin dependencies

Project-level vs app-level Gradle files

FileScopeTypical contents
android/build.gradleWhole Android projectplugin versions, repositories, buildscript classpath
android/app/build.gradleApp module onlyapp config, dependencies, applicationId, SDK versions

Changing the stdlib vs changing the plugin

ChangeWhat it affectsWill it fix this error?
Update kotlin-stdlib-jdk7App dependencyUsually no
Update ext.kotlin_version and plugin classpathBuild toolingYes, if compatibility is correct
Take Quiz

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 settings
  • android/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.

Take Quiz

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

  1. Find the project-level Gradle file for the Android part of the Flutter app.
  2. Define or update ext.kotlin_version to a newer version.
  3. Ensure the Kotlin Gradle plugin classpath uses that version.
  4. Keep the app-level Kotlin stdlib dependency valid.
  5. Run a clean rebuild using Flutter commands.
Take Quiz

Keep learning

Related questions

View more