How to fix the ''module java.base does not "opens java.io" to unnamed module '' error in Android Studio?

2022-08-31 16:16:21

Background

  1. I started my first project in android studio to get familiar with Android programming.
  2. I am following this tutorial, in which
  • I created a new project Empty Activity, without any change
  • It is supposed to simulate a simple app that shows "Hello World" message

Problem Description But every time I try to run and build (I want to emulator as of now), I get the following error message.

Unable to make field private final java.lang.String java.io.File.path accessible:
Unable to make field private final java.lang.String java.io.File.path
accessible: module java.base does not "opens java.io" to unnamed module @42760a00

Here is my config set-up:

  1. Android Gradle plugin version: 4.2.1
  2. Gradle Version: 7.0.1 (changed to fix another issue I had while syncing the Gradle ("Gradle sync failed: Unsupported class file major version 60") and based discussion on this forum as quoted below)

Andrey Dernov commented 14 Apr 2021 00:18 Please use Gradle 7.0 or JDK less than 16 version for importgin and building the project (Settings (Preferences on macOS) | Build, Execution, Deployment | Build Tools | Gradle | Gradle JVM option).

  1. JDK: 16.0.1

Any suggestion or idea?


答案 1

The solution from GitHub has worked for me. It was no need to downgrade Java JDK. Just changed gradle version in gradle-wrapper properties to 7.1.1 (6.x does not support java 16), and adding the following line in gradle.properties:

org.gradle.jvmargs=-Xmx1536M --add-exports=java.base/sun.nio.ch=ALL-UNNAMED --add-opens=java.base/java.lang=ALL-UNNAMED --add-opens=java.base/java.lang.reflect=ALL-UNNAMED --add-opens=java.base/java.io=ALL-UNNAMED --add-exports=jdk.unsupported/sun.misc=ALL-UNNAMED

答案 2

You must be using a JDK version that is not supported by the Gradle Version. ( There is no need to downgrade )

1-Check your JDK Version from C:\Program Files\Java. In My Case, It's JDK-17.0.2

2-Check The Respective Gradle version for your JDK https://docs.gradle.org/current/userguide/compatibility.html

3-Open gradle-wrapper.properties from .\android\gradle\wrapper\ Screen Shot for gradle-wrapper.properties file and change the distributionUrl to your required gradle version

e.g. for JDK 17

distributionUrl = https\://services.gradle.org/distributions/gradle-7.3-all.zip

4-Open build.gradle from .\android\build.gradle classpath in build.gradle file and change the plugin in the class path to the one according to your gradle version

e.g.

classpath("com.android.tools.build:gradle:4.2.2") for JDK 6.71+
classpath("com.android.tools.build:gradle:7.0.0") for JDK 7+

Check the Compatible Plugin for your Gradle version at https://developer.android.com/studio/releases/gradle-plugin#updating-gradle

5-run npx react-native run-android


推荐