flutter-unity-view-widget icon indicating copy to clipboard operation
flutter-unity-view-widget copied to clipboard

Build Failure due to JVM Target Compatibility Issue in flutter_unity_widget (Kotlin 17 vs Java 1.8)

Open tsutou0224 opened this issue 1 year ago • 16 comments

Describe the bug When trying to run a Flutter application with the flutter_unity_widget package using "Run and Debug" in VS Code, an error occurs during the build process. The error specifically states a mismatch between the Java version for the compileDebugJavaWithJavac (targeting 1.8) and the compileDebugKotlin (targeting 17). The build fails as a result.

To Reproduce Steps to reproduce the behavior:

  1. Open a Flutter project using the flutter_unity_widget package in VS Code.
  2. Connect an Android device running Android 12 to the PC (running Windows).
  3. Run the project using the "Run and Debug" option.
  4. Observe the error during the build process.

Expected behavior The Flutter application should successfully run on the connected Android device without any build errors.

Error details

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':flutter_unity_widget:compileDebugKotlin'.
> 'compileDebugJavaWithJavac' task (current target is 1.8) and 'compileDebugKotlin' task (current target is 17) jvm target compatibility should be set to the same Java version.
  Consider using JVM toolchain: https://kotl.in/gradle/jvm/toolchain

* 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 9s
Error: Gradle task assembleDebug failed with exit code 1

Unity (please complete the following information):

  • OS: Windows
  • Version: 2022.3.40f1

Smartphone (please complete the following information):

  • Device: Android phone
  • OS: Android 12

Additional context

  • Flutter version: 3.24.1
  • Unity package: fuw-2022.2.0.unitypackage
  • Library: flutter_unity_widget: ^2022.2.1

tsutou0224 avatar Sep 23 '24 09:09 tsutou0224

Which Unity version did you use, and which jvm settings are in your gradle files?

timbotimbo avatar Sep 23 '24 09:09 timbotimbo

Thank you for your quick reply!

  1. Unity version: 2022.3.40f1

  2. jvm settings in my gradle files In the android/unityLibrary/build.gradle file, there is a section:

compileOptions {
    sourceCompatibility JavaVersion.VERSION_11
    targetCompatibility JavaVersion.VERSION_11
}

However, I couldn't find any code specifying the JVM version in the other android/build.gradle or android/app/build.gradle files.

android/build.gradle

allprojects {
    repositories {
        flatDir {
            dirs "${project(':unityLibrary').projectDir}/libs"
        }

        google()
        mavenCentral()
    }
}

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

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

android/app/build.gradle

plugins {
   id "com.android.application"
   id "kotlin-android"
   id "dev.flutter.flutter-gradle-plugin"
   id "com.google.gms.google-services"  // Google Services 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'
}

def keystoreProperties = new Properties()
def keystorePropertiesFile = rootProject.file('key.properties')
if (keystorePropertiesFile.exists()) {
    keystoreProperties.load(new FileInputStream(keystorePropertiesFile))
}

android {
    compileSdkVersion 34
    

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

    lintOptions {
        disable 'InvalidPackage'
        checkReleaseBuilds false
    }

    defaultConfig {
        // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
        applicationId "com.mycompany.signlang"
        minSdkVersion 24
        targetSdkVersion 32
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
    }

   signingConfigs {
       release {
           keyAlias keystoreProperties['keyAlias']
           keyPassword keystoreProperties['keyPassword']
           storeFile keystoreProperties['storeFile'] ? file(keystoreProperties['storeFile']) : null
           storePassword keystoreProperties['storePassword']
       }
   }

    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 project(':unityLibrary')

    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.9.0"
}

tsutou0224 avatar Sep 23 '24 10:09 tsutou0224

I am also getting the same issue

prajjwalZotoAshish avatar Sep 28 '24 09:09 prajjwalZotoAshish

Me too, I am also getting this issue.

mvn-khoavo-dn avatar Oct 04 '24 09:10 mvn-khoavo-dn

I am also getting the same issue

Syauqiaja avatar Nov 12 '24 19:11 Syauqiaja

Hi, I found a temporary solution while waiting for this library to be updated to a higher version of Java. The library currently uses Java version 1.8. However, since my project uses Java 17, I need to modify the Java version in this library. Here are the steps:

  1. Open .flutter-plugins and locate the path for flutter_unity_widget.
  2. Open that directory on your desktop.
  3. Open the build.gradle file at the path: [LIBRARY DIRECTORY]/android/build.gradle.
  4. Change the sourceCompatibility and targetCompatibility to match the Java version you are using.
  5. Downgrade your Flutter version to 3.22.0.
  6. The issue is resolved.

Hope this helps!

Syauqiaja avatar Nov 17 '24 04:11 Syauqiaja

1.download this dependency https://github.com/juicycleff/flutter-unity-view-widget 2. set path in your flutter project pubspec.yaml file in dependencies 3. in the above downloaded dependency change android/build.gradle of above dependency compileOptions { sourceCompatibility 17 targetCompatibility 17 } and also compilesdk to 30 or above

VivaanPathak avatar Nov 22 '24 09:11 VivaanPathak

I am also getting the same issue

Abdelilah-BH avatar Dec 11 '24 15:12 Abdelilah-BH

me too, I am also getting the same issue

GUD-Luen avatar Dec 13 '24 06:12 GUD-Luen

1.download this dependency https://github.com/juicycleff/flutter-unity-view-widget 2. set path in your flutter project pubspec.yaml file in dependencies 3. in the above downloaded dependency change android/build.gradle of above dependency compileOptions { sourceCompatibility 17 targetCompatibility 17 } and also compilesdk to 30 or above

