You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何在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_HOME and update its value to the path of your new JRE. Then check the Path variable—make sure the bin folder of your new JRE is listed before the old one.
    • macOS/Linux: Edit your shell config file (like ~/.bash_profile or ~/.zshrc) and add export JAVA_HOME=/path/to/your/new/jre (replace with your actual path). Then run source ~/.bash_profile (or the corresponding file) to apply the change.

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
  • Hit Apply and OK to 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
  • Delete all contents inside the caches folder (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 SDK is set to your new 1.8u144+ JDK, and Project language level matches (select 8 - Lambdas, type annotations etc.).
  • Head to the Modules tab and verify that every module’s Module SDK is also set to the correct JDK version.

If all else fails:

  • Close your project, delete the .idea folder and all *.iml files 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

火山引擎 最新活动