Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
tsutou0224 opened this issue Sep 23, 2024 · 14 comments

Comments

@tsutou0224
Copy link

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
@timbotimbo
Copy link
Collaborator

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

@tsutou0224
Copy link
Author

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

@prajjwalZotoAshish
Copy link

I am also getting the same issue

@mvn-khoavo-dn
Copy link

mvn-khoavo-dn commented Oct 4, 2024

Me too, I am also getting this issue.

@Syauqiaja
Copy link

I am also getting the same issue

@Syauqiaja
Copy link

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!

@VivaanPathak
Copy link

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

@Abdelilah-BH
Copy link

I am also getting the same issue

@GUD-Luen
Copy link

me too, I am also getting the same issue

@WouterVandenputte
Copy link

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?

@keyss-vivaan
Copy link

@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

@GUD-Luen
Copy link

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

@timbotimbo
Copy link
Collaborator

timbotimbo commented Dec 18, 2024

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

@GUD-Luen
Copy link

GUD-Luen commented Dec 19, 2024

@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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants