QuisApp / flutter_contacts

MIT License
83 stars 138 forks source link

Compilation error due to mismatch between compileDebugJavaWithJavac and compileDebugKotlin #142

Closed Valent1 closed 1 month ago

Valent1 commented 10 months ago

This issue started happening after upgrading my Kotlin version to 1.9.20 and Gradle plugin to 8.3.

Whenever I try to run the app on Android devices or Android emulator (no issues with iOS), I get this error message during assembleDebug:

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':flutter_contacts:compileDebugKotlin'.
> Inconsistent JVM-target compatibility detected for tasks 'compileDebugJavaWithJavac' (1.8) and 'compileDebugKotlin' (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 6s
Exception: Gradle task assembleDebug failed with exit code 1

I have tried to change the following in my app/build.gradle, but nothing seems to have any effect on the final result, I always get the same exact error:

    compileOptions {
        //sourceCompatibility JavaVersion.VERSION_1_8
        //targetCompatibility JavaVersion.VERSION_1_8
        sourceCompatibility JavaVersion.VERSION_17
        targetCompatibility JavaVersion.VERSION_17
    }

    kotlinOptions {
        //jvmTarget = '1.8'
        jvmTarget = '17'
    }

This is the content of my android/build.gradle:

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

    dependencies {
        classpath 'com.android.tools.build:gradle:8.1.3'
        // START: FlutterFire Configuration
        classpath 'com.google.gms:google-services:4.4.0'
        // END: FlutterFire Configuration
        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
}

This is the content of my app/build.gradle:

def localProperties = new Properties()
def localPropertiesFile = rootProject.file('local.properties')
if (localPropertiesFile.exists()) {
    localPropertiesFile.withReader('UTF-8') { reader ->
        localProperties.load(reader)
    }
}

def flutterRoot = localProperties.getProperty('flutter.sdk')
if (flutterRoot == null) {
    throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.")
}

def flutterVersionCode = localProperties.getProperty('flutter.versionCode')
if (flutterVersionCode == null) {
    flutterVersionCode = '1'
}

def flutterVersionName = localProperties.getProperty('flutter.versionName')
if (flutterVersionName == null) {
    flutterVersionName = '1.0'
}

apply plugin: 'com.android.application'
// START: FlutterFire Configuration
apply plugin: 'com.google.gms.google-services'
// END: FlutterFire Configuration
apply plugin: 'kotlin-android'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

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

android {
    if (!project.hasProperty("namespace")) {
        namespace 'app.airplaza.airplaza'
    }

    // I had to change this to 33 to make it work with Firebase Messaging
    compileSdk 33
    ndkVersion flutter.ndkVersion

    compileOptions {
        //sourceCompatibility JavaVersion.VERSION_1_8
        //targetCompatibility JavaVersion.VERSION_1_8
        sourceCompatibility JavaVersion.VERSION_17
        targetCompatibility JavaVersion.VERSION_17
    }

    kotlinOptions {
        //jvmTarget = '1.8'
        jvmTarget = '17'
    }

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

    defaultConfig {
        // Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
        applicationId "app.airplaza.airplaza"
        // 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 31
        targetSdkVersion flutter.targetSdkVersion
        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 {
            // Add your own signing config for the release build.
            signingConfig signingConfigs.release
            // Signing with the debug keys for now, so `flutter run --release` works.
            //signingConfig signingConfigs.debug
        }
    }
}

flutter {
    source '../..'
}

dependencies {
    // It seems like the following is not needed anymore:
    // https://stackoverflow.com/questions/49956051/warning-kotlin-plugin-version-is-not-the-same-as-library-version-but-it-is
    //implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"

    // Import the Firebase BoM
    implementation platform('com.google.firebase:firebase-bom:32.3.1')

    // Add the dependencies for Firebase products you want to use
    // https://firebase.google.com/docs/android/setup#available-libraries
    // When using the BoM, no need to specify versions in Firebase dependencies
    implementation 'com.google.firebase:firebase-analytics-ktx'
    implementation 'com.google.firebase:firebase-analytics'
    implementation 'com.google.firebase:firebase-auth'
    implementation 'com.google.firebase:firebase-database'
    implementation 'com.google.firebase:firebase-messaging'
    // I had to comment out the line below because it was giving me this build error:
    // Could not find com.google.firebase:firebase-ui-auth:. Required by: project :app
    // implementation 'com.google.firebase:firebase-ui-auth'

    // Branch.io Setup for DeepLinks
    // required for all Android apps
    implementation 'io.branch.sdk.android:library:5.+'
    // required if your app is in the Google Play Store (tip: avoid using bundled play services libs)
    implementation 'com.google.android.gms:play-services-ads-identifier:18.0.1+'
    // alternatively, use the following lib for getting the AAID
    // implementation 'com.google.android.gms:play-services-ads:17.2.0'
    // optional
    // Chrome Tab matching (enables 100% guaranteed matching based on cookies)
    implementation 'androidx.browser:browser:1.5.0'
    // Replace above with the line below if you do not support androidx
    // implementation 'com.android.support:customtabs:28.0.0'
}

This is the output of my flutter doctor -v:

➜  android git:(main) ✗ flutter doctor -v
[✓] Flutter (Channel stable, 3.16.0, on macOS 14.1.1 23B81 darwin-arm64, locale en-US)
    • Flutter version 3.16.0 on channel stable at /Users/valentino/Flutter/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision db7ef5bf9f (28 hours ago), 2023-11-15 11:25:44 -0800
    • Engine revision 74d16627b9
    • Dart version 3.2.0
    • DevTools version 2.28.2

[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0-rc1)
    • Android SDK at /Users/valentino/Library/Android/sdk
    • Platform android-34, build-tools 34.0.0-rc1
    • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 17.0.6+0-17.0.6b829.9-10027231)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 15.0.1)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 15A507
    • CocoaPods version 1.14.2

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2022.3)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 17.0.6+0-17.0.6b829.9-10027231)

