現(xiàn)在公司的絕大多數(shù)工程都是使用的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部分的代碼阅畴,編譯的時候也會花費(fèi)一定的時間來編譯Flutter工程抗果,這就導(dǎo)致純iOS的開發(fā)效率很低筋帖。那如何解決這個問題呢?
我們首先看一下Google提供的集成步驟:
1.在 Podfile
中添加下面代碼:
flutter_application_path = '../my_flutter'
load File.join(flutter_application_path, '.ios', 'Flutter', 'podhelper.rb')
- 每個需要集成 Flutter 的 [Podfile target][]冤馏,執(zhí)行
`install_all_flutter_pods(flutter_application_path)`:
target 'MyApp' do
install_all_flutter_pods(flutter_application_path)
end
也就是說在pod install過程中日麸,會調(diào)用flutter_application_path/.ios/ Flutter/ podhelper.rb文件中的install_all_flutter_pods(flutter_application_path)這個方法來做一系列的操作。這個文件其實(shí)是從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
其實(shí)主要步驟就是:
- 在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
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的過程驾茴,這個過程中執(zhí)行的腳本是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工程恤批。
那如果我們現(xiàn)在想要將iOS工程與Flutter工程相隔離就需要改造整個流程。
原來的流程:在podfile中加入所有的Flutter相關(guān)的Pod庫--編譯Flutter工程產(chǎn)出相關(guān)的Pod庫文件--編譯iOS工程裹赴。
那既然每次編譯以后喜庞,都會產(chǎn)出這三種產(chǎn)物,那我們在每次發(fā)布release包的時候獲取這三種產(chǎn)物棋返,然后把他們上傳到git庫中延都。然后在iOS工程中創(chuàng)建一個submodule,指向這個git庫睛竣,然后修改podfile中的編譯流程晰房,修改這三種產(chǎn)物的pod庫地址從Flutter工程相關(guān)的地址為本地的submoudle的地址,然后直接編譯iOS工程即可射沟,即避免了每次編譯Flutter工程殊者,又能做到iOS工程和Flutter工程的徹底隔絕。
下一篇會詳細(xì)講一下如何在項目中進(jìn)行實(shí)踐验夯。