realm / realm-java

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

An error occurred after migrating from JDK 8 to 11 in a multi-module Android application during kaptDebugKotlin process #7629

Closed mo0rych0k closed 2 years ago

mo0rych0k commented 2 years ago

How frequently does the bug occur?

All the time

Description

After migrating to JDK 11, I got an error in the kaptDebugKotlin process. I tried to update before the start of support JDK 11. I have the same problem. The error occurs in modules that are commonly used by kapt. These are the database description module and the object description module for the database.

Stacktrace & log output

e: File Object History : []
e: Open Type Names     : []
e: Gen. Src Names      : []
e: Gen. Cls Names      : []
e: Agg. Gen. Src Names : []
e: Agg. Gen. Cls Names : []

java.lang.IllegalStateException: failed to analyze: com.sun.tools.javac.code.ClassFinder$BadClassFile: bad class file: /Users/myname/.gradle/caches/transforms-3/16f04b7681ec0f770a70b977c942718c/transformed/jetified-realm-android-library-10.10.0-api.jar(/io/realm/MutableRealmInteger.class)
  unable to access file: java.nio.file.ClosedFileSystemException
  Please remove or make sure it appears in the correct subdirectory of the classpath.
    at org.jetbrains.kotlin.analyzer.AnalysisResult.throwIfError(AnalysisResult.kt:56)
    at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.compileModules$cli(KotlinToJVMBytecodeCompiler.kt:105)
    at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.compileModules$cli$default(KotlinToJVMBytecodeCompiler.kt:58)
    at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.doExecute(K2JVMCompiler.kt:170)
    at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.doExecute(K2JVMCompiler.kt:52)
    at org.jetbrains.kotlin.cli.common.CLICompiler.execImpl(CLICompiler.kt:92)
    at org.jetbrains.kotlin.cli.common.CLICompiler.execImpl(CLICompiler.kt:44)
    at org.jetbrains.kotlin.cli.common.CLITool.exec(CLITool.kt:98)
    at org.jetbrains.kotlin.daemon.CompileServiceImpl.compile(CompileServiceImpl.kt:1618)
    at jdk.internal.reflect.GeneratedMethodAccessor116.invoke(Unknown Source)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at java.rmi/sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:359)
    at java.rmi/sun.rmi.transport.Transport$1.run(Transport.java:200)
    at java.rmi/sun.rmi.transport.Transport$1.run(Transport.java:197)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at java.rmi/sun.rmi.transport.Transport.serviceCall(Transport.java:196)
    at java.rmi/sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:562)
    at java.rmi/sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:796)
    at java.rmi/sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$0(TCPTransport.java:677)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at java.rmi/sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:676)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
    at java.base/java.lang.Thread.run(Thread.java:829)
