skip to Main Content

i have been trying to solve this problem for about 4 days now everything was going smooth until one day i updated android studio then the app wont build

i searched for a solution.
Updated kotlin to the lastest version ext.kotlin_version = ‘2.0.20’. still no answer
app/build.gradle :

plugins {
    id "com.android.application"
    id "kotlin-android"
    id "dev.flutter.flutter-gradle-plugin"
}

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 "trojandc.com.beshoy_nan"
    compileSdkVersion flutter.compileSdkVersion
    ndkVersion flutter.ndkVersion

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_17
        targetCompatibility JavaVersion.VERSION_17
    }

    kotlinOptions {
        jvmTarget = '17'
    }

    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 "trojandc.com.beshoy_nan"
        // 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 23
        targetSdkVersion flutter.targetSdkVersion
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
        multiDexEnabled true

    }

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

flutter {
    source '../..'
}

dependencies {
    implementation 'androidx.multidex:multidex:2.0.1'
}

android/build.gradle :

buildscript {
    ext.kotlin_version = '2.0.20'
    repositories {
        google()
        mavenCentral()
    }

    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

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
}

gradle.properties :

org.gradle.jvmargs=-Xmx1536m -Duser.country=US -Duser.language=en
android.useAndroidX=true
android.enableJetifier=true
kotlin.daemon.jvm.options=-Xmx2g

gradle-wrapper.properties :

distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https://services.gradle.org/distributions/gradle-8.10-all.zip

settings.gradle :

pluginManagement {
    def flutterSdkPath = {
        def properties = new Properties()
        file("local.properties").withInputStream { properties.load(it) }
        def flutterSdkPath = properties.getProperty("flutter.sdk")
        assert flutterSdkPath != null, "flutter.sdk not set in local.properties"
        return flutterSdkPath
    }
    settings.ext.flutterSdkPath = flutterSdkPath()

    includeBuild("${settings.ext.flutterSdkPath}/packages/flutter_tools/gradle")

    repositories {
        google()
        mavenCentral()
        gradlePluginPortal()
    }

    plugins {
        id "dev.flutter.flutter-gradle-plugin" version "1.0.0" apply false
    }
}

plugins {
    id "dev.flutter.flutter-plugin-loader" version "1.0.0"
    id "com.android.application" version "7.3.0" apply false
    id "org.jetbrains.kotlin.android" version "2.0.20" apply false
}

include ":app"

error:

FAILURE: Build failed with an exception.

  • What went wrong:
    Execution failed for task ‘:image_gallery_saver:compileReleaseKotlin’.

Inconsistent JVM-target compatibility detected for tasks ‘compileReleaseJavaWithJavac’ (1.8) and ‘compileReleaseKotlin’ (17).

Consider using JVM Toolchain: https://kotl.in/gradle/jvm/toolchain
Learn more about JVM-target validation: https://kotl.in/gradle/jvm/target-validation

  • Try:

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 3s
Running Gradle task ‘assembleRelease’… 3.6s
Gradle task assembleRelease failed with exit code 1

2

Answers


  1. Your gradle version has been updated to the latest version 8.10, here are the steps you need to do to fix this:

    1-
    Run the java --version command in the terminal to find out the java version, for example, my java version is 21.0.3

    2- Go to https://docs.gradle.org/current/userguide/compatibility.html and find the gradle version that matches your java version.
    My java version is 21 and the corresponding gradle version is 8.5.

    3- Go to android/gradle/wrapper/gradle-wrapper.properties file

    4- Update the latest version

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

    value, for example,
    change the value to 8.10 according to the compatible java version, for example, if java 21, change it to 8.5.
    This adds the correct gradle version. After these steps, run the project. It will download the package, which will take a long time,
    and then you will get an error again. This error is related to Kotlin and Java incompatibility.

    Then let’s resolve the Java and Kotlin incompatibility
    Go to app/build.gradle

     kotlinOptions {
            jvmTarget = JavaVersion.Version_1.8 
        }
    

    or pole

    kotlinOptions{ jvmTarget = 1.8 
    }
    

    you can enter,
    The value 1.8 is representative. Kotlin options and compileOptions should be equal. If the compileOPtions value is 1.8, you should make Kotlin 1.8 as well.

    compileOptions {
            sourceCompatibility JavaVersion.VERSION_1_8
            targetCompatibility JavaVersion`enter code here`.VERSION_1_8
        } 
    
    Login or Signup to reply.
  2. The error occurs due to a mismatch between the JVM target versions for Java and Kotlin. Java is targeting JVM 1.8, while Kotlin is targeting JVM 17. To resolve this, you need to align the JVM target versions for both.

    Solution:

    1. Update Kotlin JVM Target:
      Modify your build.gradle file to explicitly set the JVM target for Kotlin.

      Inside your android block, add the following:

      android {
          // your existing configuration
      
          kotlinOptions {
              jvmTarget = "1.8"
          }
      }
      
    2. Update Java Compatibility:
      Ensure your compileOptions is also targeting the same JVM version (1.8) for Java:

      android {
          compileOptions {
              sourceCompatibility = JavaVersion.VERSION_1_8
              targetCompatibility = JavaVersion.VERSION_1_8
          }
      }
      
    3. Sync the Project:
      After making these changes, sync your Gradle files, and the error should be resolved.

    Let me know if this works for you!

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