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

React Native从0.74.3升级到0.75.0后Android构建失败——native_modules.gradle报错

React Native 0.74.3升级到0.75.0后Android构建失败问题解决

问题描述

将React Native项目从0.74.3升级到0.75.0后,Android端执行任何Gradle命令(如./gradlew clean)均会触发构建失败,iOS端无异常。报错核心信息如下:

FAILURE: Build failed with an exception.

* Where:
Script '/Users/mehmetsozd/Desktop/almiratur/almira_app/node_modules/@react-native-community/cli-platform-android/native_modules.gradle' line: 418

* What went wrong:
Could not compile script '/Users/mehmetsozd/Desktop/almiratur/almira_app/node_modules/@react-native-community/cli-platform-android/native_modules.gradle'.
> startup failed:
  script '/Users/mehmetsozd/Desktop/almiratur/almira_app/node_modules/@react-native-community/cli-platform-android/native_modules.gradle': 418: [Static type checking] - No such property: inputStream for class: java.lang.Process
   @ line 418, column 31.
               def output = process.inputStream.text.trim()
                                   ^
  
  script '/Users/mehmetsozd/Desktop/almiratur/almira_app/node_modules/@react-native-community/cli-platform-android/native_modules.gradle': 418: [Static type checking] - No such property: text for class: java.lang.Object
   @ line 418, column 43.
     f output = process.inputStream.text.trim
                                   ^
  
  script '/Users/mehmetsozd/Desktop/almiratur/almira_app/node_modules/@react-native-community/cli-platform-android/native_modules.gradle': 418: [Static type checking] - Cannot find matching method java.lang.Object#trim(). Please check if the declared type is correct and if the method exists.
   @ line 418, column 24.
               def output = process.inputStream.text.trim()
                            ^
  
  3 errors

原因分析

这个问题源于React Native 0.75.x升级后,@react-native-community/cli-platform-android包的版本与新版Gradle(RN 0.75默认升级至Gradle 8.7)的兼容性冲突:

  • Gradle 8.x启用了更严格的静态类型检查,Java Process 类没有直接暴露inputStream属性,旧写法process.inputStream不符合规范,需调用getInputStream()方法;
  • Groovy对输入流的text快捷属性处理在新版Gradle中也存在兼容性问题。

解决方案

方案1:更新@react-native-community/cli-platform-android至兼容版本

这是最优解,确保CLI工具与RN 0.75.0版本匹配:

  1. 删除项目根目录下的node_modules文件夹、package-lock.json(或yarn.lock);
  2. 执行npm install(或yarn install)重新安装依赖,RN会自动拉取对应版本的CLI包;
  3. 重新执行Android构建命令(如./gradlew clean && ./gradlew assembleDebug)。

方案2:临时修改native_modules.gradle文件

若暂时无法更新依赖,可手动修复报错代码:

  1. 打开路径node_modules/@react-native-community/cli-platform-android/native_modules.gradle;
  2. 定位到第418行,将原代码:
    def output = process.inputStream.text.trim()
    
    修改为:
    def output = new BufferedReader(new InputStreamReader(process.getInputStream())).text.trim()
    
    或使用Groovy的简化写法:
    def output = process.in.text.trim()
    
  3. 保存文件后重新执行构建命令。

方案3:确认Gradle配置与RN 0.75匹配

RN 0.75默认使用Gradle 8.7及Android Gradle Plugin 8.4.0,检查以下配置:

  1. 打开gradle/wrapper/gradle-wrapper.properties,确保distributionUrl为:
    distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-all.zip
    
  2. 打开android/build.gradle,确保classpath中的AGP版本正确:
    dependencies {
        classpath("com.android.tools.build:gradle:8.4.0")
        // 其他依赖...
    }
    

方案4:清理缓存后重建

  1. 执行Android缓存清理:./gradlew clean;
  2. 清除npm缓存:npm cache clean --force;
  3. 重新安装依赖并构建:npm install && ./gradlew assembleDebug。

内容的提问来源于stack exchange,提问作者Mehmet Ozdemir

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.19 15:13:10