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

30

273

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?

Kurtis answered 1/6, 2021 at 5:3 Comment(0)
C
335

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
Construction answered 14/9, 2021 at 14:50 Comment(13)
Thanks, man! it works for me, I just copied the code and paste it to android/gradle.propertiesSoldier
It worked for me too. But could you explain what exactly this line do?Anisotropic
it was not the correct answer, though it gives me and idea to change the gradle class path, and viola it workedBellebelleek
For ButterKnife compiler, also add --add-exports=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED --add-exports=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED --add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMEDSabinesabino
Worked for me after changing properties to 7.3.3 and making the change in jvmargsShelves
Work also for Mac M1Stampede
its worked for me. i have to some changes in AndroidManifest.xml too. like in intent filter add properties android:exported="true". ThanksGrouping
@Construction It worked but I want to know what caused the problem.Flower
It worked and I don't care what caused the problem!Unbelt
Worked, really useful answer (should be the top answer).Derisible
This worked for me, but I really want to know why. Can anyone explain this?Fiorin
You can add the same vm options to spring boot config in Idea and it will work.Gink
i pasted it as it is, and it worked like a charmDistributee
F
196

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 Gradle 6.7.1+
classpath("com.android.tools.build:gradle:7.0.0") for Gradle 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

Fabric answered 21/1, 2022 at 21:8 Comment(5)
Great answer, Thanks, You need to update some numbers and it will be up-to-dateRichelle
In the last code block, did you mean Gradle 6.7.1+ and Gradle 7+?Limeade
@Limeade Yes it's Gradle, bad I thought I fixed it. Updated!Fabric
any update to this answer since JDK has been replaced with JBR now?Hubert
I have no C:\Program Files\Java directoryAlkene
T
43

I think I found the solution.

If you are importing an old project you are likely to face this error.

Basically you made your old project with lower JDK versions and now you have higher JDK versions installed currently in your system.

You should avoid higher JDK versions for building older projects.

  1. Basically Download JDK 8 (1.8) and install it.
  2. change environment variable (write JDK 8 path to environment variable)
  3. Then change your project structure and write JDK 8 path to JDK location.

enter image description here

  1. invalidate caches and restart android studio.
  2. build your project and it should work.

If you got the following error:

Error:Cannot fit requested classes in a single dex file.Try supplying a main-dex list. # methods: 72477 > 65536

Then add the following dependency in your app.gradle file:

 // multidex
implementation 'com.android.support:multidex:1.0.3'

then you need to enable multidex in your app.gradle file.