[✓] VS Code (version 1.84.2)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.76.0

[✓] Connected device (3 available)
    • sdk gphone64 arm64 (mobile) • emulator-5554 • android-arm64  • Android 13 (API 33) (emulator)
    • macOS (desktop)             • macos         • darwin-arm64   • macOS 14.1.1 23B81 darwin-arm64
    • Chrome (web)                • chrome        • web-javascript • Google Chrome 119.0.6045.159
    ! Error: Browsing on the local area network for iPhone XS Max. Ensure the device is unlocked and attached with a cable or associated with the same local area network as this Mac.
      The device must be opted into Developer Mode to connect wirelessly. (code -27)

[✓] Network resources
    • All expected network resources are available.

• No issues found!
➜  android git:(main) ✗ runner
The file /Users/valentino/AndroidStudioProjects/airplaza/android/ios/Runner.xcworkspace does not exist.
➜  android git:(main) ✗ cd ..
➜  airplaza git:(main) ✗ runner
➜  airplaza git:(main) ✗ flutter doctor -v
[✓] Flutter (Channel stable, 3.16.0, on macOS 14.1.1 23B81 darwin-arm64, locale en-US)
    • Flutter version 3.16.0 on channel stable at /Users/valentino/Flutter/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision db7ef5bf9f (28 hours ago), 2023-11-15 11:25:44 -0800
    • Engine revision 74d16627b9
    • Dart version 3.2.0
    • DevTools version 2.28.2

[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0-rc1)
    • Android SDK at /Users/valentino/Library/Android/sdk
    • Platform android-34, build-tools 34.0.0-rc1
    • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 17.0.6+0-17.0.6b829.9-10027231)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 15.0.1)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 15A507
    • CocoaPods version 1.14.2

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2022.3)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 17.0.6+0-17.0.6b829.9-10027231)

[✓] VS Code (version 1.84.2)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.76.0

[✓] Connected device (4 available)
    • sdk gphone64 arm64 (mobile) • emulator-5554             • android-arm64  • Android 13 (API 33) (emulator)
    • iPhone XS Max (mobile)      • 00008020-000130A82600013A • ios            • iOS 17.0.3 21A360
    • macOS (desktop)             • macos                     • darwin-arm64   • macOS 14.1.1 23B81 darwin-arm64
    • Chrome (web)                • chrome                    • web-javascript • Google Chrome 119.0.6045.159

