如何在Android Studio中更新Java Runtime并解决代码变红问题
解决Android Studio代码全红+JRE版本过低问题
Hey there, let's tackle this problem step by step—code turning bright red in Android Studio can be super frustrating, especially when you’ve already gone through the hassle of updating your JRE. Let’s break down what might be going wrong and how to fix it:
1. First, confirm your system is actually using the new JRE
Sometimes you install a new JRE, but your system still defaults to the old one. Let’s check:
- Open your terminal/command prompt and run
java -version. - If the output still shows
1.8u92, you need to adjust your system’s environment variables to prioritize the new JRE:- Windows: Right-click "This PC" → Properties → Advanced System Settings → Environment Variables. Find
JAVA_HOMEand update its value to the path of your new JRE. Then check thePathvariable—make sure thebinfolder of your new JRE is listed before the old one. - macOS/Linux: Edit your shell config file (like
~/.bash_profileor~/.zshrc) and addexport JAVA_HOME=/path/to/your/new/jre(replace with your actual path). Then runsource ~/.bash_profile(or the corresponding file) to apply the change.
- Windows: Right-click "This PC" → Properties → Advanced System Settings → Environment Variables. Find
2. Force Android Studio to use the new JRE
Even if your system uses the new JRE, Android Studio might still be clinging to its own bundled JRE or an old path. Let’s fix that:
- Open Android Studio and go to
File → Project Structure → SDK Location. - Under
JDK location, click the browse button and select the path to your newly installed JRE (or JDK—since JDK includes JRE, either works). For example:- Windows:
C:\Program Files\Java\jre1.8.0_202 - macOS:
/Library/Java/JavaVirtualMachines/jdk1.8.0_202.jdk/Contents/Home
- Windows:
- Hit
ApplyandOKto save the setting.
3. Clear Android Studio’s cache and restart
Corrupted cache is a common culprit for weird code highlighting issues. Let’s wipe it:
- Close Android Studio completely.
- Navigate to the cache directory:
- Windows:
C:\Users\<YourUsername>\.AndroidStudio<Version>\system\caches - macOS:
~/Library/Caches/AndroidStudio<Version> - Linux:
~/.AndroidStudio<Version>/system/caches
- Windows:
- Delete all contents inside the
cachesfolder (you can delete the whole folder too—it’ll be recreated when you restart AS). - Reopen Android Studio and wait for it to reindex your project (this might take a minute or two).
4. Double-check your project’s JDK configuration
Sometimes the project itself is still set to use the old JRE:
- Go to
File → Project Structure → Project. - Make sure
Project SDKis set to your new 1.8u144+ JDK, andProject language levelmatches (select8 - Lambdas, type annotations etc.). - Head to the
Modulestab and verify that every module’sModule SDKis also set to the correct JDK version.
If all else fails:
- Close your project, delete the
.ideafolder and all*.imlfiles in your project root, then reimport the project into Android Studio. - Confirm your Android Studio version is compatible with the JRE (most modern AS versions work fine with 1.8u144+, but if your AS is several years old, you might need to either update AS or use a slightly older compatible JRE).
内容的提问来源于stack exchange,提问作者SBS