I did exactly that, used the local dependency from master branch and updated the sourceCompatibility and targetCompatibility to 17. I did a flutter clean in my project and I still got this...

image

Am I missing something?

WouterVandenputte avatar Dec 15 '24 19:12 WouterVandenputte

@WouterVandenputte 3. in the above downloaded dependency change android/build.gradle of above you changed this inside the downloaded dependency or in the project . make sure you open local downloaded dependency first and inside it change android/build.gradle this

keyss-vivaan avatar Dec 16 '24 03:12 keyss-vivaan

I changed all build.gradle in unityLibrary, android/app, and downloaded dependency to 17. However, the same error occurs. image

Unity (please complete the following information):

  • OS: Windows
  • Version: 2022.3.54f1

Smartphone (please complete the following information):

  • Device: samsung galaxy z flip3
  • OS: Android 14

Additional context

  • Flutter version: 3.27.0
  • Unity package: fuw-2022.2.0.unitypackage Library: flutter_unity_widget: ^2022.2.1

GUD-Luen avatar Dec 16 '24 05:12 GUD-Luen

Can anyone try if this (temporary) workaround works? The snippet below should be able to override settings in the plugin, without having to edit the plugin yourself.

Update this in your local project files. You can change the JavaVersion depending on your error message. (17 or 21, etc.)

//<flutter project>/android/build.gradle

// make sure this is above any use of "evaluationDependsOn" 
subprojects {
    afterEvaluate {

        if (project.plugins.hasPlugin("com.android.application")
                || project.plugins.hasPlugin("com.android.library")) {

            if (project.name == "flutter_unity_widget") {
                // override version numbers if needed
                project.android.compileOptions {
                    sourceCompatibility = JavaVersion.VERSION_17
                    targetCompatibility = JavaVersion.VERSION_17
                }
                project.android.kotlinOptions {
                    jvmTarget = JavaVersion.VERSION_17
                }
                project.android {
                    // add namespace if it is missing
                    if (namespace == null) {
                        namespace = project.group
                    }
                    
                    // you can also override stuff like compileSdk
                    // compileSdk = 34
                }
            }
        }
    }
}

// this should already be in your android/build.gradle file.
subprojects {
    project.evaluationDependsOn(":app")
}

This error is likely due to a requirement change in some new gradle/AGP versions, for which the plugin is now outdated.

I just got this error after making the following gradle changes. (Flutter 3.24.5)

// settings.gradle
- id "com.android.application" version "7.4.2" apply false
+ id "com.android.application" version "8.1.0" apply false
- id "org.jetbrains.kotlin.android" version "1.8.21" apply false
+ id "org.jetbrains.kotlin.android" version "1.8.22" apply false

//gradle-wrapper.properties
- distributionUrl=https\://services.gradle.org/distributions/gradle-7.6.1-all.zip
+ distributionUrl=https\://services.gradle.org/distributions/gradle-8.3-all.zip

timbotimbo avatar Dec 18 '24 15:12 timbotimbo

@timbotimbo

//<flutter project>/android/build.gradle

// make sure this is above any use of "evaluationDependsOn" 
subprojects {
    afterEvaluate {

        if (project.plugins.hasPlugin("com.android.application")
                || project.plugins.hasPlugin("com.android.library")) {

            if (project.name == "flutter_unity_widget") {
                // override version numbers if needed
                project.android.compileOptions {
                    sourceCompatibility = JavaVersion.VERSION_17
                    targetCompatibility = JavaVersion.VERSION_17
                }
                project.android.kotlinOptions {
                    jvmTarget = JavaVersion.VERSION_17
                }
                project.android {
                    // add namespace if it is missing
                    if (namespace == null) {
                        namespace = project.group
                    }
                    
                    // you can also override stuff like compileSdk
                    // compileSdk = 34
                }
            }
        }
    }
}

// this should already be in your android/build.gradle file.
subprojects {
    project.evaluationDependsOn(":app")
}

This works for me. compileSdk = 34 was required.

And it could also be solved with the method below.

gradle.properties

kotlin.jvm.target.validation.mode = IGNORE

GUD-Luen avatar Dec 19 '24 01:12 GUD-Luen

Need I using all? if (project.name == "audioplayers_android" || project.name == "clipboard_watcher" || project.name == "device_calendar" || project.name == "device_info_plus" || project.name == "file_saver" || project.name == "fluttertoast" || project.name == "fluwx" || project.name == "package_info_plus" || project.name == "image_gallery_saver" || project.name == "flutter_foreground_task") { project.android.compileOptions { sourceCompatibility = JavaVersion.VERSION_17 targetCompatibility = JavaVersion.VERSION_17 } project.android.kotlinOptions { jvmTarget = JavaVersion.VERSION_17 } project.android { if (namespace == null) { namespace = project.group } // you can also override stuff like compileSdk // compileSdk = 34 } }

lucasjinreal avatar Jan 27 '25 16:01 lucasjinreal

I believe the source of this issue is fixed on the master branch.

The plugin build.gradle was missing the following:

kotlinOptions {
    jvmTarget = JavaVersion.VERSION_XXX
}

Because it wasn't defined, the default from your Android studio was used for Kotlin compilation. In your case this is Java 17, which doesn't match with the old Java version defined in the plugin.

The Java version in the plugin doesn't have to match your projects Java version to work.
But Java and Kotlin do need to use the same one.

For now you would have to import the master branch in pubspec.yaml as this isn't on pub.dev (yet).

timbotimbo avatar Feb 23 '25 23:02 timbotimbo