[✓] Network resources
    • All expected network resources are available.

• No issues found!
Valent1 commented 10 months ago

I have also tried to comment out compileOptions and kotlinOptions in the app/build.gradle, replacing them with the jvmToolchain, but the error is still the same:

    kotlin {
        jvmToolchain(17)
    }

    //compileOptions {
    //    sourceCompatibility JavaVersion.VERSION_17
    //    targetCompatibility JavaVersion.VERSION_17
    //}

    //kotlinOptions {
    //    jvmTarget = '17'
    //}
beats0126 commented 9 months ago

Having same issues.

Execution failed for task ':flutter_contacts: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

beats0126 commented 9 months ago

Having same issues.

Execution failed for task ':flutter_contacts: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

My setup

Doctor summary (to see all details, run flutter doctor -v): [√] Flutter (Channel stable, 3.16.0, on Microsoft Windows [Version 10.0.22635.2771], locale en-US) [√] Windows Version (Installed version of Windows is version 10 or higher) [√] Android toolchain - develop for Android devices (Android SDK version 34.0.0) [√] Chrome - develop for the web [√] Visual Studio - develop Windows apps (Visual Studio Community 2022 17.7.5) [√] Android Studio (version 2022.3) [√] VS Code (version 1.84.2) [√] Connected device (4 available) [√] Network resources

dependencies {
    classpath 'com.android.tools.build:gradle:8.1.3'
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    classpath 'com.google.gms:google-services:4.4.0'
}

compileOptions {
    coreLibraryDesugaringEnabled true
    sourceCompatibility JavaVersion.VERSION_11
    targetCompatibility JavaVersion.VERSION_11
}

kotlinOptions {
    jvmTarget = '11'
}

and multi flavor implemented.

Valent1 commented 9 months ago

Nothing at all?

Valent1 commented 9 months ago

I fixed this by adding:

    kotlin {
        jvmToolchain {
            languageVersion.set(JavaLanguageVersion.of(8))
        }
    }

to the build.gradle file. Please modify your build.gradle file as follows:

group 'co.quis.flutter_contacts'
version '1.0-SNAPSHOT'

