realm / realm-java

Realm is a mobile database: a replacement for SQLite & ORMs
http://realm.io
Apache License 2.0
11.47k stars 1.75k forks source link

IllegalAccessError with Android Gradle Plugin 8.0.2 and Java 17 #7824

Open ChrisBitzios opened 1 year ago

ChrisBitzios commented 1 year ago

How frequently does the bug occur?

Always

Description

We recently bumped our Android app to Realm 10.16.0 Kotlin 1.8.21 Gradle 8.0.2 kotlinCompilerExtensionVersion 1.4.7 And due to some kapt related error we also had to specifically set Java to the app and its modules like:

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_17
        targetCompatibility JavaVersion.VERSION_17
    }

With this setup we are getting an error when trying to run our unit tests and these unit tests use Java objects that include the LinkingObjects annotation:

java.lang.IllegalAccessError: Update to non-static final field com.example.myapplication.objects.Dog.owners attempted from a different method (realmSet$owners) than the initializer method <init>

This error maybe related to these ones: https://github.com/realm/realm-java/issues/7799 https://github.com/realm/realm-java/issues/7804

Two workarounds that we tried and worked are

Unfortunately both solutions are risky or hacky (the first one) or require too much work (the second one) and are not acceptable for the time being.

Stacktrace & log output

java.lang.IllegalAccessError: Update to non-static final field com.example.myapplication.objects.Dog.owners attempted from a different method (realmSet$owners) than the initializer method <init>

Can you reproduce the bug?

Always

Reproduction Steps

For this reason I created a demo project based on the same setup as our project which demonstrates the same behaviour MyApplication.zip

Version

Android 13

What Atlas App Services are you using?

Local Database only

Are you using encryption?

Yes

Platform OS and version(s)

Android 13

Build environment

Android Studio version: Flamingo | 2022.2.1 Realm 10.16.0 Kotlin 1.8.21 Gradle 8.0.2 kotlinCompilerExtensionVersion 1.4.7

patricknanganocappello commented 1 year ago

I've got the same problem with my Java application after upgrading to AGP 8.0.2 and Java 17. Do you still use workarounds to solve it or we have any chance to get it fixed?

Canato commented 1 year ago

I got a possible solution on another thread @patricknanganocappello

Check here

Let me know if works, I still didn't had the time to check myself