React Native踩坑新建的RN0.64项目无法在xcode 12.5上打开

环境信息

运行命令:

 npx react-native info   

结果如下:

 npx react-native info                              [20:34:42]
info Fetching system and libraries information...
System:
    OS: macOS 11.3
    CPU: (8) x64 Intel(R) Core(TM) i7-4720HQ CPU @ 2.60GHz
    Memory: 839.71 MB / 16.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 14.15.2 - /usr/local/bin/node
    Yarn: 1.22.10 - /usr/local/bin/yarn
    npm: 7.11.2 - /usr/local/bin/npm
    Watchman: Not Found
  Managers:
    CocoaPods: 1.10.1 - /usr/local/bin/pod
  SDKs:
    iOS SDK:
      Platforms: iOS 14.5, DriverKit 20.4, macOS 11.3, tvOS 14.5, watchOS 7.4
    Android SDK:
      API Levels: 23, 28, 29, 30
      Build Tools: 28.0.3, 29.0.2, 30.0.3
      System Images: android-30 | Google APIs Intel x86 Atom
      Android NDK: Not Found
  IDEs:
    Android Studio: 4.1 AI-201.8743.12.41.6953283
    Xcode: 12.5/12E262 - /usr/bin/xcodebuild
  Languages:
    Java: 1.8.0_271 - /usr/bin/javac
  npmPackages:
    @react-native-community/cli: Not Found
    react: 17.0.1 => 17.0.1
    react-native: 0.64.0 => 0.64.0
    react-native-macos: Not Found
  npmGlobalPackages:
    *react-native*: Not Found

报错如下:

** BUILD FAILED **

The following build commands failed:
CompileC /Users/itkey/Library/Developer/Xcode/DerivedData/speedrn-gdnmwquzchcterhjmywhtacsnjev/Build/Intermediates.noindex/Pods.build/Debug-iphonesimulator/Flipper-Folly.build/Objects-normal/x86_64/DistributedMutex.o /Users/itkey/mac/rn/speedrn/ios/Pods/Flipper-Folly/folly/synchronization/DistributedMutex.cpp normal x86_64 c++ com.apple.compilers.llvm.clang.1_0.compiler
(1 failure)

info Run CLI with --verbose flag for more details.
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
FAIL: 1

解决办法

临时解决办法(简单粗暴)

修改/ios/Podfile注释掉Flipper相关的内容。

 # Enables Flipper.
  #
  # Note that if you have use_frameworks! enabled, Flipper will not work and
  # you should disable the next line.
  #use_flipper!()

  #post_install do |installer|
    #react_native_post_install(installer)
  #end

这是一个临时解决办法,不知道后面会不会有什么隐患。实际上确实可以正常运行。
在这里插入图片描述

方法二(推荐)

修改/ios/Podfile 内容如下:

详细修改操作:
注释掉下面内容:

 use_flipper!()
  post_install do |installer|
    react_native_post_install(installer)
  end

并把此处替换为:

#⌄⌄⌄⌄⌄⌄⌄⌄⌄⌄⌄⌄⌄⌄⌄⌄⌄⌄⌄⌄修复iOS 14.5新建RN0.64无法运行⌄⌄⌄⌄⌄⌄⌄⌄⌄⌄⌄⌄⌄⌄⌄⌄⌄⌄⌄⌄⌄⌄⌄⌄⌄⌄
  def find_and_replace(dir, findstr, replacestr)
	 Dir[dir].each do |name|
		 text = File.read(name)
		 replace = text.gsub(findstr,replacestr)
		 if text != replace
		   puts "Fix: " + name
		   File.open(name, "w") { |file| file.puts replace }
		   STDOUT.flush
		 end
	 end
	 Dir[dir + '*/'].each(&method(:find_and_replace))
  end

  post_install do |installer|
    flipper_post_install(installer)
    find_and_replace("Pods/Flipper-Folly/folly/synchronization/DistributedMutex-inl.h",
    "atomic_notify_one(state)", "folly::atomic_notify_one(state)")

    find_and_replace("Pods/Flipper-Folly/folly/synchronization/DistributedMutex-inl.h",
    "atomic_wait_until(&state, previous | data, deadline)", "folly::atomic_wait_until(&state, previous | data, deadline)")
  end
#⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃修复iOS 14.5新建RN0.64无法运行⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃

最终文件如下:

require_relative '../node_modules/react-native/scripts/react_native_pods'
require_relative '../node_modules/@react-native-community/cli-platform-ios/native_modules'

platform :ios, '10.0'

target 'speed2' do
  config = use_native_modules!

  use_react_native!(
    :path => config[:reactNativePath],
    # to enable hermes on iOS, change `false` to `true` and then install pods
    :hermes_enabled => false
  )

  target 'speed2Tests' do
    inherit! :complete
    # Pods for testing
  end

  # Enables Flipper.
  #
  # Note that if you have use_frameworks! enabled, Flipper will not work and
  # you should disable the next line.
  #use_flipper!()

  #post_install do |installer|
  #  react_native_post_install(installer)
  #end

