You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React Native构建报错:无法打开settings通用类缓存及类版本不兼容

React Native构建报错:Unsupported class file major version 64

问题详情

构建React Native项目时反复报错,已尝试更换JDK 8、11、17、20版本,删除android/文件夹重建,均未解决,且问题表现与其他同报错用户存在差异。

报错信息

无法打开settings文件'C:\Users\Benj\projects\TikTokClone\android\settings.gradle'的通用类缓存(路径:C:\Users\Benj.gradle\caches\7.5\scripts\e34934ll5fugwo6b0s7f5a19r),并出现异常:BUG! exception in phase 'semantic analysis' in source unit 'BuildScript' Unsupported class file major version 64。

相关配置文件

android/build.gradle

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    ext {
        buildToolsVersion = "31.0.0"
        minSdkVersion = 21
        compileSdkVersion = 31
        targetSdkVersion = 31
    }
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath("com.android.tools.build:gradle:7.0.3")
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        mavenLocal()
        maven {
            // All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
            url("$rootDir/../node_modules/react-native/android")
        }
        maven {
            // Android JSC is installed from npm
            url("$rootDir/../node_modules/jsc-android/dist")
        }

        google()
        jcenter()
        maven { url 'https://www.jitpack.io' }
    }
}

android/gradle/wrapper/gradle-wrapper.properties

distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-all.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists

android/settings.gradle

rootProject.name = 'TikTok'
apply from: file("../node_modules/@react-native-community/cli-platform-android/native_modules.gradle"); applyNativeModulesSettingsGradle(settings)
include ':app'

解决步骤

  • 核心原因:Unsupported class file major version 64对应JDK 20的类文件版本,而你使用的Gradle 7.5最高仅支持JDK 17;同时Android Gradle Plugin(AGP)7.0.3与Gradle 7.5存在兼容性冲突。
  1. 固定JDK为17:

    • 在项目android/目录下创建local.properties文件,添加:
      org.gradle.java.home=C:\\Program Files\\Java\\jdk-17.x.x
      
    • 或确保系统环境变量JAVA_HOME指向JDK 17的安装路径。
  2. 修复Gradle与AGP版本兼容:
    方案一:降级Gradle到7.4(适配AGP 7.0.3)
    修改android/gradle/wrapper/gradle-wrapper.properties:

    distributionUrl=https\://services.gradle.org/distributions/gradle-7.4-all.zip
    

    方案二:升级AGP到7.3.1(适配Gradle 7.5)
    修改android/build.gradle中的依赖:

    classpath("com.android.tools.build:gradle:7.3.1")
    
  3. 清理缓存:删除C:\Users\Benj\.gradle\caches目录下的所有文件,避免旧缓存残留。

  4. 重新构建:执行./gradlew clean后,重新运行项目构建命令。

内容的提问来源于stack exchange,提问作者Benjamin Ikwuagwu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.23 01:10:19