Caused by: com.sun.tools.javac.code.ClassFinder$BadClassFile: bad class file: /Users/myname/.gradle/caches/transforms-3/16f04b7681ec0f770a70b977c942718c/transformed/jetified-realm-android-library-10.10.0-api.jar(/io/realm/MutableRealmInteger.class)
  unable to access file: java.nio.file.ClosedFileSystemException
  Please remove or make sure it appears in the correct subdirectory of the classpath.
    at jdk.compiler/com.sun.tools.javac.jvm.ClassReader.badClassFile(ClassReader.java:310)
    at jdk.compiler/com.sun.tools.javac.jvm.ClassReader.readClassFile(ClassReader.java:2866)
    at jdk.compiler/com.sun.tools.javac.code.ClassFinder.fillIn(ClassFinder.java:359)
    at jdk.compiler/com.sun.tools.javac.code.ClassFinder.complete(ClassFinder.java:291)
    at jdk.compiler/com.sun.tools.javac.code.Symbol.complete(Symbol.java:642)
    at jdk.compiler/com.sun.tools.javac.code.Symbol$ClassSymbol.complete(Symbol.java:1326)
    at jdk.compiler/com.sun.tools.javac.code.Type$ClassType.complete(Type.java:1140)
    at jdk.compiler/com.sun.tools.javac.code.Type$ClassType.getTypeArguments(Type.java:1066)
    at jdk.compiler/com.sun.tools.javac.code.Type$ClassType.toString(Type.java:1025)
    at java.base/java.lang.String.valueOf(String.java:2951)
    at java.base/java.lang.StringBuilder.append(StringBuilder.java:168)
    at org.jetbrains.kotlin.utils.kapt.MemoryLeakDetector.inspectStatics(MemoryLeakDetector.kt:104)
    at org.jetbrains.kotlin.utils.kapt.MemoryLeakDetector.process(MemoryLeakDetector.kt:66)
    at org.jetbrains.kotlin.kapt3.AbstractKapt3Extension.runAnnotationProcessing(Kapt3Extension.kt:240)
    at org.jetbrains.kotlin.kapt3.AbstractKapt3Extension.analysisCompleted(Kapt3Extension.kt:194)
    at org.jetbrains.kotlin.kapt3.ClasspathBasedKapt3Extension.analysisCompleted(Kapt3Extension.kt:102)
    at org.jetbrains.kotlin.cli.jvm.compiler.TopDownAnalyzerFacadeForJVM$analyzeFilesWithJavaIntegration$2.invoke(TopDownAnalyzerFacadeForJVM.kt:112)
    at org.jetbrains.kotlin.cli.jvm.compiler.TopDownAnalyzerFacadeForJVM.analyzeFilesWithJavaIntegration(TopDownAnalyzerFacadeForJVM.kt:122)
    at org.jetbrains.kotlin.cli.jvm.compiler.TopDownAnalyzerFacadeForJVM.analyzeFilesWithJavaIntegration$default(TopDownAnalyzerFacadeForJVM.kt:96)
    at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler$analyze$1.invoke(KotlinToJVMBytecodeCompiler.kt:262)
    at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler$analyze$1.invoke(KotlinToJVMBytecodeCompiler.kt:53)
    at org.jetbrains.kotlin.cli.common.messages.AnalyzerWithCompilerReport.analyzeAndReport(AnalyzerWithCompilerReport.kt:113)
    at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.analyze(KotlinToJVMBytecodeCompiler.kt:253)
    at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.compileModules$cli(KotlinToJVMBytecodeCompiler.kt:100)
    ... 23 more

Can you reproduce the bug?

Yes, always

Reproduction Steps

Update to JDK 11 in multi-module app (i try only one app)

Version

10.10.0

What SDK flavour are you using?

Local Database only

Are you using encryption?

Yes, using encryption

Platform OS and version(s)

Mac OS

Build environment

Android Studio version: 2020.3.1 Putch 4 Android Build Tools version: 31.0.0 Gradle version: 7.0.2

sync-by-unito[bot] commented 2 years ago

➤ Clemente Tort Barbero commented:

Hi, we cannot reproduce the issue at our end. That bad class file error could mean that that file was compiled with a higher JDK version.

Have you tried clearing the gradle caches? Would you mind sharing your gradle configuration?

qq253682355 commented 2 years ago

我也是出现了,运行三四次就会出现一次,不是必现,但是频率会很高

storm1kk commented 2 years ago

Hi! We are getting the same error after jdk migration 8 -> 11.

Here is a piece of debug output with error itself:

