ntsk

The state of a Gradle build process(daemon) may be corrupt.

June 06, 2021

インストールしたAndroid Studio 4.2.1 で既存プロジェクトを立ち上げたところ以下のエラーが発生し、ローカルのビルドは通るのだが、Android Studio経由のGradle Syncが通らなかった。

class jdk.internal.loader.ClassLoaders$PlatformClassLoader cannot be cast to class java.net.URLClassLoader (jdk.internal.loader.ClassLoaders$PlatformClassLoader and java.net.URLClassLoader are in module java.base of loader 'bootstrap')
class jdk.internal.loader.ClassLoaders$PlatformClassLoader cannot be cast to class java.net.URLClassLoader (jdk.internal.loader.ClassLoaders$PlatformClassLoader and java.net.URLClassLoader are in module java.base of loader 'bootstrap')

Gradle's dependency cache may be corrupt (this sometimes occurs after a network connection timeout.)

Re-download dependencies and sync project (requires network)
The state of a Gradle build process (daemon) may be corrupt. Stopping all Gradle daemons may solve this problem.

Stop Gradle build processes (requires restart)
Your project may be using a third-party plugin which is not compatible with the other plugins in the project or the version of Gradle requested by the project.

In the case of corrupt Gradle processes, you can also try closing the IDE and then killing all Java processes.

原因としては、Android Studio 4.2からJDK11がバンドルされるようになり、GradleビルドツールがバンドルされたJDKを利用しているためであった。

https://developer.android.com/studio/releases#4.2-bundled-jdk-11

Gradle compatibility and configuration changes

When running in Android Studio, the Gradle build tool uses Studio’s bundled JDK. In previous releases, JDK 8 was bundled with Studio. In 4.2, however, JDK 11 is now bundled instead. When using the new bundled JDK to run Gradle, this may result in some incompatibility or impact JVM performance due to changes to the garbage collector. These issues are described in the AGP release notes.

エラーとしてはbootstrapクラスローダーにてPlatformClassLoaderがURLClassLoaderへキャストができないというもの。

これは、Java9でのアーキテクチャの変更によるもので、PlatformClassLoaderはURLClassLoaderのインスタンスではなく、内部クラスのインスタンスとなっている。

https://docs.oracle.com/javase/9/migrate/toc.htm#JSMIG-GUID-A868D0B9-026F-4D46-B979-901834343F9E

このため、File > Project Structure > SDK Location > JDK location からJDK8を指定することで解決する。

opt

java8

Android Gradle Plugin 7.0.0-alpha02からはJava11が必須になるとのことなので、恒久的にはプロジェクト側もJDK11に対応するのが良いだろう。 https://developers-jp.googleblog.com/2020/12/announcing-android-gradle-plugin.html


© 2020-2024 ntsk.