skip to Main Content

I am encountering an error to my flutter project. I wanted to add firebase service to my flutter project. However when I am following the steps from Firebase by adding the google-json file to my android/app directory and also adding the codes dependencies to both project level and app level build.gradle file. The error of imcompatible version of Kotlin prompted up.

The errors message are

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:compileDebugKotlin'.
> A failure occurred while executing org.jetbrains.kotlin.compilerRunner.GradleCompilerRunnerWithWorkers$GradleKotlinCompilerWorkAction
   > Compilation error. See log for more details

BUILD FAILED in 50s

┌─ Flutter Fix ──────────────────────────────────────────────────────────────────────────────┐
│ [!] Your project requires a newer version of the Kotlin Gradle plugin.                     │
│ Find the latest version on https://kotlinlang.org/docs/releases.html#release-details, then │
│ update C:UsersuserDocumentsGitHubprojectAppandroidbuild.gradle:  │
│ ext.kotlin_version = '<latest-version>'                                                    │
└────────────────────────────────────────────────────────────────────────────────────────────┘
Error: Gradle task assembleDebug failed with exit code 1

This is my android/build.gradle file

buildscript {
    ext.kotlin_version = '1.7.1'
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:7.4.2'
//        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}


plugins {
    id 'com.google.gms.google-services' version '4.4.1' apply false
}

allprojects {
    repositories {
        google()
        mavenCentral()
    }
}



rootProject.buildDir = '../build'
subprojects {
    project.buildDir = "${rootProject.buildDir}/${project.name}"
}
subprojects {
    project.evaluationDependsOn(':app')
}

tasks.register("clean", Delete) {
    delete rootProject.buildDir
}

This is the android/app/build.gradle file

plugins {
    id "com.android.application"
    id "kotlin-android"
    id "dev.flutter.flutter-gradle-plugin"
    id 'com.google.gms.google-services'
}

def localProperties = new Properties()
def localPropertiesFile = rootProject.file('local.properties')
if (localPropertiesFile.exists()) {
    localPropertiesFile.withReader('UTF-8') { reader ->
        localProperties.load(reader)
    }
}

def flutterVersionCode = localProperties.getProperty('flutter.versionCode')
if (flutterVersionCode == null) {
    flutterVersionCode = '1'
}

def flutterVersionName = localProperties.getProperty('flutter.versionName')
if (flutterVersionName == null) {
    flutterVersionName = '1.0'
}

android {
    namespace "com.example.rtwo"
    compileSdk flutter.compileSdkVersion
    ndkVersion flutter.ndkVersion

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    kotlinOptions {
        jvmTarget = '1.8'
    }

    sourceSets {
        main.java.srcDirs += 'src/main/kotlin'
    }

    defaultConfig {
        // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
        applicationId "com.example.rtwo"
        // You can update the following values to match your application needs.
        // For more information, see: https://docs.flutter.dev/deployment/android#reviewing-the-gradle-build-configuration.
        minSdkVersion flutter.minSdkVersion
        targetSdkVersion flutter.targetSdkVersion
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
    }

    buildTypes {
        release {
            // TODO: Add your own signing config for the release build.
            // Signing with the debug keys for now, so `flutter run --release` works.
            signingConfig signingConfigs.debug
        }
    }
}
dependencies {
    implementation platform('com.google.firebase:firebase-bom:32.8.1')
    implementation 'com.google.firebase:firebase-analytics'
}

flutter {
    source '../..'
}



//apply plugin: 'com.google.gms.google-services'

2

Answers


  1. Try to update Kotlin version in this line:

    ext.kotlin_version = '1.7.1'
    

    Metadata version seems to be 1.9.0. So you can replace that line with the latest Kotlin version number inside android/build.gradle:

    ext.kotlin_version = '1.9.23'
    

    Also make sure that you are not changing wrong line or anything else other than what Firebase documentation states.

    Update:
    After investigating carefully, seems like Kotlin version also defined inside Android/settings.gradle file like below:

    id "org.jetbrains.kotlin.android" version "1.7.10" apply false
    

    We need to update version number there to the latest Kotlin version which is 1.9.23 currently. So corrected line will be like this:

    id "org.jetbrains.kotlin.android" version "1.9.23" apply false
    

    If Kotlin number is updated here, no need to make any other changes inside buile.gradle files. This version number here overrides whatever defined inside build.gradle files.

    Login or Signup to reply.
  2. It seems like you’re encountering a version conflict between the Kotlin Gradle plugin and other dependencies in your Flutter project. To resolve this issue, you need to update the Kotlin Gradle plugin version to match the required version.

    Here are the steps to fix the issue:

    1. Find the latest version of the Kotlin Gradle plugin from the Kotlin Releases page..
    2. Update the ext.kotlin_version variable in your android/build.gradle file to match the latest version:
    buildscript {
        ext.kotlin_version = '<latest-version>' // Update this line with the latest version
        repositories {
            google()
            mavenCentral()
        }
        dependencies {
            classpath 'com.android.tools.build:gradle:7.4.2'
        }
    }
    

    Replace with the version you found in step 1.

    1. Sync your project with the updated Gradle settings by running the following command in the terminal at the root of your project:
    flutter pub get
    

    After following these steps, the version conflict should be resolved, and you should be able to build your Flutter project without encountering the Kotlin version error.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search