星星博客 »  > 

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文件更新以后,这个问题应该就会不存在了,期待它官方更新。

参考

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

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

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

相关文章