`2022-02-17T09:05:43.7432990Z 2022-02-17T09:05:42.946+0000 [ERROR] [org.gradle.api.Task] e: java.lang.IllegalStateException: failed to analyze: com.sun.tools.javac.code.ClassFinder$BadClassFile: bad class file: /Users/runner/.gradle/caches/transforms-3/4ebdc31ab4f14b6ff67679e1fe6b3de4/transformed/jetified-realm-android-library-10.4.0-api.jar(/io/realm/MutableRealmInteger.class)
2022-02-17T09:05:43.7434200Z   unable to access file: java.nio.file.ClosedFileSystemException
2022-02-17T09:05:43.7434750Z   Please remove or make sure it appears in the correct subdirectory of the classpath.
2022-02-17T09:05:43.7435390Z    at org.jetbrains.kotlin.analyzer.AnalysisResult.throwIfError(AnalysisResult.kt:56)
2022-02-17T09:05:43.7436210Z    at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.compileModules$cli(KotlinToJVMBytecodeCompiler.kt:95)
2022-02-17T09:05:43.7437130Z    at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.compileModules$cli$default(KotlinToJVMBytecodeCompiler.kt:56)
2022-02-17T09:05:43.7437920Z    at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.doExecute(K2JVMCompiler.kt:169)
2022-02-17T09:05:43.7438580Z    at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.doExecute(K2JVMCompiler.kt:52)
2022-02-17T09:05:43.7439230Z    at org.jetbrains.kotlin.cli.common.CLICompiler.execImpl(CLICompiler.kt:92)
2022-02-17T09:05:43.7439870Z    at org.jetbrains.kotlin.cli.common.CLICompiler.execImpl(CLICompiler.kt:44)
2022-02-17T09:05:43.7440470Z    at org.jetbrains.kotlin.cli.common.CLITool.exec(CLITool.kt:98)
2022-02-17T09:05:43.7441110Z    at org.jetbrains.kotlin.daemon.CompileServiceImpl.compile(CompileServiceImpl.kt:1574)
2022-02-17T09:05:43.7441760Z    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
2022-02-17T09:05:43.7442450Z    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
2022-02-17T09:05:43.7443240Z    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
2022-02-17T09:05:43.7443920Z    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
2022-02-17T09:05:43.7444530Z    at java.rmi/sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:359)
2022-02-17T09:05:43.7445140Z    at java.rmi/sun.rmi.transport.Transport$1.run(Transport.java:200)
2022-02-17T09:05:43.7445720Z    at java.rmi/sun.rmi.transport.Transport$1.run(Transport.java:197)
2022-02-17T09:05:43.7446290Z    at java.base/java.security.AccessController.doPrivileged(Native Method)
2022-02-17T09:05:43.7446870Z    at java.rmi/sun.rmi.transport.Transport.serviceCall(Transport.java:196)
2022-02-17T09:05:43.7447530Z    at java.rmi/sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:562)
2022-02-17T09:05:43.7448940Z    at java.rmi/sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:796)
2022-02-17T09:05:43.7449690Z    at java.rmi/sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$0(TCPTransport.java:677)
2022-02-17T09:05:43.7450350Z    at java.base/java.security.AccessController.doPrivileged(Native Method)
2022-02-17T09:05:43.7450990Z    at java.rmi/sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:676)
2022-02-17T09:05:43.7451720Z    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
2022-02-17T09:05:43.7452420Z    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
2022-02-17T09:05:43.7453040Z    at java.base/java.lang.Thread.run(Thread.java:829)
2022-02-17T09:05:43.7454720Z Caused by: com.sun.tools.javac.code.ClassFinder$BadClassFile: bad class file: /Users/runner/.gradle/caches/transforms-3/4ebdc31ab4f14b6ff67679e1fe6b3de4/transformed/jetified-realm-android-library-10.4.0-api.jar(/io/realm/MutableRealmInteger.class)
2022-02-17T09:05:43.7455730Z   unable to access file: java.nio.file.ClosedFileSystemException
2022-02-17T09:05:43.7456290Z   Please remove or make sure it appears in the correct subdirectory of the classpath.
2022-02-17T09:05:43.7456930Z    at jdk.compiler/com.sun.tools.javac.jvm.ClassReader.badClassFile(ClassReader.java:310)
2022-02-17T09:05:43.7457630Z    at jdk.compiler/com.sun.tools.javac.jvm.ClassReader.readClassFile(ClassReader.java:2866)
2022-02-17T09:05:43.7458320Z    at jdk.compiler/com.sun.tools.javac.code.ClassFinder.fillIn(ClassFinder.java:359)
2022-02-17T09:05:43.7458990Z    at jdk.compiler/com.sun.tools.javac.code.ClassFinder.complete(ClassFinder.java:291)
2022-02-17T09:05:43.7459620Z    at jdk.compiler/com.sun.tools.javac.code.Symbol.complete(Symbol.java:642)
2022-02-17T09:05:43.7460270Z    at jdk.compiler/com.sun.tools.javac.code.Symbol$ClassSymbol.complete(Symbol.java:1326)
2022-02-17T09:05:43.7461030Z    at jdk.compiler/com.sun.tools.javac.code.Type$ClassType.complete(Type.java:1140)
2022-02-17T09:05:43.7461730Z    at jdk.compiler/com.sun.tools.javac.code.Type$ClassType.getTypeArguments(Type.java:1066)
2022-02-17T09:05:43.7462410Z    at jdk.compiler/com.sun.tools.javac.code.Type$ClassType.toString(Type.java:1025)
2022-02-17T09:05:43.7463000Z    at java.base/java.lang.String.valueOf(String.java:2951)
2022-02-17T09:05:43.7463560Z    at java.base/java.lang.StringBuilder.append(StringBuilder.java:172)
2022-02-17T09:05:43.7464220Z    at org.jetbrains.kotlin.utils.kapt.MemoryLeakDetector.inspectStatics(MemoryLeakDetector.kt:104)
2022-02-17T09:05:43.7464960Z    at org.jetbrains.kotlin.utils.kapt.MemoryLeakDetector.process(MemoryLeakDetector.kt:66)
2022-02-17T09:05:43.7465700Z    at org.jetbrains.kotlin.kapt3.AbstractKapt3Extension.runAnnotationProcessing(Kapt3Extension.kt:240)
2022-02-17T09:05:43.7466450Z    at org.jetbrains.kotlin.kapt3.AbstractKapt3Extension.analysisCompleted(Kapt3Extension.kt:194)
2022-02-17T09:05:43.7467220Z    at org.jetbrains.kotlin.kapt3.ClasspathBasedKapt3Extension.analysisCompleted(Kapt3Extension.kt:102)
2022-02-17T09:05:43.7468200Z    at org.jetbrains.kotlin.cli.jvm.compiler.TopDownAnalyzerFacadeForJVM.analyzeFilesWithJavaIntegration$invokeExtensionsOnAnalysisComplete(TopDownAnalyzerFacadeForJVM.kt:112)
2022-02-17T09:05:43.7469260Z    at org.jetbrains.kotlin.cli.jvm.compiler.TopDownAnalyzerFacadeForJVM.analyzeFilesWithJavaIntegration(TopDownAnalyzerFacadeForJVM.kt:122)
2022-02-17T09:05:43.7470260Z    at org.jetbrains.kotlin.cli.jvm.compiler.TopDownAnalyzerFacadeForJVM.analyzeFilesWithJavaIntegration$default(TopDownAnalyzerFacadeForJVM.kt:86)
2022-02-17T09:05:43.7471190Z    at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler$analyze$1.invoke(KotlinToJVMBytecodeCompiler.kt:252)
2022-02-17T09:05:43.7472080Z    at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler$analyze$1.invoke(KotlinToJVMBytecodeCompiler.kt:243)
2022-02-17T09:05:43.7472970Z    at org.jetbrains.kotlin.cli.common.messages.AnalyzerWithCompilerReport.analyzeAndReport(AnalyzerWithCompilerReport.kt:113)
2022-02-17T09:05:43.7474530Z    at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.analyze(KotlinToJVMBytecodeCompiler.kt:243)
2022-02-17T09:05:43.7475400Z    at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.compileModules$cli(KotlinToJVMBytecodeCompiler.kt:90)
2022-02-17T09:05:43.7476000Z    ... 24 more`

