realm / realm-kotlin

Kotlin Multiplatform and Android SDK for the Realm Mobile Database: Build Better Apps Faster.
Apache License 2.0
942 stars 57 forks source link

Realm AnalyticsService class cast exception in 1.13.0 #1591

Closed msa1422 closed 9 months ago

msa1422 commented 10 months ago

How frequently does the bug occur?

Always

Description

Upgrading Realm to v1.13.0 causes compile time error in a Kotlin Multiplatform project. I have 2 modules (with Realm gradle plugin), one has a local Realm setup and the second one has device sync Realm setup. The error occurs only for the latter module. However, in another project where I am using only Sync config realm (only one module with realm plugin), I am facing no issues in upgrading the realm.

Stacktrace & log output

class io.realm.kotlin.gradle.analytics.AnalyticsService$Inject cannot be cast to class io.realm.kotlin.gradle.analytics.AnalyticsService (io.realm.kotlin.gradle.analytics.AnalyticsService$Inject is in unnamed module of loader org.gradle.internal.classloader.VisitableURLClassLoader$InstrumentingVisitableURLClassLoader @1fb0ab3b; io.realm.kotlin.gradle.analytics.AnalyticsService is in unnamed module of loader org.gradle.internal.classloader.VisitableURLClassLoader$InstrumentingVisitableURLClassLoader @b594990)
class io.realm.kotlin.gradle.analytics.AnalyticsService$Inject cannot be cast to class io.realm.kotlin.gradle.analytics.AnalyticsService (io.realm.kotlin.gradle.analytics.AnalyticsService$Inject is in unnamed module of loader org.gradle.internal.classloader.VisitableURLClassLoader$InstrumentingVisitableURLClassLoader @1fb0ab3b; io.realm.kotlin.gradle.analytics.AnalyticsService is in unnamed module of loader org.gradle.internal.classloader.VisitableURLClassLoader$InstrumentingVisitableURLClassLoader @b594990)

Gradle's dependency cache may be corrupt (this sometimes occurs after a network connection timeout.)

Re-download dependencies and sync project (requires network)
The state of a Gradle build process (daemon) may be corrupt. Stopping all Gradle daemons may solve this problem.

Stop Gradle build processes (requires restart)
Your project may be using a third-party plugin which is not compatible with the other plugins in the project or the version of Gradle requested by the project.

In the case of corrupt Gradle processes, you can also try closing the IDE and then killing all Java processes.

Can you reproduce the bug?

Always

Reproduction Steps

Upgrading Realm to v1.13.0

Version

Realm v1.13.0

What Atlas App Services are you using?

Both Atlas Device Sync and Atlas App Services

Are you using encryption?

Yes

Platform OS and version(s)

Android & iOS

Build environment

Android Studio version: Hedgehog 2023.1.1 Android Build Tools version: 34.0.0 Android Gradle Plugin version: reproducible on both 8.1.4 and 8.2.0 Gradle version: reproducible on both 8.4 and 8.5 Kotlin version: reproducible on both 1.9.10 and 1.9.20

cmelchior commented 9 months ago

I tried to reproduce the crashing using this sample project which we use for integration tests: https://github.com/realm/realm-kotlin/tree/main/integration-tests/gradle/gradle8-test

It also contains two modules, both using the Realm Gradle Plugin. The source code are currently using 8.3, but upgrading to 8.5 did not trigger the crash.

Are you by any chance loading two different versions of the Plugin?

Otherwise, if you are able to reproduce in a sample project we can try to figure out what is wrong from that, or perhaps you can compare our sample project with yours to see where they might differ.

msa1422 commented 9 months ago

I have created a sample project to reproduce this issue. However, it was reproduced just once. It happened when I added another (codingfeline.buildkonfig) plugin to Realm sync module. On next gradle sync, the issue disappeared. I will investigate more on this issue. I am using Version Catalog for dependency management, so I believe the plugin version should be same for both modules.

EDIT: After some more tweaking and hit and trial, I believe there is some issue with either Gradle caching or Plugin management in Kotlin DSL build.gradle.kts I applied the same set of plugins in both the modules, rebuilt the project and the issue has disappeared. I'll try to figure out the exact problem soon, but as of now, issue seems to be on Gradle or Kotlin side.

cmelchior commented 9 months ago

Yes, it sounds like it could be a Gradle cache issue somehow. Unfortunately, I'm not sure there is much we can do about it.

cmelchior commented 9 months ago

After discussing this, we are going to close this issue, as it most likely is a Gradle issue (which the error message also says). If this persists after cleaning the Gradle cache, please get back in touch.

Ynnck123 commented 9 months ago

@cmelchior I am affected by exactly the same issue when upgrading to v1.13.0 but unfortunately not able to solve it by rebuilding or cleaning the Gradle cache.

cmelchior commented 9 months ago

@Ynnck123 If you can reproduce this in a sample project. We will be happy to take a look.