2026年2月28日 星期六

iOS Xcode: WebRTC for simulator and device

在 xcode 下使用 COCOAPODS 去裝 GoogleWebRTC,可以在把整個專案編譯到手機上,但是在虛擬機上面就會出錯,這裡紀錄處理方案。


原因 & 計畫

原本的 GoogleWebRTC 在編譯時因為沒有指定編譯版本給 simulator,這樣就變成下載來了但是不能在 Simulator 裡面打開。 又因 GoogleWebRTC 在 cocoapods 的 repository 已經棄用更新了,所以本篇文章的解決方案是自己把整個 WebRTC 拉下來編譯然後手動拖進專案裡面。


install.sh

# 選擇 xcode build 工具
sudo xcode-select -s /Applications/Xcode.app/Contents/Developer

# 下載 depot tools
mkdir -p ./build 
cd ./build
git clone https://chromium.googlesource.com/chromium/tools/depot_tools.git

# 把路徑加上去
export PATH="$(shell pwd)/depot_tools:$PATH"

# 下載 webrtc_ios 專案回來
cd ..
mkdir -p ./webrtc_ios
cd ./webrtc_ios
fetch --nohooks webrtc_ios
gclient sync
gclient runhooks


好了之後,由於專案可能沒有引用到特定變數導致引用 google_test_runner_objc 時出錯,這裡採用直接忽略的方式,接下來需要去修改 ./webrtc_ios/src/webrtc.gni 裡面找到一行:

    # Build //test:google_test_runner_objc when the test is not a real XCTest.
    if (is_ios && rtc_include_tests) {
      if (!defined(invoker.is_xctest) || !invoker.is_xctest) {
        xctest_module_target = "//test:google_test_runner_objc"
      }
    }

在這個 xctest_module 下面加一行
    # Build //test:google_test_runner_objc when the test is not a real XCTest.
    if (is_ios && rtc_include_tests) {
      if (!defined(invoker.is_xctest) || !invoker.is_xctest) {
        xctest_module_target = "//test:google_test_runner_objc"
        not_needed([ "xctest_module_target" ]) # <--- Add this line
      }
    }

ref issue:

gn gen out/ios_64 --args='target_os="ios" target_cpu="arm64" target_environment="device"'

ERROR at //webrtc.gni:548:32: Assignment had no effect.

        xctest_module_target = "//test:google_test_runner_objc"

                               ^-------------------------------

You set the variable "xctest_module_target" here and it was unused before it went

out of scope.

Via these template invocations:

  rtc_test("rtc_unittests")  //BUILD.gn:628

  test("rtc_unittests")  //webrtc.gni:494

  mixed_test("rtc_unittests")  //testing/test.gni:990

  ios_app_bundle("rtc_unittests")  //testing/test.gni:172

  executable("rtc_unittests_executable")  //build/config/ios/rules.gni:288

See //build/config/ios/rules.gni:288:3: whence it was called.

  executable(_executable_target) {

  ^-------------------------------

See //testing/test.gni:172:5: whence it was called.

    target(invoker.target_type, target_name) {

    ^-----------------------------------------

See //testing/test.gni:990:5: whence it was called.

    mixed_test(_test_target) {

    ^-------------------------

See //webrtc.gni:494:3: whence it was called.

  test(target_name) {

  ^------------------

See //BUILD.gn:628:3: whence it was called.

  rtc_test("rtc_unittests") {

  ^--------------------------


接著就繼續下一步驟:

cd src

# 這個  target_environment="simulator" 是給模擬器的,手機要指定為 device
gn gen out/ios --args='target_os="ios" target_cpu="arm64" target_environment="simulator" is_debug=false is_component_build=false rtc_include_tests=false rtc_enable_objc_symbol_export=true enable_dsyms=false enable_stripping=true'

# 這裡要以 framework_objc 方式處理,不能用 AppRTCMobile,他可能會把不使用的程式碼移除掉,導致引用失敗
ninja -C out/ios framework_objc

完成後,把 out/WebRTC.framework 直接拖拉進去專案,在專案設定的 Frameworks, Libraries, and Embedded Content 要記得打開至少 Embed Without Signing



如果要同時支持手機和 simulator,要編譯兩次,然後用工具把它們合併,像是:

gn gen out/ios_simulator --args='target_os="ios" target_cpu="arm64" target_environment="simulator" is_debug=false is_component_build=false rtc_include_tests=false rtc_enable_objc_symbol_export=true enable_dsyms=false enable_stripping=true'

ninja -C out/ios_simulator framework_objc


gn gen out/ios_device --args='target_os="ios" target_cpu="arm64" target_environment="device" is_debug=false is_component_build=false rtc_include_tests=false rtc_enable_objc_symbol_export=true enable_dsyms=false enable_stripping=true'

ninja -C out/ios_device framework_objc


回到整個專案的根目錄:

xcodebuild -create-xcframework -framework build/webrtc_ios/src/out/ios_device/WebRTC.framework -framework build/webrtc_ios/src/out/ios_simulator/WebRTC.framework -output build/WebRTC.xcframework



Reference:

[1] https://webrtc.github.io/webrtc-org/native-code/ios/
[2] https://gn.googlesource.com/gn/
[3] https://github.com/CocoaPods/CocoaPods/issues/6578
[4] https://stackoverflow.com/questions/63607158/xcode-building-for-ios-simulator-but-linking-in-an-object-file-built-for-ios-f
[5] https://groups.google.com/g/discuss-webrtc/c/wdZKCx6yMvE
[6] https://stackoverflow.com/questions/29865899/ld-framework-not-found-pods/57167318
[7] https://stackoverflow.com/questions/33085505/error-building-webrtc-library-for-ios
[8] https://github.com/pristineio/webrtc-build-scripts/tree/master




  


沒有留言:

張貼留言

© Mac Taylor, 歡迎自由轉貼。
Background Email Pattern by Toby Elliott
Since 2014