`React/RCTBridgeModule.h` 找不到文件

`React/RCTBridgeModule.h` file not found

在 xcode 上构建反应本机 iOS 应用程序时出现此错误。

在 npm 安装和 rpm 链接 react-native-fs 库后开始出现此错误。但是在网上搜索解决方案后,我发现很多人在安装其他 react native 库时遇到同样的错误。

许多人建议的可能的解决方案是, 在 "Build Settings" -> "Header Search Paths".

下添加以下内容

$(SRCROOT)/../node_modules/react-native/React - (递归)

但是这个解决方案不走运,仍然出现同样的错误

您可以做的是:

1) npm uninstall reat-native-fs 卸载库

2)npm unlink react-native-fs 取消link 图书馆

现在库已成功删除,现在再次在您的项目中安装库,这次 link 手动安装所有内容。有时自动 linking 会导致此错误。

快速修复(不是最好的)

更改导入 react-native header 行:

 #import <React/RCTBridgeModule.h>
 #import <React/RCTLog.h>

收件人:

 #import "RCTBridgeModule.h"
 #import "RCTLog.h"

这是我必须对尝试使用的库进行更改的示例:Closes #46 - 'RCTBridgeModule.h' file not found

react-native 库的最新版本,如之前的帖子和 here 中所述,具有破坏性的兼容性更改。如果你不打算升级到 react-native 0.40+,你可以强制安装以前版本的库,例如 react-native-fs:

npm install --save -E react-native-fs@1.5.1

对于将 React Native 升级到 0.40+ 后出现此错误的观众,您可能需要在命令行上 运行 react-native upgrade

在我的例子中,当我试图为 iOS 归档一个 0.40+ react-native 应用程序时,这个特殊问题发生了(解决方案在这里找到:Reliable build on ^0.39.2 fails when upgrading to ^0.40.0)。

发生的事情是 Xcode 试图并行构建 react-native 库 并且正在构建库在实际构建反应库之前隐式反应依赖

我的解决方案是:

  1. 禁用并行构建:

    • Xcode 菜单 -> 产品 -> 方案 -> 管理 Shemes...
    • 双击您的应用程序
    • 构建选项卡 -> 取消选中并行化构建
  2. 添加 react 作为项目依赖

    • Xcode 项目导航器 -> 将 React.xcodeproj 从库拖到根树
    • 构建阶段选项卡 -> 目标依赖项 -> + -> 添加 React

我能够构建调试,但无法构建存档。

我通过将 /node_modules/react-native/React 中的 React.xcodeproj 拖到 Xcode 中的根目录解决了这个问题,然后在构建阶段 > 目标依赖项中将 React 添加为目标依赖项。

如果 Libraries/React.xcodeproj 在 xcode 中为红色,则重新安装 node_modules

rm -rf node_modules && yarn

我从 react-native 0.46.3 新建的项目是红色的:S 当我做 react-native init 时我有 npm 5.3.0 和 yarn 0.24.5

改变

  #import "RCTBridgeModule.h"

 #import "React/RCTBridgeModule.h"

确保禁用 Parallelise Build 并在目标上方添加 React 目标

如果你想从你的编辑器中制作它也打开 SMobile.xcscheme

并改变parallelizeBuildables = "NO"