buildscript {
    ext.kotlin_version = '1.9.0'
    ext.kotlin_coroutines_version = '1.6.4'
    repositories {
        google()
        jcenter()
        mavenCentral()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:8.1.4'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

rootProject.allprojects {
    repositories {
        google()
        jcenter()
        mavenCentral()
    }
}

apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'

android {
    compileSdkVersion 31

    kotlin {
        jvmToolchain {
            languageVersion.set(JavaLanguageVersion.of(8))
        }
    }

    if (!project.hasProperty("namespace")) {
        namespace 'co.quis.flutter_contacts'
    }

    sourceSets {
        main.java.srcDirs += 'src/main/kotlin'
    }
    defaultConfig {
        minSdkVersion 16
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    lintOptions {
        disable 'InvalidPackage'
    }
}

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$kotlin_coroutines_version"
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$kotlin_coroutines_version"
    implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.0.0")
}
dharmendrasinh-concetto commented 8 months ago

Add below code in your project level build.gradle file. This worked for me. Also help me in other modules where same issue occurred.

rootProject.buildDir = '../build'
subprojects {
  afterEvaluate {
        if (project.plugins.hasPlugin("com.android.application")
                || project.plugins.hasPlugin("com.android.library")) {

            if (project.name == "flutter_contacts") {
                project.android.compileOptions {
                    sourceCompatibility = JavaVersion.VERSION_17
                    targetCompatibility = JavaVersion.VERSION_17
                }
            }
        }
    }
    project.buildDir = "${rootProject.buildDir}/${project.name}"
    project.evaluationDependsOn(':app')
}
joachim-quis commented 3 months ago

Is this the same as https://github.com/QuisApp/flutter_contacts/issues/158 ?

Valent1 commented 3 months ago

I have already provided the solution, you simply need to implement it.

Add the following lines to your android/build.gradle:

    kotlin {
        jvmToolchain {
            languageVersion.set(JavaLanguageVersion.of(8))
        }
    }

Please refer to my previous message:

I fixed this by adding:

    kotlin {
        jvmToolchain {
            languageVersion.set(JavaLanguageVersion.of(8))
        }
    }

to the build.gradle file. Please modify your build.gradle file as follows:

group 'co.quis.flutter_contacts'
version '1.0-SNAPSHOT'

buildscript {
    ext.kotlin_version = '1.9.0'
    ext.kotlin_coroutines_version = '1.6.4'
    repositories {
        google()
        jcenter()
        mavenCentral()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:8.1.4'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

rootProject.allprojects {
    repositories {
        google()
        jcenter()
        mavenCentral()
    }
}

apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'

android {
    compileSdkVersion 31

    kotlin {
        jvmToolchain {
            languageVersion.set(JavaLanguageVersion.of(8))
        }
    }

    if (!project.hasProperty("namespace")) {
        namespace 'co.quis.flutter_contacts'
    }

    sourceSets {
        main.java.srcDirs += 'src/main/kotlin'
    }
    defaultConfig {
        minSdkVersion 16
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    lintOptions {
        disable 'InvalidPackage'
    }
}

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$kotlin_coroutines_version"
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$kotlin_coroutines_version"
    implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.0.0")
}
Valent1 commented 3 months ago

And yes, this is related to https://github.com/QuisApp/flutter_contacts/issues/147 and https://github.com/QuisApp/flutter_contacts/issues/158

And it still happens with version 1.1.8

arabnology commented 3 months ago

OH cmon developer!!! we need real fix for this problem, can you please upgrade you package!!

arabnology commented 3 months ago

I have already provided the solution, you simply need to implement it.

Add the following lines to your android/build.gradle:

    kotlin {
        jvmToolchain {
            languageVersion.set(JavaLanguageVersion.of(8))
        }
    }

Please refer to my previous message:

I fixed this by adding:

    kotlin {
        jvmToolchain {
            languageVersion.set(JavaLanguageVersion.of(8))
        }
    }

to the build.gradle file. Please modify your build.gradle file as follows:

group 'co.quis.flutter_contacts'
version '1.0-SNAPSHOT'

buildscript {
    ext.kotlin_version = '1.9.0'
    ext.kotlin_coroutines_version = '1.6.4'
    repositories {
        google()
        jcenter()
        mavenCentral()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:8.1.4'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

rootProject.allprojects {
    repositories {
        google()
        jcenter()
        mavenCentral()
    }
}

apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'

android {
    compileSdkVersion 31

    kotlin {
        jvmToolchain {
            languageVersion.set(JavaLanguageVersion.of(8))
        }
    }

    if (!project.hasProperty("namespace")) {
        namespace 'co.quis.flutter_contacts'
    }

    sourceSets {
        main.java.srcDirs += 'src/main/kotlin'
    }
    defaultConfig {
        minSdkVersion 16
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    lintOptions {
        disable 'InvalidPackage'
    }
}

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$kotlin_coroutines_version"
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$kotlin_coroutines_version"
    implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.0.0")
}

Is there any negative effect on the project? I mean, is this affecting anything else in the project or breaking something?

Valent1 commented 2 months ago

Can you please simply update the build.gradle file with the changes I have shown? It really takes 30 seconds, and fixes the issue.

arabnology commented 2 months ago

Can you please simply update the build.gradle file with the changes I have shown? It really takes 30 seconds, and fixes the issue.

This is what worked for me: https://github.com/QuisApp/flutter_contacts/issues/142#issuecomment-1898946388

joachim-quis commented 1 month ago

Hi all - thanks for your patience and the recommended solutions.

This should be fixed in version 1.1.9. If it is not, please let me know, and reopen this conversation.

Valent1 commented 1 month ago

Yes, it fixes the issue and the app now compiles fine on Android. Thanks!