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

Cannot compile with Kotlin 1.9.10 #1513

Closed
Matej-Hlatky opened this issue Sep 11, 2023 · 8 comments · Fixed by #1534
Closed

Cannot compile with Kotlin 1.9.10 #1513

Matej-Hlatky opened this issue Sep 11, 2023 · 8 comments · Fixed by #1534

Comments

@Matej-Hlatky
Copy link

Matej-Hlatky commented Sep 11, 2023

How frequently does the bug occur?

Always

Description

Used Android project with Kotlin v1.8.22 and Realm-Kotlin v1.10.0 with no issues.

When migrated to use Kotlin 1.9.x, specifically 1.9.10, cannot compile due to this error:

'compileDebugJavaWithJavac' task (current target is 17) and 'kaptGenerateStubsDebugKotlin' task (current target is 1.8) jvm target compatibility should be set to the same Java version.
  Consider using JVM toolchain: https://kotl.in/gradle/jvm/toolchain

Tbe build.gradle in every Android Module contains:

android {
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_17
        targetCompatibility JavaVersion.VERSION_17
    }
    kotlinOptions {
        jvmTarget = '17'
    }
}

without reference to Module containing dependency: io.realm.kotlin:library-base, the project compiles.

Stacktrace & log output

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

Can you reproduce the bug?

Always

Reproduction Steps

No response

Version

1.11.1, 1.10.0

What Atlas App Services are you using?

Local Database only

Are you using encryption?

No

Platform OS and version(s)

Android SDK 33

Build environment

Android Studio version: Android Studio Giraffe | 2022.3.1 Patch 1
Android Build Tools version: 33
Gradle version: 8.3

@cmelchior
Copy link
Contributor

Yes, unfortunately, the supported bytecode settings are a bit of a mess right now. Currently, we are shipping the library with Java 8 bytecode, mostly for historical reasons. We could probably safely bump it to 11, but not everyone is at Java 17 yet.

You can fix it by adding this to your build.gradle file:

tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).all {
    kotlinOptions {
        jvmTarget = "17"
    }
}

and if using kts files, it looks like this:

tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile>().all {
    kotlinOptions {
        jvmTarget = "17"
    }
}

@Matej-Hlatky
Copy link
Author

Hi, tried that before and it didn't work.
I'll try to make minimally reproducible repo.

@rorbech
Copy link
Contributor

rorbech commented Sep 26, 2023

We have removed an internal override of the Kotlin compile tasks target version in #1514. That could have an impact on this. The fix is already available in the 1.12.0-SNAPSHOT.

@polesapart
Copy link

Tried both 1.12.0-NEXTMAJORCORE-SNAPSHOT and 1.12.0-SNAPSHOT to no avail. Same error even with the block suggested above.

However, this override worked even with stable version (build.gradle.kts):

allprojects {
    tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile>().configureEach {
        kotlinOptions {
            jvmTarget = "17"
        }
    }
}

@cmelchior
Copy link
Contributor

Going over our gradle plugin we realized that we don't need to run KAPT anymore, so it has been removed, but we are still shipping our bytecode as a Java 8 target as that is the minimum Kotlin supports.

But it should be possible to configure it using the jvmToolchain as described in https://kotl.in/gradle/jvm/toolchain

@keinar
Copy link

keinar commented Oct 21, 2023

any solution?
I'm already 2 days on it.
this is what I get:

