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

React Native升级至0.72.1时:react-native-restart构建报错求助

React Native 0.72.1升级后react-native-restart构建报错解决方法

问题背景

将React Native从0.64.2升级到0.72.1后,构建Android项目时触发:react-native-restart模块报错:

No signature of method: build_afxi0qx2ay1lg1ud0qnu4nfni.android() is applicable for argument types: (build_afxi0qx2ay1lg1ud0qnu4nfni$_run_closure1) values: [build_afxi0qx2ay1lg1ud0qnu4nfni$_run_closure1@1fae07c5]

问题原因

  1. RN 0.70+版本升级了Gradle和Android Gradle Plugin(AGP)版本,旧版react-native-restart的构建脚本语法与新环境不兼容。
  2. 提供的代码片段中namespace 'co m.reactnativerestart'存在空格错误,应为com.reactnativerestart。
  3. 根项目buildscript中的SDK版本配置(如compileSdkVersion=31)与RN 0.72要求的版本不匹配。

解决方案

1. 优先升级react-native-restart到兼容版本

旧版库未适配新Gradle语法,直接升级到官方适配RN 0.72的版本:

npm uninstall react-native-restart
npm install react-native-restart@latest

目前0.0.27及以上版本已适配RN新架构和Gradle 7+,可直接使用。

2. 手动修复库的构建脚本(仅当升级库无效时使用)

如果无法升级库,手动修改node_modules/react-native-restart/android/build.gradle:

  • 修正namespace的空格错误:
    android {
      // 把 co m.reactnativerestart 改为 com.reactnativerestart
      namespace 'com.reactnativerestart'
      // ... 其他配置
    }
    
  • 调整SDK版本获取逻辑,适配新Gradle:
    def safeExtGet(name) {
      rootProject.ext.has(name) ? rootProject.ext.get(name) : project.properties[name]
    }
    
    android {
      namespace 'com.reactnativerestart'
      compileSdkVersion safeExtGet('compileSdkVersion') ?: 33
      buildToolsVersion safeExtGet('buildToolsVersion') ?: "33.0.0"
    
      defaultConfig {
        minSdkVersion safeExtGet('minSdkVersion') ?: 21
        targetSdkVersion safeExtGet('targetSdkVersion') ?: 33
        versionCode 1
        versionName "1.0"
      }
    }
    

3. 更新根项目的build.gradle配置

修改项目根目录android/build.gradle中的ext配置,匹配RN 0.72的要求:

buildscript {
    ext {
        buildToolsVersion = "33.0.0"
        minSdkVersion = 21
        compileSdkVersion = 33
        targetSdkVersion = 33
        ndkVersion = "25.1.8937393"
        googlePlayServicesAuthVersion = "19.2.0"
        kotlin_version = '1.8.10'
    }
    // ... 其他buildscript配置
}

4. 清理缓存并重新构建

执行以下命令清理构建缓存,避免残留旧配置影响:

cd android && ./gradlew clean
cd .. && npx react-native run-android

内容的提问来源于stack exchange,提问作者Nikhil Masurkar

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.31 20:46:33