47

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?

Ashkanxy
  • 1,012
  • 1
  • 3
  • 13

14 Answers14

75

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
lortschi
  • 1,250
  • 1
  • 11
  • 11
  • Thanks, man! it works for me, I just copied the code and paste it to android/gradle.properties – Jesus Erwin Suarez Feb 12 '22 at 01:56
  • It worked for me too. But could you explain what exactly this line do? – Arthur Pereira Mar 28 '22 at 18:30
  • 1
    it was not the correct answer, though it gives me and idea to change the gradle class path, and viola it worked – Lakhwinder Singh Mar 31 '22 at 08:34
  • 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-UNNAMED` – Miha_x64 May 23 '22 at 18:38
28

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

usamayaseen
  • 280
  • 3
  • 5
16

I found this discussion kind of related to my question. But could not figure out how to pass JVM parameters in Android studio.

But, the error is gone after replacing JDK 16 by JDK 15.

Ashkanxy
  • 1,012
  • 1
  • 3
  • 13
  • 4
    I also fix the issue by replacing jdk 16 to 1.8. – Hill Jun 24 '21 at 07:57
  • 35
    Downgrading JDK is not a solution. – Rohit Gaikwad Nov 03 '21 at 07:15
  • @RohitGaikwad Why? – reducing activity Jan 15 '22 at 16:40
  • @reducingactivity, At first you should ask your AppSecurity Team whether downgrading is an accepted solution? They should never accept this. In enterprise applications all 3rd party softwares should move to latest versions only(For getting official support, new features and to avoid migration overhead (If you don't move from JDK15 to higher a version, then one day you will be forced to do so)). Moreover, Ask yourself just due to this one modularization issue why the enterprise should move back to JDK15? Looks like this is just a POC application. – Rohit Gaikwad Jan 16 '22 at 02:44
  • 2
    @RohitGaikwad I am not working in corporation with some dumb ban on downgrading. "They should never accept this." there are sometimes good reason for downgrading, including critical security issues. Blanket ban on downgrading is a typical corporation bureaucracy red tape. And that assumes that developers are unable to control their own machines, which is not true even in all corporations. Blanket "is not a solution" claim is not true. – reducing activity Jan 16 '22 at 07:14
  • @reducingactivity. Okay go for it, happy journey!!! Also, make sure you downgrade your other 3rdparty libraries example like logging from log4j2.x to 1.x!, If your developers encounters any blocking issues with logging. Moreover, What's the exact justification/root cause to downgrade the JDK? This needs to be bring on the table at first. – Rohit Gaikwad Jan 16 '22 at 11:24
  • @RohitGaikwad "What's the exact justification/root cause to downgrade the JDK?" "Android Studio is broken with it" is pretty compelling reason when one makes Android app :) "If your developers" - I do not have developers, I am developer. – reducing activity Jan 16 '22 at 12:30
  • @reducingactivity "Android Studio is broken" is not a root cause its an issue you are facing during development with JDK16. You need to justify what exact change in JDK16 have broken this. Check JDK16 release notes/Bug fix report for any changes made in the area you think is causing this regression. If your company have a paid support raise a bug with Oracle along with a POC which results into this exception. Also, Try with JDK17 it should be a better option then to downgrade as its a LTS and JDK15/16 are not LTS. The solution to upgrade Gradle looks good to me, find such issues at your end. – Rohit Gaikwad Jan 16 '22 at 13:18
11

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"
}
Meraj
  • 181
  • 7
7

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
Andrey
  • 2,033
  • 1
  • 23
  • 44
  • 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? – lopes710 Oct 23 '21 at 21:13
  • 2
    @lopes710 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 Fox – Andrey Oct 24 '21 at 14:43
  • 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) – derZorn Jan 10 '22 at 11:16
4

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

3

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.

tread khuram
  • 167
  • 1
  • 5
1

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

Dheeraj Gupta
  • 138
  • 10
1

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

shubomb
  • 484
  • 6
  • 20
1

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

Kewal Shah
  • 911
  • 13
  • 26
1

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

uvx4040
  • 11
  • 2
1

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.

Jean G
  • 117
  • 1
  • 9
0

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.

mbonnin
  • 6,500
  • 3
  • 34
  • 54
-1

I updated jdk to 18 and then I got this error. After running this command choco install openjdk11 it fixed my problem. fot this yooou have to install chocolatey

Here is the link https://community.chocolatey.org/packages/openjdk11

Engr.Aftab Ufaq
  • 1
  • 1
  • 9
  • 30