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配置:
- 在
android/settings.gradle顶部添加:apply from: new File(["node", "--print", "require.resolve('expo/package.json')"].execute(null, rootDir).text.trim(), "../scripts/autolinking.gradle") useExpoModules() - 在
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"未注册异常
- 确认入口注册与配置一致:
- 打开
index.js,确保注册代码使用app.json中的名称:import { AppRegistry } from 'react-native'; import App from './App'; import { name as appName } from './app.json'; AppRegistry.registerComponent(appName, () => App); - 检查
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
相关产品推荐
相关产品推荐

