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

React Native项目构建失败求助:Metro与Gradle报错解决方案

React Native 构建问题解决方案

问题1:Metro Bundler 启动报错 ERR_OSSL_EVP_UNSUPPORTED

解决方案:

  • 临时修复(当前会话生效):
    修改项目根目录下package.json的start脚本,添加Node.js兼容参数:

    "scripts": {
      "start": "NODE_OPTIONS=--openssl-legacy-provider react-native start",
      // 保留其他原有脚本
    }
    

    修改后执行npm start即可启动Metro。

  • 持久修复(全局生效):

    • Windows:打开「控制面板→系统和安全→系统→高级系统设置→环境变量」,新增系统变量NODE_OPTIONS,值设为--openssl-legacy-provider,重启终端后生效。
    • Mac/Linux:在终端执行以下命令,将配置写入shell配置文件:
      echo 'export NODE_OPTIONS=--openssl-legacy-provider' >> ~/.zshrc
      source ~/.zshrc
      
      若使用bash,将.zshrc替换为.bashrc即可。

问题2:Gradle 构建失败,找不到 org.jetbrains.kotlin:kotlin-stdlib-jre7:1.7.20

解决方案:

Kotlin 1.7+版本已废弃kotlin-stdlib-jre7依赖,需替换为兼容版本:

  1. 打开android/build.gradle,确认并统一Kotlin版本配置:

    buildscript {
        ext {
            kotlinVersion = "1.7.20"
        }
        dependencies {
            classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
            // 移除任何引用kotlin-stdlib-jre7的配置
        }
    }
    
  2. 检查所有build.gradle文件(包括app模块下的),将所有implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlinVersion"替换为:

    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlinVersion"
    // Kotlin 1.8+版本推荐直接使用:
    // implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion"
    
  3. 清理缓存后重新构建:

    cd android
    ./gradlew clean
    cd ..
    npm run android
    

内容的提问来源于stack exchange,提问作者Aayush Singh

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.08 14:50:28