0

I am getting a compile error in the following code in Android Studio.

Path fileName = Path.of("demo.txt");
String content  = "hello world !!";
Files.writeString(fileName, content);
String actual = Files.readString(fileName);

As 'writeString' and 'readString' were instroduced in Java 11, my first guess was it has something to do with that.
I'm using the latest version of Android Studio and have Java 11 installed. Used to have Java 8 as well but I have deleted that already. I'm certain it is just some setting issue or some stucked cache stuff as in IntelliJ it works fine.

I have tried:

  • invalidate cache and restart
  • removing the .idea and .gradle folders
  • I also let Adroid Studio to download Java 11 for itself and not using the one specified by me.

Any guesses?
Thanks in advance!

Kokufuu
  • 145
  • 1
  • 2
  • 7

1 Answers1

0

What Java runtime you have installed on your PC is not relevant. The Java app code is compiled using the JRE API from Android.jar of the targetSDK you have selected in build.gradle in your project. And even more important for executing a method it has to be present in the framework.jar installed on the device you execute your app (which is part of the Android OS).

Using latest Android Studio you can use Java 11 code features but the Android API is still not on the same level as Java 11, therefore you can't use Java 11 specific methods.

Please look at the Android Java API documentation of java.nio.Files which API methods you can use in an Android app.

Robert
  • 39,162
  • 17
  • 99
  • 152