Android Studio编译报错:无法识别Java 11新增的Files.writeString与Files.readString方法
Got it, since you've already tried common fixes like invalidating caches, deleting .idea/.gradle folders, and letting Android Studio download its own Java 11, here are more targeted troubleshooting steps to resolve the writeString/readString compilation error:
Verify Project & Module Java Version Configurations
OpenFile > Project Structureand double-check these settings:- Under Project, confirm the
Project SDKis set to Java 11. - Navigate to Modules > [Your App Module] > Sources, set the
Language levelto11 (Lambda expressions, type annotations etc.)or higher. - Go to Modules > [Your App Module] > Dependencies, ensure the
Module SDKmatches either Java 11 or an Android API level that supports Java 11 features (API 30+).
- Under Project, confirm the
Update Module-level
build.gradleConfigurations
Make sure your app'sbuild.gradleexplicitly enables Java 11 compatibility:android { compileSdkVersion 34 // Use API 30 or higher for Java 11 support defaultConfig { // ... other configs } compileOptions { sourceCompatibility JavaVersion.VERSION_11 targetCompatibility JavaVersion.VERSION_11 } // If using Kotlin, add this to align JVM target kotlinOptions { jvmTarget = "11" } }Check Gradle Plugin & Wrapper Versions
Older Gradle versions lack full Java 11 support. Update your project-levelbuild.gradleto use a compatible plugin version (7.0+):buildscript { dependencies { classpath "com.android.tools.build:gradle:7.4.2" // Or a newer stable version } }Also, confirm the Gradle wrapper in
gradle/wrapper/gradle-wrapper.propertiesmatches:distributionUrl=https\://services.gradle.org/distributions/gradle-7.4.2-bin.zipValidate System Java Environment
Even if you removed Java 8, check if your system's environment variables are pointing to Java 11. Open Android Studio's Terminal and run:java -version javac -versionBoth should output Java 11.x. If not, update your
JAVA_HOMEandPATHto point to your Java 11 installation.Check Compile SDK Version Compatibility
Android only supports Java 11 language features when usingcompileSdkVersion 30or higher. Double-check this setting in your app'sbuild.gradle—using an older SDK version will block access to Java 11-specific methods.Test with a Fresh Project
Create a new empty Android project, configure it for Java 11, and paste your file I/O code. If it compiles successfully, the issue is likely tied to your original project's custom configurations, dependency conflicts, or corrupted module settings. You can then gradually migrate your original project's code/configs to the new project to isolate the problem.
内容的提问来源于stack exchange,提问作者Kokufuu

