M1 運行 react-native 項目遇到的問題

系統(tǒng):11.2.3
Xcode:12.5


錯誤提示:xx.app/main.jsbundle does not exist. this must be a bug with

問題描述: Build Config 選擇 release 的情況下,運行 appmain.jsbundle 的圖片沒有加載。打包時會提示 main.jsbundle does not exist 錯誤。但是在 debug 狀態(tài)下卻沒有出現(xiàn)這個問題。
原因分析: 應(yīng)該是 main.jsbundle 的加載路徑出現(xiàn)了問題,在 Build Phases 中可以看到 Bundle React Native code and images 這一欄中:

export NODE_BINARY=node
../node_modules/react-native/scripts/react-native-xcode.sh

大概意思是通過 node 運行 react-native-xcode.sh 這個腳本來加載圖片,但是由于 M1node 所在的路徑與之前的都不一樣,導(dǎo)致了 Xcode 在運行時無法正確使用 node 指令。
解決辦法: 打開終端,輸入:which node,得到一下路徑:/opt/homebrew/bin/node
將這個路徑添加到 Bundle React Native code and images

export PATH="$PATH:/opt/homebrew/bin"
export NODE_BINARY=node
../node_modules/react-native/scripts/react-native-xcode.sh

重新運行后,圖片可以顯示了,問題得到解決。


錯誤提示:cannot initialize a parameter of type 'NSArray<id<RCTBridgeModule>> *' with an lvalue of type 'NSArray<Class> *__strong' NSArray<RCTModuleData *> *newModules = [self _initializeModules:modules withDispatchGroup:NULL lazilyDiscovered:YES];

問題描述: 從 GitHub 上看到,這是在 Xcode 12.5 才有的錯誤。
解決辦法: 把報錯的參數(shù)類型,按照提示修改為對應(yīng)的類型即可。
NSArray<id<RCTBridgeModule>> * 改為 NSArray<Class> *
如果有集成了 cocoapods 就比較方便,直接在 Podfile 添加以下代碼:

post_install do |installer|
## Fix for XCode 12.5
  find_and_replace(
  "../node_modules/react-native/React/CxxBridge/RCTCxxBridge.mm",
  "_initializeModules:(NSArray<id<RCTBridgeModule>> *)modules", 
  "_initializeModules:(NSArray<Class> *)modules")
  
  find_and_replace(
  "../node_modules/react-native/ReactCommon/turbomodule/core/platform/ios/RCTTurboModuleManager.mm",
  "RCTBridgeModuleNameForClass(module))", 
  "RCTBridgeModuleNameForClass(Class(module)))"
  )
end

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

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容