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

IllegalAccessError with Android Gradle Plugin 8.0.2 and Java 17 #7824

Open
ChrisBitzios opened this issue Jun 16, 2023 · 2 comments
Open

IllegalAccessError with Android Gradle Plugin 8.0.2 and Java 17 #7824

ChrisBitzios opened this issue Jun 16, 2023 · 2 comments

Comments

@ChrisBitzios
Copy link

ChrisBitzios commented Jun 16, 2023

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:
#7799
#7804

Two workarounds that we tried and worked are

  • Using :
afterEvaluate {
    tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile) {
      if (project.plugins.hasPlugin("com.android.application") || project.plugins.hasPlugin("com.android.library")) {
        kotlinOptions.jvmTarget = android.compileOptions.sourceCompatibility
      } else {
        kotlinOptions.jvmTarget = sourceCompatibility
      }
    }
  }

instead of:

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_17
        targetCompatibility JavaVersion.VERSION_17
    }
  • converting the related Java objects to Kotlin ones

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
Copy link

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
Copy link

Canato commented Sep 5, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants