Skip to content

IllegalAccessError with Android Gradle Plugin 8.x and Java 17 #7799

@mannodermaus

Description

@mannodermaus

How frequently does the bug occur?

Always

Description

In the process of upgrading our codebase to Android Gradle Plugin 8.x, we are forced to update to Java 17+ in order to compile. Note that this is independent of the sourceCompatibility and targetCompatibility settings in the build scripts: Downgrading these values to a lower number does not prevent this issue from happening.

It seems like usage of internal APIs inside io.realm.processor.Utils causes a compilation error at runtime because of a missing add-opens declaration. Previous versions of JDK might've been more relaxed about it, but now those workarounds seem no longer working. I have tried adding the add-opens declaration to my jvmArgs via gradle.properties, but to no avail: The issue remains.

Since I assume that a refactoring of the offending code might not be feasible, could we maybe introduce a proper module-info.java to the annotation processor so it can tell javac about the required modules itself? Are there any plans to update Realm's toolchain to accommodate the increasing strictness of Java's module system?

Stacktrace & log output

After updating to Java 17 (even with sourceCompatibility and targetCompatibility capped at 11), trying to compile the Android project fails with:

Execution failed for task ':app:compileDebugJavaWithJavac'.
> java.lang.IllegalAccessError: class io.realm.processor.Utils (in unnamed module @0x64c9b2bd) cannot access class com.sun.tools.javac.code.Symbol$ClassSymbol (in module jdk.compiler) because module jdk.compiler does not export com.sun.tools.javac.code to unnamed module @0x64c9b2bd

Can you reproduce the bug?

Always

Reproduction Steps

  • Create an Android project using AGP 8.x and its required Java 17 toolchain
  • Add io.realm:realm-transformer:10.13.3-transformer-api to its dependencies
  • Try to compile

Version

10.13.3-transformer-api

What Atlas App Services are you using?

Local Database only

Are you using encryption?

No

Platform OS and version(s)

macOS Ventura 13.2.1

Build environment

Android Studio version: 2022.3.1 Canary 11
Android Build Tools version: 33.0.2
Gradle version: 8.0.2

Workarounds

Try using the embedded JDK from Android Studio Flamingo

  • Right-click the root folder of your project
  • "Open Module Settings"
  • Go to the "SDK Location" tab
  • Click on "Gradle Settings"
  • At the bottom next to "Gradle JDK", select the "Android Studio default JDK"
  • Click "OK" twice

Add a declaration to the global JVM arguments via gradle.properties

# gradle.properties
org.gradle.jvmargs = --add-opens=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED

Add a declaration to the Java compilation tasks

// app/build.gradle.kts:

android {
    io.realm.transformer.RealmTransformer.register(project)

    tasks.withType<JavaCompile>().configureEach {
        options.forkOptions.jvmArgs?.add("--add-opens=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED")
    }
}

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions