realm-kotlin icon indicating copy to clipboard operation
realm-kotlin copied to clipboard

Realm incompatible with Kotlin projects compiled against a Java version newer than 8

Open jakobkmar opened this issue 2 years ago • 2 comments

Cause: https://github.com/realm/realm-kotlin/blob/0198f3a408c1f34fe27fc1d82b75df80e6e7de49/packages/gradle-plugin/src/main/kotlin/io/realm/kotlin/gradle/RealmPlugin.kt#L92

Because of this, Realm is incompatible with other Kotlin libraries which have been built targetting a newer JVM version - since it is impossible to inline functions from these libraries then.

I don't think the Realm Gradle plugin should force all compilations to target JVM 1.8, right?

The temporary workaround to overwrite what the Realm Gradle plugin set here is the following:

project.afterEvaluate {
    kotlin.target.compilations.all { // use targets if this is a Multiplatform project
        kotlinOptions.jvmTarget = "17"
    }
}

jakobkmar avatar Jun 12 '22 17:06 jakobkmar

@jakobkmar Thanks for the report and temporary workaround. We will investigate how to enforce our minimal required version along with working with other libraries. Do you mind sharing any immediate combination of libraries/setup that triggers this?

rorbech avatar Jun 28 '22 14:06 rorbech

Sorry for the late reply!

You simply have to find a library which has been compiled targetting a more recent Java version than 8 and try using an inline function from that library. I currently do not know any lightweight library which I can recommend for quickly testing this - but if you really need it I can search for one.

However creating a very simple one with one inline function taking a lambda parameter and publishing it to Maven Local and then adding it to a realm project is enough to trigger this issue.

jakobkmar avatar Jul 22 '22 20:07 jakobkmar

Thanks a lot for the workaround. It's a lifesaver!

I have faced the same problem with my stack: Compose (which requires JVM 11) + Koin (using reified inline functions to provide dependencies) + Realm.

Also, I noticed that the workaround looks like an extension-based solution. I rewrote it a bit:

project.afterEvaluate {
    kotlin.targets.all {
        compilations.all {
            (kotlinOptions as? org.jetbrains.kotlin.gradle.dsl.KotlinJvmOptions)?.jvmTarget = "11"
        }
    }
}

anton-avm avatar Mar 22 '23 13:03 anton-avm

I ran into this issue while implementing: Nordic's BLE Library and got the error on asValidResponseFlow() off of setNotificationCallback.

I can confirm the above fix allows my code to once again compile.

ghost avatar Apr 26 '23 21:04 ghost

Is there an update on this issue? The workaround is effective for me, but it remains just that — a workaround

amadeuszblanik avatar Aug 12 '23 23:08 amadeuszblanik

This is fixed by #1513 available from the 1.12.0 release.

rorbech avatar Nov 09 '23 16:11 rorbech