flutter 在OC工程中添加swift庫報錯的問題

下午看見羣裏有人問 swift 插件導入 oc 項目報錯的問題, 我之前解決過, 但是忘了解決過程是怎麼樣的了, 這裏記錄下方便以後備查

沒興趣追蹤原因的直接查看總結下章節

復現錯誤

  1. 創建一個主工程(oc)
flutter create oc_project
  1. 創建一個插件工程(swift)
cd oc_project
flutter create -i swift -t plugin swift_plugin
  1. 關聯工程
  swift_plugin:
    path: ./swift_plugin
    ```
  1. 運行 iOS 項目得到錯誤信息
Launching lib/main.dart on iPhone XS Max in debug mode...

CocoaPods' output:

↳

      Preparing

    Analyzing dependencies

    Inspecting targets to integrate

      Using `ARCHS` setting to build architectures of target `Pods-Runner`: (``)

    Fetching external sources

    -> Fetching podspec for `Flutter` from `.symlinks/flutter/ios`

    -> Fetching podspec for `swift_plugin` from `.symlinks/plugins/swift_plugin/ios`

    Resolving dependencies of `Podfile`

    Comparing resolved specification to the sandbox manifest

      A Flutter

      A swift_plugin

    Downloading dependencies

    -> Installing Flutter (1.0.0)

    -> Installing swift_plugin (0.0.1)

      - Running pre install hooks

    [!] Unable to determine Swift version for the following pods:

    - `swift_plugin` does not specify a Swift version and none of the targets (`Runner`) integrating it have the `SWIFT_VERSION` attribute set. Please contact the author or set the `SWIFT_VERSION` attribute in at least one of the targets that integrate this pod.

    /Library/Ruby/Gems/2.3.0/gems/cocoapods-1.6.1/lib/cocoapods/installer/xcode/target_validator.rb:115:in `verify_swift_pods_swift_version'

    /Library/Ruby/Gems/2.3.0/gems/cocoapods-1.6.1/lib/cocoapods/installer/xcode/target_validator.rb:37:in `validate!'

    /Library/Ruby/Gems/2.3.0/gems/cocoapods-1.6.1/lib/cocoapods/installer.rb:459:in `validate_targets'

    /Library/Ruby/Gems/2.3.0/gems/cocoapods-1.6.1/lib/cocoapods/installer.rb:138:in `install!'

    /Library/Ruby/Gems/2.3.0/gems/cocoapods-1.6.1/lib/cocoapods/command/install.rb:48:in `run'

    /Library/Ruby/Gems/2.3.0/gems/claide-1.0.2/lib/claide/command.rb:334:in `run'

    /Library/Ruby/Gems/2.3.0/gems/cocoapods-1.6.1/lib/cocoapods/command.rb:52:in `run'

    /Library/Ruby/Gems/2.3.0/gems/cocoapods-1.6.1/bin/pod:55:in `<top (required)>'

    /usr/local/bin/pod:22:in `load'

    /usr/local/bin/pod:22:in `<main>'

Error output from CocoaPods:

↳

    [!] `<PBXGroup UUID=`97C146E51CF9000F007C117D`>` attempted to initialize an object with an unknown UUID. `CF3B75C9A7D2FA2A4C99F110` for attribute: `children`. This can be the result of a merge and  the unknown UUID is being discarded.

    [!] Automatically assigning platform `ios` with version `8.0` on target `Runner` because no platform was specified. Please specify a platform for this target in your Podfile. See `https://guides.cocoapods.org/syntax/podfile.html#platform`.

Error running pod install

Error launching application on iPhone XS Max.

Exited (sigterm)

錯誤信息大概是這樣的

嘗試解決

提示信息中讓我們聯繫作者加入一個 SWIFT_VERSION 的鬼東西, 因爲插件的作者是我們自己,所以直接改就行了

打開 swift_plugin/ios/swift_plugin.podspec 文件
接着查詢下這個 podspec 的配置頁面文檔: https://guides.cocoapods.org/syntax/podspec.html

20190531155747.png

看新聞最新的是 5.0, 我們就指定 5.0 吧, 在後面添加一行s.swift_version = '5.0', 接着運行, 不出意外的又報錯了

果然沒有這麼順利, 這次的報錯信息是The “Swift Language Version” (SWIFT_VERSION) build setting must be set to a supported value for targets which use Swift. Supported values are: 3.0, 4.0, 4.2. This setting can be set in the build settings editor.

也就是說只支持這三個版本, 看到這裏我想到的是兩個解決方案:

  1. 升級 cocoapod 或查詢文檔看是否新版本支持 5.0+
  2. 查詢當前 swift 文件的真實版本號

思考了幾秒鐘排除了 1, 因爲 swift 可不是什麼穩定的語言, 4.2 的語法 5.0 未必認,反之亦然

所以我們要查詢當前 swift 的文件是用哪個版本編寫的,以便於對號入座

用 xcode 打開 swift_plugin/example/ios, 就像這樣
20190531160129.png

如果打開後只有 Runner,那說明 pod 相關的文件夾沒生成,如下圖
20190531160219.png

先關閉 xcode

然後我們需要在命令行這樣做:

cd swift_plugin/example/ios;pod install

接着再 open in xcode
20190531160129.png

見到 Pods 就說明對了
20190531160433.png

接着查看 swift 語言

2019-05-31 at 16.05.png

往下翻,找到這個,我這裏是 4
20190531160900.png

設置給剛剛那個 swift_version: s.swift_version = '4.0'

接着運行項目, 嗯… 繼續報錯

這次的信息: The “Swift Language Version” (SWIFT_VERSION) build setting must be set to a supported value for targets which use Swift. Supported values are: 3.0, 4.0, 4.2. This setting can be set in the build settings editor.

這次倒是沒讓我們聯繫作者, 猜測一下,估計是主項目可能也需要設置, 查詢文檔: https://guides.cocoapods.org/syntax/podfile.html#supports_swift_versions

打開ios/Podfile文件

target 'Runner' do
supports_swift_versions '>=4.0', '<5.0'` # add

接着運行項目
Invalid Podfile file: undefined method `supports_swift_versions’

可能是我 cocoapod 版本太低

我升級一下: sudo gem install cocoapods

pod --version
1.7.1

接着運行項目, 這次又有另一個新花招
fatal error: 'swift_plugin/swift_plugin-Swift.h' file not found

告訴我, swift.h 沒找到, 這個是 swift 在編譯過程中自動生成的頭文件

如何修改呢, 還是剛剛主工程的 Podfile 文件

在開頭所有註釋的後面加入一行: use_frameworks!

這個是使用 framework 代替靜態庫, pod 的官方文檔說明: https://guides.cocoapods.org/syntax/podfile.html#use_frameworks_bang

再運行, 終於成功了!

然後刪除掉supports_swift_versions,再運行,我這裏沒發現問題,所以這一步其實是可以忽略的

總結下

  1. 聯繫作者添加 swift_version 的版本號(通常是 4.0),
    1. 如果是自己的項目可以查看 xcode 獲得
    2. 別人的項目
      1. 如果作者添加,皆大歡喜
      2. 作者不添加
        1. 嘗試 PR
        2. 自己 fork 或 download 後魔改,使用 git 依賴或者 path 依賴
  2. 升級 cocoapod 的版本(我這裏是 1.7.1)
  3. 在項目 podspec 中加入use_frameworks!

後記

本篇解決了 oc 項目使用 swift 插件的問題, 分享出來方便你我他

example 項目地址: https://github.com/CaiJingLong/flutter-ocproject-use-swift-plugin

以上

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章