Any suggestions on the subject would be appreciated. JDK 11.0.14, Gradle 7.0.2, MacOS, Android build tools 31

Abnobery commented 2 years ago

If you are building multiple modules/tasks at once make sure to clean caches folder after each of them. Separate to multiple tasks for example.

mo0rych0k commented 2 years ago

@storm1kk I just recently successfully upgraded to Java 11. The cause of the problem is still not clear. I created a new project with identical package name. And imported all the modules and code into the application, including the gradle files.

edualonso commented 2 years ago

Hi @mo0rych0k

I just recently successfully upgraded to Java 11. The cause of the problem is still not clear.

Do you mean you have "duplicated" the project itself and upgraded to Java 11 and it worked?

mo0rych0k commented 2 years ago

@edualonso

Do you mean you have "duplicated" the project itself and upgraded to Java 11 and it worked?

Create new empty project on gradle Gradle 7.0.2 and java 11.

I guess the problem is in some gradle settings. Clearing the cache didn't work for me. I tried to move the module with database and check that it is a build is fine and only then transferred all the other modules.

storm1kk commented 2 years ago

If you are building multiple modules/tasks at once make sure to clean caches folder after each of them. Separate to multiple tasks for example.

You were right. We had a few tasks in one run so I divided them into multiple runs and it works. Thank you!