#⌄⌄⌄⌄⌄⌄⌄⌄⌄⌄⌄⌄⌄⌄⌄⌄⌄⌄⌄⌄修复iOS 14.5新建RN0.64无法运行⌄⌄⌄⌄⌄⌄⌄⌄⌄⌄⌄⌄⌄⌄⌄⌄⌄⌄⌄⌄⌄⌄⌄⌄⌄⌄
  def find_and_replace(dir, findstr, replacestr)
	 Dir[dir].each do |name|
		 text = File.read(name)
		 replace = text.gsub(findstr,replacestr)
		 if text != replace
		   puts "Fix: " + name
		   File.open(name, "w") { |file| file.puts replace }
		   STDOUT.flush
		 end
	 end
	 Dir[dir + '*/'].each(&method(:find_and_replace))
  end

  post_install do |installer|
    flipper_post_install(installer)
    find_and_replace("Pods/Flipper-Folly/folly/synchronization/DistributedMutex-inl.h",
    "atomic_notify_one(state)", "folly::atomic_notify_one(state)")

    find_and_replace("Pods/Flipper-Folly/folly/synchronization/DistributedMutex-inl.h",
    "atomic_wait_until(&state, previous | data, deadline)", "folly::atomic_wait_until(&state, previous | data, deadline)")
  end
#⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃修复iOS 14.5新建RN0.64无法运行⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃
end

总结

方法二最终实现思路就是修改这个文件达到修复无法启动的问题。

Pods/Flipper-Folly/folly/synchronization/DistributedMutex-inl.h

在这里插入图片描述

理论上等到Flipper-Folly文件更新以后,这个问题应该就会不存在了,期待它官方更新。

RN0.63.3测试

有网友评论0.63版本报错,我来实测一下。

创建0.63的项目

npx react-native init AwesomeProject --version 0.63.3

运行测试

不对Podfile做修改

报错如下:
在这里插入图片描述

修改/ios/Podfile注释掉Flipper相关的内容。

  • 修改/ios/Podfile注释掉Flipper相关的内容。
  • 删除/ios/Pod目录
  • 删除/ios/Podfile.lock
  • 执行 pod install --verbose
  • yarn ios运行项目
  • 运行成功!
    在这里插入图片描述

方法二修改/ios/Podfile 内容

  • 修改/ios/Podfile修改后内容如下:
require_relative '../node_modules/react-native/scripts/react_native_pods'
require_relative '../node_modules/@react-native-community/cli-platform-ios/native_modules'

platform :ios, '10.0'

target 'AwesomeProject' do
  config = use_native_modules!

  use_react_native!(:path => config["reactNativePath"])

  target 'AwesomeProjectTests' do
    inherit! :complete
    # Pods for testing
  end

  # Enables Flipper.
  #
  # Note that if you have use_frameworks! enabled, Flipper will not work and
  # you should disable these next few lines.
  #use_flipper!
  #post_install do |installer|
  #  flipper_post_install(installer)
  #end
  #⌄⌄⌄⌄⌄⌄⌄⌄⌄⌄⌄⌄⌄⌄⌄⌄⌄⌄⌄⌄修复iOS 14.5新建RN0.64无法运行⌄⌄⌄⌄⌄⌄⌄⌄⌄⌄⌄⌄⌄⌄⌄⌄⌄⌄⌄⌄⌄⌄⌄⌄⌄⌄
  def find_and_replace(dir, findstr, replacestr)
	 Dir[dir].each do |name|
		 text = File.read(name)
		 replace = text.gsub(findstr,replacestr)
		 if text != replace
		   puts "Fix: " + name
		   File.open(name, "w") { |file| file.puts replace }
		   STDOUT.flush
		 end
	 end
	 Dir[dir + '*/'].each(&method(:find_and_replace))
  end

  post_install do |installer|
    flipper_post_install(installer)
    find_and_replace("Pods/Flipper-Folly/folly/synchronization/DistributedMutex-inl.h",
    "atomic_notify_one(state)", "folly::atomic_notify_one(state)")

    find_and_replace("Pods/Flipper-Folly/folly/synchronization/DistributedMutex-inl.h",
    "atomic_wait_until(&state, previous | data, deadline)", "folly::atomic_wait_until(&state, previous | data, deadline)")
  end
 #⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃修复iOS 14.5新建RN0.64无法运行⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃
end

target 'AwesomeProject-tvOS' do
  # Pods for AwesomeProject-tvOS

  target 'AwesomeProject-tvOSTests' do
    inherit! :search_paths
    # Pods for testing
  end
end
  • 删除/ios/Pod目录
  • 删除/ios/Podfile.lock
  • 执行 pod install --verbose
  • yarn ios运行项目
  • 运行成功!
    在这里插入图片描述

参考

https://github.com/facebook/react-native/issues/31179

https://github.com/facebook/flipper/issues/2215

https://github.com/facebook/react-native/issues/31179

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值