8

I get this failure both in Android Studio and running ./gradlew assembleDebug on the command line on a new Macbook with Apple M1 Max chip. On my old Intel Mac there are no problems building the project.

> Task :app:kaptGenerateStubsDebugKotlin FAILED
e: java.lang.IllegalAccessError: class org.jetbrains.kotlin.kapt3.base.KaptContext (in unnamed module @0x4d1ecff7) cannot access class com.sun.tools.javac.util.Context (in module jdk.compiler) because module jdk.compiler does not export com.sun.tools.javac.util to unnamed module @0x4d1ecff7
        at org.jetbrains.kotlin.kapt3.base.KaptContext.<init>(KaptContext.kt:28)
        at org.jetbrains.kotlin.kapt3.KaptContextForStubGeneration.<init>(KaptContextForStubGeneration.kt:40)
        at org.jetbrains.kotlin.kapt3.AbstractKapt3Extension.contextForStubGeneration(Kapt3Extension.kt:287)
        at org.jetbrains.kotlin.kapt3.AbstractKapt3Extension.analysisCompleted(Kapt3Extension.kt:171)
        at org.jetbrains.kotlin.kapt3.ClasspathBasedKapt3Extension.analysisCompleted(Kapt3Extension.kt:102)

(Here's the full error output)

class org.jetbrains.kotlin.kapt3.base.KaptContext (in unnamed module @0x4d1ecff7) cannot access class com.sun.tools.javac.util.Context (in module jdk.compiler) because module jdk.compiler does not export com.sun.tools.javac.util to unnamed module @0x4d1ecff7

Googling around, the error message looks a lot like the one here: "Kapt is not compatible with JDK 16+". But thing is, I'm using JDK 11. (Same result on both Android Studio's default JDK (11.0.10) and "11.0.13 2021-10-19 LTS" downloaded from www.oracle.com.)

Any ideas how to get this working on the M1 Mac?

Jonik
  • 80,077
  • 70
  • 264
  • 372
  • 1
    The next day, without having changed anything, the build is now working in both Android Studio and on the command line. ‍♂️ – Jonik Jan 13 '22 at 16:26
  • If it wasn't working, the next thing I thought of trying would be replacing [kapt](https://kotlinlang.org/docs/kapt.html) with [KSP](https://kotlinlang.org/docs/ksp-overview.html) as suggested on kapt website. kapt is used for Moshi in this project and KSP should support that. – Jonik Jan 13 '22 at 16:28
  • I got the same exact issue. I just started using M1 mac today. I have the same idea with replacing kapt with KSP but dagger/hilt is not yet supported. Also room is still in experimental stage. The issue is still persistent as of the moment. I am also using the Android Studio's default JDK. – Jake Jan 21 '22 at 07:14
  • 4
    So I just restarted my machine and it magically fixed everything – Jake Jan 21 '22 at 07:25
  • 1
    I haven't seen this problem after upgrading to kotlin 1.5.32 (though I haven't tested robustly), which has the fix for [the JDK 16+ issue](https://youtrack.jetbrains.com/issue/KT-45545) linked above. For Android, this required me to suppress the Compose Compiler version warning though. Like Jake, I also found restarting fixed the issue temporarily. fwiw, I reproduced this on an Intel MBP, not an M1. – mcomella Jan 27 '22 at 19:06
  • https://stackoverflow.com/questions/69541831/unknown-host-cpu-architecture-arm64-android-ndk-siliconm1-apple-macbook-pro/69555276#69555276 have a look at this post maybe it may help you. – Atif AbbAsi Mar 02 '22 at 07:59

3 Answers3

5

From the link mcomella has shared - https://youtrack.jetbrains.com/issue/KT-45545, they have fixed it in Kotlin 1.6.x.

So bumping to Kotlin 1.6.x has worked for my case (Apple M1 Pro).

Wei Yuan
  • 301
  • 2
  • 13
4

As @Jake wrote, restarting the computer somehow fixed the issue.

Wojtek
  • 1,210
  • 3
  • 18
  • 23
0

I solve it in my MacBook Air M1 updating the Kotlin version and all other library for the last version, you have to update the compileSDKVersion and targetSDKVersion for the latest version as well.

Desilio Neto
  • 484
  • 5
  • 4