edualonso commented 2 years ago

Based on your information I suspect it had to do with how your gradle scripts were configured. Do you think you could somehow calculate the diff between your gradle configuration scripts before and after fixing the problem? It would be highly beneficial for us to further expand our knowledge base. I will close the issue but you are more than welcome to post your findings here.

storm1kk commented 2 years ago

Based on your information I suspect it had to do with how your gradle scripts were configured. Do you think you could somehow calculate the diff between your gradle configuration scripts before and after fixing the problem? It would be highly beneficial for us to further expand our knowledge base. I will close the issue but you are more than welcome to post your findings here.

I didn't touch Gradle configuration scripts. I just split one-in-all pipeline into multiple. Like it was: "./gradlew assembleApp1 assembleApp2 assembleApp3"; It is now: "./gradlew assembleApp1", "./gradlew assembleApp2", "./gradlew assembleApp3". Thus I avoid Gradle cache trashing with previous builds. Anyway I'm wondering why it was working with java 8 :)

DamienDennehy commented 2 years ago

We had also encountered this issue. Our Android app was already on Java 11 to start with. We use GitHub Actions for our builds. We used both Ubuntu and Windows to test this issue.

Ubuntu Error

Caused by: com.sun.tools.javac.code.ClassFinder$BadClassFile: bad class file: /home/runner/.gradle/caches/transforms-3/651ef93de3eb908829a6084972084d66/transformed/jetified-realm-android-library-object-server-10.10.1-api.jar(/io/realm/MutableRealmInteger.class)
  unable to access file: java.nio.file.ClosedFileSystemException
  Please remove or make sure it appears in the correct subdirectory of the classpath.

Windows Error

e: java.lang.IllegalStateException: failed to analyze: com.sun.tools.javac.code.ClassFinder$BadClassFile: bad class file: C:\Users\runneradmin\.gradle\caches\transforms-3\615f9dae86d0ed4bf2f329c4b7f5c328\transformed\jetified-realm-android-library-object-server-10.10.1-api.jar(/io/realm/MutableRealmInteger.class)
  unable to access file: java.nio.file.ClosedFileSystemException
  Please remove or make sure it appears in the correct subdirectory of the classpath.

We tried about a dozen different ways to fix this including building each module individually, but nothing worked.

Our original command: gradlew assembleRelease

We changed to: gradlew assembleRelease -p libraries/library-name_goes_here

We also tried: gradlew assembleRelease -p libraries/library-name_goes_here --no-build-cache

Any other ideas would be appreciated, thanks.

EDIT: we were able to partially resolve this it seems. It wasn't modules - it was flavors. We have 4 flavors (dev,qa,uat,prod) and we split our gradle command from "gradlew assembleRelease" to

gradlew assembleDevRelease gradlew assembleQaRelease gradlew assembleUatRelease gradlew assembleProdRelease

Our build time has increased a lot though. And we still get failures. It's almost as if there's a multiplier factor going on here - your chances of seeing this error are very low, but the more modules or flavors you have increases the odds of this happening.