android {
compileSdkVersion 30

defaultConfig {
    applicationId "com.convenient.easymeasure"
    minSdkVersion 19
    targetSdkVersion 30
    versionCode 1
    versionName "1.0"
    multiDexEnabled true

    testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
Tourer answered 5/7, 2021 at 16:10 Comment(0)
P
26

has worked for me in Macbook Air M1 Apple Silicon. you no need to downgrade or uninstall Java JDK.

in my case Just changed gradle version in gradle-wrapper.properties to 7.2 like this :

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

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

related solution on github

Possibly answered 20/12, 2021 at 1:34 Comment(0)
L
16

Add this line in your gradle.properties file.

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
Liston answered 25/8, 2023 at 7:7 Comment(1)
it's important to note that using these flags might expose your application to potential security risks or compatibility issues. It's recommended to understand the implications and risks associated with modifying JVM behavior and only use these configurations if necessary to resolve specific issues. Additionally, consider updating your code or dependencies to adhere to best practices and avoid relying on these flags for normal operations.Tobi
B
11

Run Flutter doctor, if there´s an error indicating that the java bundled can´t be found (specially if you have Android Studio version Artic Fox), run this commands if using Mac:

  1. cd /Applications/Android\ Studio.app/Contents/jre
  2. ln -s ../jre jdk
  3. ln -s "/Library/Internet Plug-Ins/JavaAppletPlugin.plugin" jdk
Bradway answered 31/8, 2021 at 3:9 Comment(4)
I was fighting with Android Studio Artic Fox (for 2 days) to build and run an android emulator. Flutter doctor was all good. I tried JDK 11, 15, 16. I tried adding variables to .bash_profile. I installed and tried older emulators. Nothing was working. Your commands worked. Can you explain why Andrey?Folly
@Folly the reason is because the Artic Fox version have a different location for the java library, the commands from above is for giving the correct path. This error usually happens when you come from an older version of Android Studio to Artic FoxBradway
This should be the accepted answer. If on mac, and after updating Android Studio to Arctic Fox 2020.3.1 (patch 4) from the previous version (4.2.2)Abscind
Same here, I was wasting my time installing and changing versions of Java. - macOS Ventura. - Android Studio Dolphin (2021.3.1).Coronet
C
11

In my case the following steps did the trick:

  1. Changing the gradle verion in gradle-wrapper.properties to:
    distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.3-all.zip
    
  2. Changing build gradle and google services classpath version in build.gradle to:
    dependencies {
        classpath 'com.android.tools.build:gradle:7.2.1'
        classpath 'com.google.gms:google-services:4.3.5'
    }
    
Colliery answered 16/6, 2023 at 15:32 Comment(0)
T
8

I was able to fix it by upgrading grade project settings using Android Studio which replaced classpath("com.android.tools.build:gradle:4.2.2") with classpath('com.android.tools.build:gradle:7.0.3') in android/build.gradle

Tahsildar answered 30/1, 2022 at 4:39 Comment(1)
This replacement solved my issue in my Flutter project.Mauriac
J
6

Downgrade your Java version.It works for me

enter image description here

Jenna answered 21/12, 2023 at 7:17 Comment(3)
I work on multiple react-native projects. one project requires Java 17 and another one Java 11. when I used Java 17 for the project which actually requires Java 11 then i faced this error. When I reverted back to Java 11 then issue resolved.Renz
@Renz Yes! I was also facing this error when I used java 17 Jenna
How am I supposed to know what version to downgrade to?Brewhouse
M
5

I had the same issue with a Flutter 2 project after updating Android Studio to electric eel. I was still able to build the android project, but flutter run on the Flutter project would cause this issue. lortschi's answer was the key, but it was missing one more entry to make it work, so my final gradle.properties looks like this now:

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 \
--add-opens=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED
android.useAndroidX=true
android.enableJetifier=true

I was very skeptical, but it worked.

Multiparous answered 15/1, 2023 at 22:45 Comment(0)
G
5

For those having these issues with the latest Android Studio - Electric Eel version, and other canaries and preview releases, note that the bundled jre directory in the Android Studio installation folder is now renamed to jbr

(This answer came from eja's answer on this question.)

To resolve this, just create a sym link jre -> jbr and Flutter won't complain.

On Linux

cd ~/android-studio/ && ln -s jbr jre

Windows (check installation folder)

cd C:\Program Files\Android\Android Studio
mklink /D "jre" "jbr"

or

New-Item -ItemType SymbolicLink -Path .\jre -Target .\jbr

Mac OS

cd /Applications/Android\ Studio.app/Contents
ln -s jbr jre

Note that if you are running a preview release, the default installation directory might be different, e.g. on Linux it would be

~/android-studio-preview/
Goldengoldenberg answered 23/1, 2023 at 16:52 Comment(0)
M
5

This is a known issue by Gradle when running tests with Java 17 with Test Distribution enabled

Add this to your build.gradle (.kts version available at the link below)

tasks.named('test', Test) {
    jvmArgs(
        '--add-opens', 'java.base/java.lang=ALL-UNNAMED',
        '--add-opens', 'java.base/java.util=ALL-UNNAMED'
    )
}

See https://docs.gradle.com/enterprise/test-distribution/#accessing_jdk_internal_classes_from_your_tests

See original github issue https://github.com/gradle/gradle/issues/18647

Marden answered 28/7, 2023 at 8:1 Comment(1)
Replace 'test' with the name of your task. If you have to open java.io, add it to the jvmArgs list. If you have more than one test task (for example, for multiple build variants) you can use tasks.withType(Test) instead of tasks.named.Rota
H
5

In my case, my error was happened when I'm using:

Gradle version = 6.5 & Gradle JDK version = 17.0.6 in Android Studio Giraffe (Stable)

You could check the Gradle version in the gradle-wrapper.properties file, on this line:

distributionUrl=https\://services.gradle.org/distributions/gradle-6.5-bin.zip

And check JDK version on:

File > Settings > Build, Execution, Deployment > Build Tools > Gradle > Gradle JDK

To fix this error, you need to pick JDK version that is compatible with your Gradle Version. You could check the compatibility in here:

https://developer.android.com/build/releases/gradle-plugin#updating-gradle

I pick JDK 11.0.18 for Gradle version 6.5 (a lower JDK version compared to my current JDK)

In summary, choosing the compatible JDK version is the answer for this error.

Hypertensive answered 5/8, 2023 at 14:57 Comment(1)
One more option: do an AGP update via AGP upgrade assistance.Harvest
P
4

I downloaded 2 JDK jdk-17.0.1 and jdk-11.0.13 and add below line on gradle.properties

org.gradle.java.home=C\:\\Program Files (x86)\\Java\\jdk-11.0.13

Also go to Android Studio (Arctic Fox)-> Project Structure -> Gradle setting and choose JDK version

Punjab answered 25/12, 2021 at 14:58 Comment(1)
Did every other possible solution mentioned none helped, only this! Thanks!Sapele
R
3

If you are importing an old project you may get this error

  1. Download JDK 8 (1.8) and install it.
  2. Change your project structure and write JDK 8 path to the JDK location.

some screenshots for reference (Android Studio Flaming version) (sorry, couldn't em images)

file -> project structure

SDK location -> Gradle Settings

Gradle JDK choose download JDK

Choose version 1.8

After download choose gradle jdk to 1.8

Rheum answered 26/6, 2023 at 13:17 Comment(2)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Burp
I thought your answer was clear. Helped me, at least. Sometimes you just need some hand-holding with screenshots to sort things out. Thanks!Unexperienced
O
3

1.ext.kotlin_version = '1.8.0'

2.replace this classpath 'com.android.tools.build:gradle:7.4.2' with your build gradle version in dependencies.

3.replace distributionUrl=https://services.gradle.org/distributions/gradle-7.5-all.zip in gradle wrapper properties.

Overdraw answered 23/9, 2023 at 11:1 Comment(0)
M
2

Installing JDK 1.8.0 and setting JAVA_HOME path to JDK 1.8.0's folder in environment variables worked for me.

Marbleize answered 17/3, 2022 at 10:16 Comment(1)
JDK 1.8 no longer has active support, it only has security support (for a little while longer) and I would not recommend using it anymore. There are newer versions available. See also endoflife.date/javaMondrian
P
2

Had same problem with Flutter. I uninstalled Android Studio and Android SDK and reinstalled everything and upgraded Flutter, without success.

Then I created a new Flutter project and copied the sources from the old one, and this time it worked.

Populace answered 18/3, 2022 at 8:49 Comment(0)
P
2

This is a known issue when using Gson and reflection for parsing Json. Updating your Android Gradle Plugin version to 7.0.0+ and the error should go away.

Pocked answered 3/6, 2022 at 15:3 Comment(0)
O
2

To Flutters, just by deleting Android folder and running flutter create . on the project root fix it as well

Olia answered 22/9, 2023 at 18:34 Comment(1)
Dude, thanks !! This solution worked for meDrank
S
2

I had the same problem at java 17, when I try to connect on JMS queue. It was solved adding the follow JVM options:

--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

In my case, just one line was enough:

--add-opens=java.base/java.io=ALL-UNNAMED 
Silden answered 14/12, 2023 at 12:49 Comment(0)
U
2

enter image description here

CHange the Gradle version of your project. It's worked for me

Unwise answered 30/1 at 6:22 Comment(1)
This answer is correct. I upgrade Gradle plugin version and it works.Malisamalison
D
1

I also face this issue in Android Studio after Migrating Java JDK 17

tasks.withType(Test) {
    jvmArgs('--add-opens=java.base/java.lang=ALL-UNNAMED')
}

https://docs.gradle.com/enterprise/test-distribution/#accessing_jdk_internal_classes_from_your_tests

Disoblige answered 6/11, 2023 at 8:5 Comment(2)
In which gradle file we have to add this exactly? app? module?Bilocular
Project build.grade file not app model build.gradle fileDisoblige
N
0

You must be using JDK-16, use the version below JDK-16, in my case, I downgrade to JDK-11 and it worked for me.

Nuli answered 9/9, 2021 at 17:28 Comment(1)
Downgrading only delays problems, it doesn't solve them.Mondrian
C
0

Just added from another solution

If you have later version installed, and your project is using older version. Don't forget to check in terminal which jdk is used.

In my case my project using JDK 15. And terminal using 18. So i uninstall the other version and leaving the proper version (in my mac, just delete another jdk directory in /Libray/Java/JavaVirtualMachine)

Chaulmoogra answered 5/9, 2022 at 3:12 Comment(0)
A
0

Windows user:

Go to C:\Users\arpan.gradle\caches and delete all the gradle cache.

then,

Go to "C:\Program Files\Android\Android Studio" there you will see jbr and jre folder then copy the content of jbr and paste the content into jre folder. ss: enter image description here and run flutter doctor again and run your App.

Affidavit answered 30/1, 2023 at 15:34 Comment(0)
N
-1

run

sudo update-alternatives --config 

java and select a minor version of java .

Numb answered 16/3, 2023 at 17:31 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Burp
D
-1

Steps To Solve

1. Gradle.properties

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

2. Java version

choose java version 17 or any compatiable

3. Gradle Project

choose kotlin version and gradle miniume version


buildscript {
    ext.kotlin_version = '1.7.10'
    repositories {
        google()
        mavenCentral()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:7.2.0'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}
Dosage answered 19/4, 2023 at 10:36 Comment(1)
This isn't helpful in any possible way.Ballinger
C
-2

Check if JDK is installed or not configure your SDK also and try again you can download JDKfrom Java SE

Carouse answered 1/6, 2021 at 6:21 Comment(1)
Yes they are both set-up and configured in project structure sectionKurtis
F
-5

Note: this is basically a solution for old project to run is successfully.

After I updated jdk to 18 then I got the error mentioned in the question. So I downgraded to jdk11, and this fixed my issue

To install jdk11 please run this command in windows 10: choco install openjdk11. Hope this will fix your problem.

To run the above command you might have to install chocolatey and add it to path environment variable. Chocolatey is a dependency manager for windows 10.

Here is link to a guide:

https://community.chocolatey.org/packages/openjdk11

Feebleminded answered 22/5, 2022 at 7:14 Comment(3)
You mention both jdk 18 and 11 here, that's a bit confusing?Mondrian
@SimonForsberg I have updated answer hope this solve your problem. more clearly you need to install jdk 11Feebleminded
Downgrading does not seem like a good idea.Mondrian

© 2022 - 2024 — McMap. All rights reserved.