sirushti
sirushti

Reputation: 157

Unknown Kotlin JVM Target: 21 - Flutter Build Error in Beginner Project

I'm a beginner in Flutter app development and I'm encountering an error while trying to build an APK using flutter build apk. The error message mentions an "Unknown Kotlin JVM target: 21" within the nb_utils module.

Here's what I've tried so far:

I've changed my Java SDK version to Java 11. The full error message is:

FAILURE: Build failed with an exception.

Unknown Kotlin JVM target: 21

Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights. Get more help at https://help.gradle.org.

BUILD FAILED in 1s Running Gradle task 'assembleRelease'...   1,620ms Gradle task assembleRelease failed with exit code 1

my build.gradle

    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)
    }
}

android {
    compileSdkVersion 34

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

    lintOptions {
        disable 'InvalidPackage'
    }

    defaultConfig {
        applicationId "com.mighty.web"
        minSdkVersion 22
        targetSdkVersion 34
        versionCode 36
        versionName "5.0.2"
        multiDexEnabled true
    }

    buildTypes {
        release {
            minifyEnabled false
            shrinkResources false
            signingConfig signingConfigs.debug
        }
    }

    googleServices { disableVersionCheck = true }

}

flutter {
    source '../..'
}
allprojects {
    /// TODO: remove. hotfix for flutter_inappwebview (using androidx.webkit:webkit:1.8.0)
    /// webview_flutter_android: ^3.16.2 androidx.webkit:webkit ^1.9.0 remove SUPPRESS_ERROR_PAGE
    configurations.all {
        resolutionStrategy {
            force 'androidx.webkit:webkit:1.8.0'
        }
    }
}

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.8.0"
    implementation 'androidx.multidex:multidex:2.0.1'
    implementation 'com.google.firebase:firebase-analytics:21.2.2'
    implementation 'com.google.android.gms:play-services-basement:18.2.0'
}

Upvotes: 11

Views: 10707

Answers (2)

Azhar Ali
Azhar Ali

Reputation: 2476

I got the same error after updating my Android Studio, After struggling for many hours, my issue is fixed here is a solution (worked for my two projects)

app\build.gradle:

android {
  ndkVersion "25.1.8937393"

compileOptions {
  sourceCompatibility JavaVersion.VERSION_17
  targetCompatibility JavaVersion.VERSION_17
}
kotlinOptions {
  jvmTarget = 17
}

This in settings.gradle:

id "com.android.application" version "8.3.2" apply false
id "org.jetbrains.kotlin.android" version "2.0.20" apply false

And this in gradle-wrapper.properties:

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

How I find this solution, My logs after the Android Studio update (Beta version)

Android Studio Meerkat | 2024.3.1 Nightly 2025-01-14

App logs

Github link - https://github.com/flutter/flutter/issues/156304

Credit Go to @deakjahan here is his solution - https://github.com/flutter/flutter/issues/156304#issuecomment-2397707812

I hope it will work for others as well

Upvotes: 2

Nafis Kabbo
Nafis Kabbo

Reputation: 718

Please open the dependency "nb_utils" from direct/transitive [Screenshot attached of VS Code]

enter image description here

Now in "nb_utils" package, please add the below code in android block [android/app/build.gradle]

compileOptions {
    sourceCompatibility = JavaVersion.VERSION_1_8
    targetCompatibility = JavaVersion.VERSION_1_8
}

kotlinOptions {
    jvmTarget = JavaVersion.VERSION_1_8
}

Upvotes: 4

Related Questions