美文网首页
2023-02-21 iOS工程接入flutter模块的实现流程

2023-02-21 iOS工程接入flutter模块的实现流程

作者: 我是小胡胡分胡 | 来源:发表于2023-02-21 17:25 被阅读0次

    IOSapp接入FlutterModule按照官方文档操作,我们使用使用 CocoaPods 和 Flutter SDK 集成:
    https://flutter.cn/docs/development/add-to-app/ios/project-setup

    IOSapp代码中接入Flutter页面方式:
    https://flutter.cn/docs/development/add-to-app/ios/add-flutter-screen?tab=engine-uikit-objc-tab

    在工程中我们没有看到App.framework
    Flutter.framework,这些怎么在编译的时候又加进来的呢?

    image.png

    可以看到编译app后 App.framework 和Flutter.framework链接进来的:

    image.png

    这其实就是flutter 的podhelper.rb中完成的操作:

    • 第1步, flutter pub get 生成, .ios/Flutter/podhelper.rb
    • 第2步,在app的工程中的podfile添加脚本
    flutter_application_path = '../fluttermodule'
    load File.join(flutter_application_path, '.ios', 'Flutter', 'podhelper.rb')
    
    target 'flutter-app' do
      use_frameworks!
      install_all_flutter_pods(flutter_application_path)
    end
    
    post_install do |installer|
      flutter_post_install(installer) if defined?(flutter_post_install)
    end
    

    在 .ios/Flutter/podhelper.rb 的方法 install_all_flutter_pods

    def install_all_flutter_pods(flutter_application_path = nil)
      # defined_in_file is a Pathname to the Podfile set by CocoaPods.
      pod_contents = File.read(defined_in_file)
      unless pod_contents.include? 'flutter_post_install'
        puts  <<~POSTINSTALL
    Add `flutter_post_install(installer)` to your Podfile `post_install` block to build Flutter plugins:
    
    post_install do |installer|
      flutter_post_install(installer)
    end
    POSTINSTALL
        raise 'Missing `flutter_post_install(installer)` in Podfile `post_install` block'
      end
    
      require File.expand_path(File.join('packages', 'flutter_tools', 'bin', 'podhelper'), flutter_root)
    
      flutter_application_path ||= File.join('..', '..')
      install_flutter_engine_pod(flutter_application_path)
      install_flutter_plugin_pods(flutter_application_path)
      install_flutter_application_pod(flutter_application_path)
    end
    
    1. install_flutter_engine_pod(flutter_application_path)方法:
    def install_flutter_engine_pod(flutter_application_path = nil)
      flutter_application_path ||= File.join('..', '..')
      ios_application_path = File.join(flutter_application_path, '.ios')
    
      # flutter_install_ios_engine_pod is in Flutter root podhelper.rb
      flutter_install_ios_engine_pod(ios_application_path)
    end
    

    这个方法会调用flutter 中的podhelper.rb脚本: flutter_tools/bin/podhelper.rb

    def flutter_install_ios_engine_pod(ios_application_path = nil)
      # defined_in_file is set by CocoaPods and is a Pathname to the Podfile.
      ios_application_path ||= File.dirname(defined_in_file.realpath) if self.respond_to?(:defined_in_file)
      raise 'Could not find iOS application path' unless ios_application_path
    
      podspec_directory = File.join(ios_application_path, 'Flutter')
      copied_podspec_path = File.expand_path('Flutter.podspec', podspec_directory)
    
      # Generate a fake podspec to represent the Flutter framework.
      # This is only necessary because plugin podspecs contain `s.dependency 'Flutter'`, and if this Podfile
      # does not add a `pod 'Flutter'` CocoaPods will try to download it from the CocoaPods trunk.
      File.open(copied_podspec_path, 'w') { |podspec|
        podspec.write <<~EOF
          #
          # NOTE: This podspec is NOT to be published. It is only used as a local source!
          #       This is a generated file; do not edit or check into version control.
          #
    
          Pod::Spec.new do |s|
            s.name             = 'Flutter'
            s.version          = '1.0.0'
            s.summary          = 'A UI toolkit for beautiful and fast apps.'
            s.homepage         = 'https://flutter.dev'
            s.license          = { :type => 'BSD' }
            s.author           = { 'Flutter Dev Team' => 'flutter-dev@googlegroups.com' }
            s.source           = { :git => 'https://github.com/flutter/engine', :tag => s.version.to_s }
            s.ios.deployment_target = '11.0'
            # Framework linking is handled by Flutter tooling, not CocoaPods.
            # Add a placeholder to satisfy `s.dependency 'Flutter'` plugin podspecs.
            s.vendored_frameworks = 'path/to/nothing1234567'
          end
        EOF
      }
    
      # Keep pod path relative so it can be checked into Podfile.lock.
      pod 'Flutter', :path => flutter_relative_path_from_podfile(podspec_directory)
    end
    

    这里作用就是创建一个Flutter的podspec,导入Flutter pod仓库。但是这时候只是提供一个pod 名。 实际上Flutter.framework并没有导入到app工程中。


    image.png
    2. install_flutter_plugin_pods
    def install_flutter_plugin_pods(flutter_application_path)
      flutter_application_path ||= File.join('..', '..')
      ios_application_path = File.join(flutter_application_path, '.ios')
      # flutter_install_plugin_pods is in Flutter root podhelper.rb
      flutter_install_plugin_pods(ios_application_path, '.symlinks', 'ios')
    
      # Keep pod path relative so it can be checked into Podfile.lock.
      relative = flutter_relative_path_from_podfile(ios_application_path)
      pod 'FlutterPluginRegistrant', :path => File.join(relative, 'Flutter', 'FlutterPluginRegistrant'), :inhibit_warnings => true
    end
    

    这里是安装FlutterPluginRegistrant 仓库

    3. install_flutter_application_pod
    def install_flutter_application_pod(flutter_application_path)
      flutter_application_path ||= File.join('..', '..')
    
      export_script_directory = File.join(flutter_application_path, '.ios', 'Flutter')
    
      # Keep script phase paths relative so they can be checked into source control.
      relative = flutter_relative_path_from_podfile(export_script_directory)
    
      flutter_export_environment_path = File.join('${SRCROOT}', relative, 'flutter_export_environment.sh');
    
      # Compile App.framework and move it and Flutter.framework to "BUILT_PRODUCTS_DIR"
      script_phase :name => 'Run Flutter Build fluttermodule Script',
        :script => "set -e\nset -u\nsource \"#{flutter_export_environment_path}\"\nexport VERBOSE_SCRIPT_LOGGING=1 && \"$FLUTTER_ROOT\"/packages/flutter_tools/bin/xcode_backend.sh build",
        :execution_position => :before_compile
    
      # Embed App.framework AND Flutter.framework.
      script_phase :name => 'Embed Flutter Build fluttermodule Script',
        :script => "set -e\nset -u\nsource \"#{flutter_export_environment_path}\"\nexport VERBOSE_SCRIPT_LOGGING=1 && \"$FLUTTER_ROOT\"/packages/flutter_tools/bin/xcode_backend.sh embed_and_thin",
        :execution_position => :after_compile
    end
    

    script_phase 这个是一个ruby函数,他的实现应该是cocoapods提供的基本功能。
    作用就是给xcode工程编译的时候插入脚本,
    before_compile,after_compile 编译前和编译后,分别执行脚本。


    image.png

    我们可以学习这种写入脚本的方法,写一些别的脚本干点事情,例如:

     
    def install_flutter_application_pod
      flutter_export_environment_path = '123'
      script_phase :name => 'Run Flutter Build fluttermodule Script',
        :script => "set -e\nset -u\nsource \"#{flutter_export_environment_path}\"\nexport VERBOSE_SCRIPT_LOGGING=1 && \"$FLUTTER_ROOT\"/packages/flutter_tools/bin/xcode_backend.sh build",
        :execution_position => :before_compile
      script_phase :name => 'Embed Flutter Build fluttermodule Script',
        :script => "set -e\nset -u\nsource \"#{flutter_export_environment_path}\"\nexport VERBOSE_SCRIPT_LOGGING=1 && \"$FLUTTER_ROOT\"/packages/flutter_tools/bin/xcode_backend.sh embed_and_thin",
        :execution_position => :after_compile
    end
    
    target 'tt' do
      use_frameworks!
      install_flutter_application_pod
    end
    
    4. flutter_post_install
    post_install do |installer|
      flutter_post_install(installer) if defined?(flutter_post_install)
    end
    
    • 首先是flutter module里面的podhelper.rb
    def flutter_post_install(installer, skip: false)
      return if skip
    
      installer.pods_project.targets.each do |target|
        target.build_configurations.each do |build_configuration|
          # flutter_additional_ios_build_settings is in Flutter root podhelper.rb
          flutter_additional_ios_build_settings(target)
        end
      end
    end
    
    • 然后是flutter里面的podhelper.rb
      flutter_additional_ios_build_settings
    def flutter_additional_ios_build_settings(target)
      return unless target.platform_name == :ios
    
      # [target.deployment_target] is a [String] formatted as "8.0".
      inherit_deployment_target = target.deployment_target[/\d+/].to_i < 11
    
      # This podhelper script is at $FLUTTER_ROOT/packages/flutter_tools/bin.
      # Add search paths from $FLUTTER_ROOT/bin/cache/artifacts/engine.
      artifacts_dir = File.join('..', '..', '..', '..', 'bin', 'cache', 'artifacts', 'engine')
      debug_framework_dir = File.expand_path(File.join(artifacts_dir, 'ios', 'Flutter.xcframework'), __FILE__)
    
      unless Dir.exist?(debug_framework_dir)
        # iOS artifacts have not been downloaded.
        raise "#{debug_framework_dir} must exist. If you're running pod install manually, make sure \"flutter precache --ios\" is executed first"
      end
    
      release_framework_dir = File.expand_path(File.join(artifacts_dir, 'ios-release', 'Flutter.xcframework'), __FILE__)
      # Bundles are com.apple.product-type.bundle, frameworks are com.apple.product-type.framework.
      target_is_resource_bundle = target.respond_to?(:product_type) && target.product_type == 'com.apple.product-type.bundle'
    
      target.build_configurations.each do |build_configuration|
        # Build both x86_64 and arm64 simulator archs for all dependencies. If a single plugin does not support arm64 simulators,
        # the app and all frameworks will fall back to x86_64. Unfortunately that case is not detectable in this script.
        # Therefore all pods must have a x86_64 slice available, or linking a x86_64 app will fail.
        build_configuration.build_settings['ONLY_ACTIVE_ARCH'] = 'NO' if build_configuration.type == :debug
    
        # Workaround https://github.com/CocoaPods/CocoaPods/issues/11402, do not sign resource bundles.
        if target_is_resource_bundle
          build_configuration.build_settings['CODE_SIGNING_ALLOWED'] = 'NO'
          build_configuration.build_settings['CODE_SIGNING_REQUIRED'] = 'NO'
          build_configuration.build_settings['CODE_SIGNING_IDENTITY'] = '-'
          build_configuration.build_settings['EXPANDED_CODE_SIGN_IDENTITY'] = '-'
        end
    
        # Skip other updates if it's not a Flutter plugin (transitive dependency).
        next unless target.dependencies.any? { |dependency| dependency.name == 'Flutter' }
    
        # Profile can't be derived from the CocoaPods build configuration. Use release framework (for linking only).
        configuration_engine_dir = build_configuration.type == :debug ? debug_framework_dir : release_framework_dir
        Dir.new(configuration_engine_dir).each_child do |xcframework_file|
          next if xcframework_file.start_with?(".") # Hidden file, possibly on external disk.
          if xcframework_file.end_with?("-simulator") # ios-arm64_x86_64-simulator
            build_configuration.build_settings['FRAMEWORK_SEARCH_PATHS[sdk=iphonesimulator*]'] = "\"#{configuration_engine_dir}/#{xcframework_file}\" $(inherited)"
          elsif xcframework_file.start_with?("ios-") # ios-arm64
            build_configuration.build_settings['FRAMEWORK_SEARCH_PATHS[sdk=iphoneos*]'] = "\"#{configuration_engine_dir}/#{xcframework_file}\" $(inherited)"
          else
            # Info.plist or another platform.
          end
        end
        build_configuration.build_settings['OTHER_LDFLAGS'] = '$(inherited) -framework Flutter'
    
        build_configuration.build_settings['CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER'] = 'NO'
        # Suppress warning when pod supports a version lower than the minimum supported by Xcode (Xcode 12 - iOS 9).
        # This warning is harmless but confusing--it's not a bad thing for dependencies to support a lower version.
        # When deleted, the deployment version will inherit from the higher version derived from the 'Runner' target.
        # If the pod only supports a higher version, do not delete to correctly produce an error.
        build_configuration.build_settings.delete 'IPHONEOS_DEPLOYMENT_TARGET' if inherit_deployment_target
    
        # Override legacy Xcode 11 style VALID_ARCHS[sdk=iphonesimulator*]=x86_64 and prefer Xcode 12 EXCLUDED_ARCHS.
        build_configuration.build_settings['VALID_ARCHS[sdk=iphonesimulator*]'] = '$(ARCHS_STANDARD)'
        build_configuration.build_settings['EXCLUDED_ARCHS[sdk=iphonesimulator*]'] = '$(inherited) i386'
        build_configuration.build_settings['EXCLUDED_ARCHS[sdk=iphoneos*]'] = '$(inherited) armv7'
      end
    end
    

    主要作用就是设置 FRAMEWORK_SEARCH_PATHS ,OTHER_LDFLAGS


    image.png

    看到了吗,编译时候查找的是flutter中的flutter.framework,这个目录下有各种架构和版本的Flutter.framework,真机,模拟器,release,debug,profile各种:
    /Volumes/huc/myshared/opt/fvm/versions/3.3.7/bin/cache/artifacts/engine/ios/


    image.png
    5. xcode_backend.sh
    set -e
    set -u
    source "${SRCROOT}/../fluttermodule/.ios/Flutter/flutter_export_environment.sh"
    export VERBOSE_SCRIPT_LOGGING=1 && "$FLUTTER_ROOT"/packages/flutter_tools/bin/xcode_backend.sh build
    

    xcode_backend.sh 调用xcode_backend.dart

    我们修改一下runSync函数, 让所有脚本都输出日志:

        //if (verbose) {
          print('♦ $bin ${args.join(' ')}');
        //}
    
    image.png

    最后可以导出日志文件进行匹配,可以看看执行了哪些操作:

    Searching 1 file for "♦ " (regex)
    
    ~/Desktop/Build flutter-app_2023-02-22T14-20-33.txt:
      667: ♦ /Volumes/huc/myshared/opt/fvm/versions/3.3.7/bin/flutter --verbose assemble --no-version-check --output=/Users/a58/Library/Developer/Xcode/DerivedData/flutter-app-dknedoeqlmcgtfafasrahlcseagi/Build/Products/Debug-iphonesimulator/ -dTargetPlatform=ios -dTargetFile=lib/main.dart -dBuildMode=debug -dIosArchs=x86_64 -dSdkRoot=/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator16.0.sdk -dSplitDebugInfo= -dTreeShakeIcons=false -dTrackWidgetCreation=true -dDartObfuscation=false -dEnableBitcode= --ExtraGenSnapshotOptions= --DartDefines= --ExtraFrontEndOptions= -dCodesignIdentity=- debug_ios_bundle_flutter_assets
    
      
      742: ♦ /Volumes/huc/myshared/opt/fvm/versions/3.3.7/bin/flutter --verbose assemble --no-version-check --output=/Users/a58/Library/Developer/Xcode/DerivedData/flutter-app-dknedoeqlmcgtfafasrahlcseagi/Build/Products/Debug-iphonesimulator/ -dTargetPlatform=ios -dTargetFile=lib/main.dart -dBuildMode=debug -dIosArchs=x86_64 -dSdkRoot=/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator16.0.sdk -dSplitDebugInfo= -dTreeShakeIcons=false -dTrackWidgetCreation=true -dDartObfuscation=false -dEnableBitcode= --ExtraGenSnapshotOptions= --DartDefines= --ExtraFrontEndOptions= -dCodesignIdentity=- debug_ios_bundle_flutter_assets
    
    
    
    
     1475: ♦ mkdir -p -- /Users/a58/Library/Developer/Xcode/DerivedData/flutter-app-dknedoeqlmcgtfafasrahlcseagi/Build/Products/Debug-iphonesimulator/flutter-app.app/Frameworks
     1476: ♦ rsync -8 -av --delete --filter - .DS_Store /Users/a58/Library/Developer/Xcode/DerivedData/flutter-app-dknedoeqlmcgtfafasrahlcseagi/Build/Products/Debug-iphonesimulator/App.framework /Users/a58/Library/Developer/Xcode/DerivedData/flutter-app-dknedoeqlmcgtfafasrahlcseagi/Build/Products/Debug-iphonesimulator/flutter-app.app/Frameworks
     1477: ♦ rsync -av --delete --filter - .DS_Store /Users/a58/Library/Developer/Xcode/DerivedData/flutter-app-dknedoeqlmcgtfafasrahlcseagi/Build/Products/Debug-iphonesimulator/Flutter.framework /Users/a58/Library/Developer/Xcode/DerivedData/flutter-app-dknedoeqlmcgtfafasrahlcseagi/Build/Products/Debug-iphonesimulator/flutter-app.app/Frameworks/
     1478: ♦ plutil -extract NSBonjourServices xml1 -o - /Users/a58/Library/Developer/Xcode/DerivedData/flutter-app-dknedoeqlmcgtfafasrahlcseagi/Build/Products/Debug-iphonesimulator/flutter-app.app/Info.plist
     1479: ♦ plutil -insert NSBonjourServices -json ["_dartobservatory._tcp"] /Users/a58/Library/Developer/Xcode/DerivedData/flutter-app-dknedoeqlmcgtfafasrahlcseagi/Build/Products/Debug-iphonesimulator/flutter-app.app/Info.plist
     1480: ♦ plutil -extract NSLocalNetworkUsageDescription xml1 -o - /Users/a58/Library/Developer/Xcode/DerivedData/flutter-app-dknedoeqlmcgtfafasrahlcseagi/Build/Products/Debug-iphonesimulator/flutter-app.app/Info.plist
     1481: ♦ plutil -insert NSLocalNetworkUsageDescription -string Allow Flutter tools on your computer to connect and debug your application. This prompt will not appear on release builds. /Users/a58/Library/Developer/Xcode/DerivedData/flutter-app-dknedoeqlmcgtfafasrahlcseagi/Build/Products/Debug-iphonesimulator/flutter-app.app/Info.plist
    
    
    
    
     1483: ♦ mkdir -p -- /Users/a58/Library/Developer/Xcode/DerivedData/flutter-app-dknedoeqlmcgtfafasrahlcseagi/Build/Products/Debug-iphonesimulator/flutter-app.app/Frameworks
     1485: ♦ rsync -8 -av --delete --filter - .DS_Store /Users/a58/Library/Developer/Xcode/DerivedData/flutter-app-dknedoeqlmcgtfafasrahlcseagi/Build/Products/Debug-iphonesimulator/App.framework /Users/a58/Library/Developer/Xcode/DerivedData/flutter-app-dknedoeqlmcgtfafasrahlcseagi/Build/Products/Debug-iphonesimulator/flutter-app.app/Frameworks
     1487: ♦ rsync -av --delete --filter - .DS_Store /Users/a58/Library/Developer/Xcode/DerivedData/flutter-app-dknedoeqlmcgtfafasrahlcseagi/Build/Products/Debug-iphonesimulator/Flutter.framework /Users/a58/Library/Developer/Xcode/DerivedData/flutter-app-dknedoeqlmcgtfafasrahlcseagi/Build/Products/Debug-iphonesimulator/flutter-app.app/Frameworks/
     1489: ♦ plutil -extract NSBonjourServices xml1 -o - /Users/a58/Library/Developer/Xcode/DerivedData/flutter-app-dknedoeqlmcgtfafasrahlcseagi/Build/Products/Debug-iphonesimulator/flutter-app.app/Info.plist
     1491: ♦ plutil -insert NSBonjourServices -json ["_dartobservatory._tcp"] /Users/a58/Library/Developer/Xcode/DerivedData/flutter-app-dknedoeqlmcgtfafasrahlcseagi/Build/Products/Debug-iphonesimulator/flutter-app.app/Info.plist
     1493: ♦ plutil -extract NSLocalNetworkUsageDescription xml1 -o - /Users/a58/Library/Developer/Xcode/DerivedData/flutter-app-dknedoeqlmcgtfafasrahlcseagi/Build/Products/Debug-iphonesimulator/flutter-app.app/Info.plist
     1495: ♦ plutil -insert NSLocalNetworkUsageDescription -string Allow Flutter tools on your computer to connect and debug your application. This prompt will not appear on release builds. /Users/a58/Library/Developer/Xcode/DerivedData/flutter-app-dknedoeqlmcgtfafasrahlcseagi/Build/Products/Debug-iphonesimulator/flutter-app.app/Info.plist
    
    16 matches in 1 file
    
    

    通过从日志匹配,看到以上操作。

    • 编译App.framework
    • 拷贝App.framework到 flutter-app.app/Frameworks包下面
    • 拷贝Flutter.framework到app包
    • Info.plist添加NSBonjourServices配置
    • Info.plist添加NSLocalNetworkUsageDescription配置
    image.png

    这里用到了shell命令,rsync来拷贝文件,plutil来操作plist。

    可见我们在开发flutter模块过程中, app用接入flutter模块这种用flutter源代码方式接入的开发方式下,

    • flutter测新添加了pub仓库,执行flutter pub get,然后执行pod install 同步。
    • 如果没有新加pub,只是修改dart代码,ios这边只需要重新build 就可以同步最新dart代码。

    到此,ios工程接入flutter module的开发过程就分析完成。

    相关文章

      网友评论

          本文标题:2023-02-21 iOS工程接入flutter模块的实现流程

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