对我来说,上面的解决方案没有任何效果,下面是有效的(我已经签出 Parallelize Build 并添加了 React

1. Open XCode --> To Libraries add `$LibraryWhichDoesNotWork.xcodeproj$`
2. Then for your app in the `Build Phases` add to the `Link Binary with Libraries` the file `lib$LibraryWhichDoesNotWork$.a`

在我对新依赖项执行 运行 pod install 命令后,出现了这个错误。除此之外,还安装了 React。因此可能 Xcode 混淆了路径。我从 PodFile 中删除了这些行,错误消失了。请注意,从此处删除的内容已链接到 Xcode。

target 'app' do

  pod 'GoogleMaps'
  pod 'Firebase/Auth', '~> 6.3.0'
  pod 'Firebase/Database', '~> 6.3.0'

  # Removed four pods below and it worked.

  pod 'react-native-image-picker', :path => '../node_modules/react-native-image-picker'

  pod 'ReactNativePermissions', :path => '../node_modules/react-native-permissions'

  pod 'react-native-image-resizer', :path => '../node_modules/react-native-image-resizer'

  pod 'RNFS', :path => '../node_modules/react-native-fs'

  end

我在从 0.58.4 升级到新的 react-native 版本 0.60.4 时遇到了这个问题。我在互联网上找到的东西对我没有任何帮助,但我设法让它工作:

转到构建设置,搜索 'Header search paths'、select 条目,按删除按钮。

我覆盖了这些值,看起来它们在删除后恢复为默认值。 Cocoapods 在 pod install:

之后也在终端中用消息抱怨它
[!] The `app [Release]` target overrides the `HEADER_SEARCH_PATHS` build setting defined in `Pods/Target Support Files/Pods-app/Pods-app.release.xcconfig'. This can lead to problems with the CocoaPods installation

转到项目中的 iOS 文件夹并安装 pod -

$ pod install

如果您在安装 pod 类型命令时遇到任何错误-

$ xcode-select -p

结果应该是 - /Applications/Xcode.app/Contents/Developer

如果路径不正确,请在 Xcode 中打开您的 iOS 项目并转到:Xcode->首选项->命令行工具-> select Xcode

然后再次安装 pod,您的问题就会得到解决。

对我来说,当我在应用程序中添加一个新的 scheme/target (app.staging) 并使用 pod 安装 pods 时发生了这个错误安装。

出现此问题是因为 pods 并未为所有目标共享。所以我需要在 Podfile.

中添加新添加的目标 (app.staging)

这是我的 Podfile。

platform :ios, '9.0'
require_relative '../node_modules/@react-native-community/cli-platform-ios/native_modules'

target 'app' do
  # Pods for app
  pod 'FBLazyVector', :path => "../node_modules/react-native/Libraries/FBLazyVector"
  pod 'FBReactNativeSpec', :path => "../node_modules/react-native/Libraries/FBReactNativeSpec"

  target 'appTests' do
    inherit! :search_paths
    # Pods for testing
  end

  # Pods for staging app // important line below
  target 'app.staging'

  use_native_modules!
end

在 React Native 0.60 之后,这个问题通常是由混合了新 'auto-linking' 功能的链接库引起的。这为我修复了它

取消链接旧库使用

$ react-native unlink react-native-fs

完全使用

刷新Pods集成
$ pod deintegrate && pod install

现在重新加载您的工作区并进行干净的构建。

我在使用 create-react-native-module 创建的任何新模块中都收到此错误。 None 已发布的解决方案对我有用。

对我有用的是首先确保在新创建的模块文件夹中 运行 yarn 以创建 node_modules/(这一步可能很明显)。然后,在 XCode、select Product -> Scheme -> React 而不是默认的 selection of MyModuleName.

我 运行 在对 RN 0.59+

上的不支持自动 link 的依赖项进行手动 react-native link 后进入了这个问题

解决方案是select在Xcode的Libraries文件夹下的xcodeproj文件,然后在Build Settings中,更改Header Search Paths来添加这两个(递归):

$(SRCROOT)/../../../ios/Pods/Headers/Public/React-Core
$(SRCROOT)/../../../ios/Pods/Headers/Public

如果您想保持启用 Parallelise Build 并避免丢失 header 问题,请在您的方案中提供一个 pre-build 步骤,将反应 header 放入 derived-data 区域。请注意,在这种情况下,构建设置来自 React 项目。是的,这不是一件美事,但它可以完成工作,还可以节省大量构建时间。预构建步骤输出以 prebuild.log 结束。您需要复制的确切 header 取决于您的项目 react-native 依赖项,但您将从中得到要点。

从环境变量中获取派生数据目录,将需要的react header复制过来。

#build_prestep.sh (chmod a+x)
derived_root=$(echo $SHARED_DERIVED_FILE_DIR|sed 's/DerivedSources//1')
react_base_headers=$(echo $PROJECT_FILE_PATH|sed 's#React.xcodeproj#Base/#1')
react_view_headers=$(echo $PROJECT_FILE_PATH|sed 's#React.xcodeproj#Views/#1')
react_modules_head=$(echo $PROJECT_FILE_PATH|sed 's#React.xcodeproj#Modules/#1')
react_netw_headers=$(echo $PROJECT_FILE_PATH|sed 's#React/React.xcodeproj#Libraries/Network/#1')
react_image_header=$(echo $PROJECT_FILE_PATH|sed 's#React/React.xcodeproj#Libraries/Image/#1')

echo derived root = ${derived_root}
echo react headers = ${react_base_headers}

mkdir -p ${derived_root}include/React/

find  "${react_base_headers}" -type f -iname "*.h" -exec cp {} "${derived_root}include/React/" \;
find  "${react_view_headers}" -type f -iname "*.h" -exec cp {} "${derived_root}include/React/" \;
find  "${react_modules_head}" -type f -iname "*.h" -exec cp {} "${derived_root}include/React/" \;
find  "${react_netw_headers}" -type f -iname "*.h" -exec cp {} "${derived_root}include/React/" \;
find  "${react_image_header}" -type f -iname "*.h" -exec cp {} "${derived_root}include/React/" \;

脚本确实在 build-clean 期间被调用 - 这并不理想。在我的例子中,有一个 env 变量改变了,让我在清理过程中尽早退出脚本。

if [ "$RUN_CLANG_STATIC_ANALYZER" != "NO" ] ; then
    exit 0 
fi

anhdevit 在 https://github.com/facebook/react-native/issues/24363#issuecomment-488547280 中的建议对我有用:

In your terminal, run: defaults delete com.apple.dt.Xcode

我的解决方案是删除 Libraries 中的所有内容,如 here

所述