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

React Native项目迁移Gradle 7遇运行时错误求助

解决方案

一、修复TypeError: (0, _expoModulesCore.requireNativeModule) is not a function错误

  • 匹配expo-modules-core版本:Expo 44.0.0对应expo-modules-core@~1.1.1,执行以下命令重装:
    npm uninstall expo-modules-core
    npm install expo-modules-core@~1.1.1
    
  • 验证Android项目Expo配置:
    1. 在android/settings.gradle顶部添加:
      apply from: new File(["node", "--print", "require.resolve('expo/package.json')"].execute(null, rootDir).text.trim(), "../scripts/autolinking.gradle")
      useExpoModules()
      
    2. 在android/app/build.gradle顶部添加:
      apply from: new File(["node", "--print", "require.resolve('expo/package.json')"].execute(null, rootDir).text.trim(), "../scripts/expo.gradle")
      
  • 清除全量缓存:
    npx react-native clean
    cd android && ./gradlew clean && cd ..
    rm -rf node_modules && npm install
    

二、修复"TestApp"未注册异常

  • 确认入口注册与配置一致:
    1. 打开index.js,确保注册代码使用app.json中的名称:
      import { AppRegistry } from 'react-native';
      import App from './App';
      import { name as appName } from './app.json';
      
      AppRegistry.registerComponent(appName, () => App);
      
    2. 检查app.json的name字段是否为TestApp,与AndroidManifest.xml中android:name保持一致。

三、Gradle 7迁移具体步骤(适配React Native 0.68.0)

  • 更新Gradle wrapper:修改android/gradle/wrapper/gradle-wrapper.properties:
    distributionUrl=https\://services.gradle.org/distributions/gradle-7.2-all.zip
    
  • 升级Gradle插件:在android/build.gradle的buildscript.dependencies中设置:
    classpath("com.android.tools.build:gradle:4.2.2")
    
  • 替换旧依赖配置:将项目中所有compile关键字替换为implementation(Gradle 7已废弃compile)。
  • 配置JDK 11兼容:在android/app/build.gradle中添加:
    android {
        compileOptions {
            sourceCompatibility JavaVersion.VERSION_11
            targetCompatibility JavaVersion.VERSION_11
        }
        kotlinOptions {
            jvmTarget = '11'
        }
    }
    
  • 排查第三方Native库兼容性:检查项目依赖的第三方Native库,升级到支持Gradle 7的版本,若无法升级则手动修改其build.gradle文件,替换compile为implementation并调整JDK版本配置。

内容的提问来源于stack exchange,提问作者Parth Bhayani

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.22 22:36:19