美文网首页flutter
IOS Flutter混编工程的CI接入--原理篇

IOS Flutter混编工程的CI接入--原理篇

作者: huangjun0 | 来源:发表于2020-05-20 18:01 被阅读0次

    现在公司的绝大多数工程都是使用的iOS和Flutter混编来做的,其中混编的方式是采用的Google推荐的Module方式来做的。具体请参考将 Flutter module 集成到 iOS 项目-选项 A - 使用 CocoaPods 和 Flutter SDK 集成或者Integrate a Flutter module into your iOS project : Option A - Embed with CocoaPods and the Flutter SDK。 但是在这种混编工程中,即使只修改ios部分的代码,编译的时候也会花费一定的时间来编译Flutter工程,这就导致纯iOS的开发效率很低。那如何解决这个问题呢?
    我们首先看一下Google提供的集成步骤:
    1.在 Podfile 中添加下面代码:

    flutter_application_path = '../my_flutter'
    load File.join(flutter_application_path, '.ios', 'Flutter', 'podhelper.rb')
    
    1. 每个需要集成 Flutter 的 [Podfile target][],执行
    `install_all_flutter_pods(flutter_application_path)`:
    
    target 'MyApp' do
      install_all_flutter_pods(flutter_application_path)
    end
    

    也就是说在pod install过程中,会调用flutter_application_path/.ios/ Flutter/ podhelper.rb文件中的install_all_flutter_pods(flutter_application_path)这个方法来做一系列的操作。这个文件其实是从Flutter SDK中的模版文件拷贝过来的,具体可以查看podhelper.rb.tmpl

    我们看一下install_all_flutter_pods方法:

    def install_all_flutter_pods(flutter_application_path = nil)
      flutter_application_path ||= File.join('..', '..')
      install_flutter_engine_pod
      install_flutter_plugin_pods(flutter_application_path)
      install_flutter_application_pod(flutter_application_path)
    end
    

    其实主要步骤就是:

    1. 在podFile中指定flutter_engine的pod库地址:
      也就是在podfile中加入
      pod 'Flutter', :path => relative.to_s, :inhibit_warnings => true,其中relative.to_s就是方法中需要指定的地址。
    def install_flutter_engine_pod
      engine_dir = File.join(__dir__, 'engine')
      if !File.exist?(engine_dir)
        # Copy the debug engine to have something to link against if the xcode backend script has not run yet.
        # CocoaPods will not embed the framework on pod install (before any build phases can generate) if the dylib does not exist.
        debug_framework_dir = File.join(flutter_root, 'bin', 'cache', 'artifacts', 'engine', 'ios')
        FileUtils.mkdir_p(engine_dir)
        FileUtils.cp_r(File.join(debug_framework_dir, 'Flutter.framework'), engine_dir)
        FileUtils.cp(File.join(debug_framework_dir, 'Flutter.podspec'), engine_dir)
      end
    
      # Keep pod path relative so it can be checked into Podfile.lock.
      # Process will be run from project directory.
      engine_pathname = Pathname.new engine_dir
      project_directory_pathname = Pathname.new Dir.pwd
      relative = engine_pathname.relative_path_from project_directory_pathname
    
      pod 'Flutter', :path => relative.to_s, :inhibit_warnings => true
    end
    

    2.在podFile中指定flutter_plugin的pod库地址:
    读取Flutter工程目录下的.flutter-plugins文件,读取你所引用的所有plugin,然后逐个加入到podfile中。

    def install_flutter_plugin_pods(flutter_application_path)
      flutter_application_path ||= File.join('..', '..')
    
      # Keep pod path relative so it can be checked into Podfile.lock.
      # Process will be run from project directory.
      current_directory_pathname = Pathname.new __dir__
      project_directory_pathname = Pathname.new Dir.pwd
      relative = current_directory_pathname.relative_path_from project_directory_pathname
      pod 'FlutterPluginRegistrant', :path => File.join(relative, 'FlutterPluginRegistrant'), :inhibit_warnings => true
    
      symlinks_dir = File.join(relative, '.symlinks')
      FileUtils.mkdir_p(symlinks_dir)
      plugin_pods = parse_KV_file(File.join(flutter_application_path, '.flutter-plugins'))
      plugin_pods.map do |r|
        symlink = File.join(symlinks_dir, r[:name])
        FileUtils.rm_f(symlink)
        File.symlink(r[:path], symlink)
        pod r[:name], :path => File.join(symlink, 'ios'), :inhibit_warnings => true
      end
    end
    
    .flutter-plugins文件示例

    3.在podFile中指定flutter_application的pod库地址:
    也就是在podfile中加入
    pod 'flutter_xxx_xxx', :path => relative.to_s, :inhibit_warnings => true,其中relative.to_s就是方法中需要指定的地址。
    然后在Xcode的build Phase中增加一个Flutter Build Script的过程,这个过程中执行的脚本是FLUTTER_ROOT/packages/flutter_tools/bin/xcode_backend.sh

    def install_flutter_application_pod(flutter_application_path)
      app_framework_dir = File.join(__dir__, 'App.framework')
      app_framework_dylib = File.join(app_framework_dir, 'App')
      if !File.exist?(app_framework_dylib)
        # Fake an App.framework to have something to link against if the xcode backend script has not run yet.
        # CocoaPods will not embed the framework on pod install (before any build phases can run) if the dylib does not exist.
        # Create a dummy dylib.
        FileUtils.mkdir_p(app_framework_dir)
        `echo "static const int Moo = 88;" | xcrun clang -x c -dynamiclib -o "#{app_framework_dylib}" -`
      end
    
      # Keep pod and script phase paths relative so they can be checked into source control.
      # Process will be run from project directory.
      current_directory_pathname = Pathname.new __dir__
      project_directory_pathname = Pathname.new Dir.pwd
      relative = current_directory_pathname.relative_path_from project_directory_pathname
      pod 'flutter_xxx_xxx', :path => relative.to_s, :inhibit_warnings => true
    
      flutter_export_environment_path = File.join('${SRCROOT}', relative, 'flutter_export_environment.sh');
      script_phase :name => 'Run Flutter Build Script',
        :script => "set -e\nset -u\nsource \"#{flutter_export_environment_path}\"\n\"$FLUTTER_ROOT\"/packages/flutter_tools/bin/xcode_backend.sh build",
        :input_files => [
          File.join('${SRCROOT}', flutter_application_path, '.metadata'),
          File.join('${SRCROOT}', relative, 'App.framework', 'App'),
          File.join('${SRCROOT}', relative, 'engine', 'Flutter.framework', 'Flutter'),
          flutter_export_environment_path
        ],
        :execution_position => :before_compile
    end
    

    因此整个流程也就变成,在podfile中加入flutter_engine(Flutter.framework)的pod库、flutter_plugin的pod库和flutter_application(App.framework)的pod库,然后编译整个flutter工程。

    那如果我们现在想要将iOS工程与Flutter工程相隔离就需要改造整个流程。
    原来的流程:在podfile中加入所有的Flutter相关的Pod库--编译Flutter工程产出相关的Pod库文件--编译iOS工程。


    ios_flutter_origin.jpg

    那既然每次编译以后,都会产出这三种产物,那我们在每次发布release包的时候获取这三种产物,然后把他们上传到git库中。然后在iOS工程中创建一个submodule,指向这个git库,然后修改podfile中的编译流程,修改这三种产物的pod库地址从Flutter工程相关的地址为本地的submoudle的地址,然后直接编译iOS工程即可,即避免了每次编译Flutter工程,又能做到iOS工程和Flutter工程的彻底隔绝。


    ios_flutter_after.jpg

    下一篇会详细讲一下如何在项目中进行实践。

    相关文章

      网友评论

        本文标题:IOS Flutter混编工程的CI接入--原理篇

        本文链接:https://www.haomeiwen.com/subject/okynohtx.html