Flutter module方式集成到iOS工程中

本篇讲述Flutter module如何集成、调试、打包发布三个问题

一、集成

Flutter module集成方式有三种:pod管理集成(简单快捷本篇是基于此)、手动通过flutter build ios-framework生成并管理framework、最后是flutter build ios-framework --cocoapods(需要flutter SDK v1.13.6及以上才支持);pod集成方式步骤如下:

  • 在主工程某个目录下面执行flutter create --template module my_flutter,本工程是在podfile目录下执行
  • cd 到my_flutter 目录,执行flutter packages get
  • 编辑podfile, 添加如下代码
target ‘hunHePrg’ do
  
  flutter_application_path = './my_flutter'
  load File.join(flutter_application_path, '.ios', 'Flutter', 'podhelper.rb')
  install_all_flutter_pods(flutter_application_path)

    pod 'SVProgressHUD'
end

  • 返回上一个目录,执行pod install
  • 至此就集成好了,后续pubspec.yaml文件发生变化重新执行 flutter packages get然后pod install就可以了

二、调试

这里使用的是vs code 编辑器,打开my_flutter代码

1,不需要断点,

  • 确保已连接设备,然后在终端执行 flutter attach
  • 待出现waiting for a connect后,在Xcode端运行主工程
Flutter module方式集成到iOS工程中_第1张图片
image.png

2,断点调试

  • vscode 打开设置-->command palette--->attach to flutter on device;然后就会出现调试面板
    *待 Waiting for a connection from Flutter on 出现后运行xcode 主工程
Flutter module方式集成到iOS工程中_第2张图片
image.png

三、打包

  • 在 my_flutter 目录下执行flutter build ios --release --no-codesign
    目的是生成flutter 相关的release 产物,生成framework 产物不需要签名,所以--no-codesign,否则就需要配置./.ios目录下的证书,在脚本打包场景中相当麻烦

  • xcode 目录下执行 pod install;后续就按照xcode流程打包

遇到的问题

错误1

ERROR: Unknown FLUTTER_BUILD_MODE: dailybuild.

Valid values are 'Debug', 'Profile', or 'Release' (case insensitive).

This is controlled by the FLUTTER_BUILD_MODE environment variable.

If that is not set, the CONFIGURATION environment variable is used.



You can fix this by either adding an appropriately named build

configuration, or adding an appropriate value for FLUTTER_BUILD_MODE to the

.xcconfig file for the current build configuration (DailyBuild).

========================================================================

Command /bin/sh failed with exit code 255

解决方案

  • 方案1,Xcode工程中配置FLUTTER_BUILD_MODE环境变量
    这种设置可能不会生效,至少我设置没有生效
Flutter module方式集成到iOS工程中_第3张图片
image.png
  • 方案2 在打包脚本中把 FLUTTER_BUILD_MODE 写死自己想要的值
    我就是这样干的

  • 方案3 报这个错误的源码在xcode_backend.sh脚本中,相关代码如下

local build_mode="$(echo "${FLUTTER_BUILD_MODE:-${CONFIGURATION}}" | tr "[:upper:]" "[:lower:]")"
  local artifact_variant="unknown"
  case "$build_mode" in
    *release*) build_mode="release"; artifact_variant="ios-release";;
    *profile*) build_mode="profile"; artifact_variant="ios-profile";;
    *debug*) build_mode="debug"; artifact_variant="ios";;
    *)
      EchoError "========================================================================"
      EchoError "ERROR: Unknown FLUTTER_BUILD_MODE: ${build_mode}."
      EchoError "Valid values are 'Debug', 'Profile', or 'Release' (case insensitive)."
      EchoError "This is controlled by the FLUTTER_BUILD_MODE environment variable."
      EchoError "If that is not set, the CONFIGURATION environment variable is used."
      EchoError ""
      EchoError "You can fix this by either adding an appropriately named build"
      EchoError "configuration, or adding an appropriate value for FLUTTER_BUILD_MODE to the"
      EchoError ".xcconfig file for the current build configuration (${CONFIGURATION})."
      EchoError "========================================================================"
      exit -1;;

假如 FLUTTER_BUILD_MODE环境变量读取失败,那么可以将要编译的CONFIGURATION 设置成包含release、profile、debug字符样式,比如我这里的CONFIGURATION 是dailybuild 那就改成 dailybuild_release样式。

你可能感兴趣的:(Flutter module方式集成到iOS工程中)