react native 出现程序包com.facebook.react不存在

今天在通过android studio 编译react-native 项目的时候,莫名出现com.facebook.react下的文件不存在,部分缺失如图:
如:程序包com.facebook.react不存在、 程序包com.facebook.react.bridge不存在、程序包com.facebook.react.uimanager不存在
在这里插入图片描述
项目中react-native引入是通过:

 implementation 'com.facebook.react:react-native:+'

在这里插入图片描述
在这里插入图片描述
解决办法:
方法一
:react-native版本低于0.63,在 android\build.gradle 添加如下内容:

def REACT_NATIVE_VERSION = new File(['node', '--print',"JSON.parse(require('fs').readFileSync(require.resolve('react-native/package.json'), 'utf-8')).version"].execute(null, rootDir).text.trim())

allprojects {
    configurations.all {
        resolutionStrategy {
            // Remove this override in 0.65+, as a proper fix is included in react-native itself.
            force "com.facebook.react:react-native:" + REACT_NATIVE_VERSION
        }
    }

添加这段代码意思是获取到当前使用的 react-native 版本,然后把所有依赖项目的 rn 都覆盖成这个版本。

在我自己的项目,刚开始是用在每个build.gradle,引用到implementation ‘com.facebook.react:react-native:+’,都直接写死版本号:implementation ‘com.facebook.react:react-native:0.52.0’

方法二:react-native版本大于0.63,根据官网issue(Android build failures happening since Nov 4th 2022 · Issue #35210)里找到对应的热更新补丁,更新 package.json 内容,重新 yarn install,然后 cd android && ./gradlew clean 清理缓存,之后应该就恢复正常了

猜你喜欢

转载自blog.csdn.net/qq_38847655/article/details/127739870