`

  • What went wrong:
    Could not determine the dependencies of task ':expo-modules-core$android-annotation-processor:compileJava'.

Could not resolve all task dependencies for configuration ':expo-modules-core$android-annotation-processor:compileClasspath'.
Could not resolve project :expo-modules-core$android-annotation.
Required by:
project :expo-modules-core$android-annotation-processor
> No matching variant of project :expo-modules-core$android-annotation was found. The consumer was configured to find a library for use during compile-time, compatible with Java 11, preferably in the form of class files, preferably optimized for standard JVMs, and its dependencies declared externally, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'jvm' but:
- Variant 'apiElements' capability host.exp.exponent:expo-modules-core$android-annotation:1.1.1 declares a library for use during compile-time, packaged as a jar, preferably optimized for standard JVMs, and its dependencies declared externally, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'jvm':
- Incompatible because this component declares a component, compatible with Java 17 and the consumer needed a component, compatible with Java 11
- Variant 'mainSourceElements' capability host.exp.exponent:expo-modules-core$android-annotation:1.1.1 declares a component, and its dependencies declared externally:
- Incompatible because this component declares a component of category 'verification' and the consumer needed a library
- Other compatible attributes:
- Doesn't say anything about its target Java environment (preferred optimized for standard JVMs)
- Doesn't say anything about its target Java version (required compatibility with Java 11)
- Doesn't say anything about its elements (required them preferably in the form of class files)
- Doesn't say anything about its usage (required compile-time)
- Doesn't say anything about org.jetbrains.kotlin.platform.type (required 'jvm')
- Variant 'runtimeElements' capability host.exp.exponent:expo-modules-core$android-annotation:1.1.1 declares a library for use during runtime, packaged as a jar, preferably optimized for standard JVMs, and its dependencies declared externally, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'jvm':
- Incompatible because this component declares a component, compatible with Java 17 and the consumer needed a component, compatible with Java 11
- Variant 'testResultsElementsForTest' capability host.exp.exponent:expo-modules-core$android-annotation:1.1.1:
- Incompatible because this component declares a component of category 'verification' and the consumer needed a library
- Other compatible attributes:
- Doesn't say anything about how its dependencies are found (required its dependencies declared externally)
- Doesn't say anything about its target Java environment (preferred optimized for standard JVMs)
- Doesn't say anything about its target Java version (required compatibility with Java 11)
- Doesn't say anything about its elements (required them preferably in the form of class files)
- Doesn't say anything about its usage (required compile-time)
- Doesn't say anything about org.jetbrains.kotlin.platform.type (required 'jvm')

  • 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.

BUILD FAILED in 19s`

@clementetb
Copy link
Contributor

@keinar It seems that you are declaring a module as Java 17 and your project is Java 11. Please check what module is causing the incompatibility as the Gradle output does not show it. The latest Realm version is compatible with Java 11.

You might consider bumping the Project Java version.

@shashikantkaushik
Copy link

//    I also face the same issue and i have solved this issue... in //order to solve this you must have same version of java and kotline //running on your system.
    To solve this you have to add some code in android/gradle file --
    buildscript {
        ext.kotlin_version = "1.8.0"
        repositories {
            google()
            mavenCentral()
        }
    
        dependencies {
            classpath 'com.android.tools.build:gradle:7.2.2'
            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
    }

//Your app level build.gradle must be matched like--

    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'
    }
    apply plugin: 'com.android.application'
    apply plugin: 'kotlin-android'
    
    android {
        namespace "com.example.aargus_shashi_1"
        compileSdkVersion 34
        buildToolsVersion '34.0.0'
    
        ndkVersion flutter.ndkVersion
    
        compileOptions {
            sourceCompatibility JavaVersion.VERSION_1_8
            targetCompatibility JavaVersion.VERSION_1_8
        }
    
        kotlinOptions {
            jvmTarget = '1.8'
        }
    
        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 "com.example.aargus_shashi_1"
            // 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 19
            multiDexEnabled true
            targetSdkVersion flutter.targetSdkVersion
            versionCode flutterVersionCode.toInteger()
            versionName flutterVersionName
        }
        dataBinding {
            enabled 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 {
        def multidex_version = "2.0.1"
        implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
        implementation 'com.google.android.gms:play-services-location:21.0.1'
        implementation "androidx.multidex:multidex:$multidex_version"
    
    }
   // Your gradle.wrapper.properties file present in //android/gradle/wrapper must be like-
    distributionBase=GRADLE_USER_HOME
    distributionPath=wrapper/dists
    zipStoreBase=GRADLE_USER_HOME
    zipStorePath=wrapper/dists
    distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-all.zip
    
    
  //  this error is caused by mismatch of the version which could //be usually happen when you integrate old library fucntion or code //into your new project.

//follow me for more help-
gitHub- https://github.com/shashikantkaushik
Linkedin-- https://in.linkedin.com/in/shashi-kant-kaushik

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 18, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants