Build Failure due to JVM Target Compatibility Issue in flutter_unity_widget (Kotlin 17 vs Java 1.8)
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:
- Open a Flutter project using the flutter_unity_widget package in VS Code.
- Connect an Android device running Android 12 to the PC (running Windows).
- Run the project using the "Run and Debug" option.
- 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
Which Unity version did you use, and which jvm settings are in your gradle files?
Thank you for your quick reply!
-
Unity version: 2022.3.40f1
-
jvm settings in my gradle files In the
android/unityLibrary/build.gradlefile, 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"
}
I am also getting the same issue
Me too, I am also getting this issue.
I am also getting the same issue
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:
- Open .flutter-plugins and locate the path for flutter_unity_widget.
- Open that directory on your desktop.
- Open the build.gradle file at the path: [LIBRARY DIRECTORY]/android/build.gradle.
- Change the sourceCompatibility and targetCompatibility to match the Java version you are using.
- Downgrade your Flutter version to 3.22.0.
- The issue is resolved.
Hope this helps!
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 am also getting the same issue
me too, I am also getting the same issue
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...
Am I missing something?
@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
I changed all build.gradle in unityLibrary, android/app, and downloaded dependency to 17. However, the same error occurs.
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
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
//<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
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 } }
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).