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

現(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')
  1. 每個需要集成 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í)主要步驟就是:

  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的過程驾茴,這個過程中執(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工程裹赴。


ios_flutter_origin.jpg

那既然每次編譯以后喜庞,都會產(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工程的徹底隔絕。


ios_flutter_after.jpg

下一篇會詳細(xì)講一下如何在項目中進(jìn)行實(shí)踐验夯。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末猖吴,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子挥转,更是在濱河造成了極大的恐慌海蔽,老刑警劉巖共屈,帶你破解...
    沈念sama閱讀 217,509評論 6 504
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異党窜,居然都是意外死亡拗引,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,806評論 3 394
  • 文/潘曉璐 我一進(jìn)店門幌衣,熙熙樓的掌柜王于貴愁眉苦臉地迎上來矾削,“玉大人,你說我怎么就攤上這事豁护≌恚” “怎么了?”我有些...
    開封第一講書人閱讀 163,875評論 0 354
  • 文/不壞的土叔 我叫張陵择镇,是天一觀的道長挡逼。 經(jīng)常有香客問我,道長腻豌,這世上最難降的妖魔是什么家坎? 我笑而不...
    開封第一講書人閱讀 58,441評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮吝梅,結(jié)果婚禮上虱疏,老公的妹妹穿的比我還像新娘。我一直安慰自己苏携,他們只是感情好做瞪,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,488評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著右冻,像睡著了一般装蓬。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上纱扭,一...
    開封第一講書人閱讀 51,365評論 1 302
  • 那天牍帚,我揣著相機(jī)與錄音,去河邊找鬼乳蛾。 笑死暗赶,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的肃叶。 我是一名探鬼主播蹂随,決...
    沈念sama閱讀 40,190評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼因惭!你這毒婦竟也來了岳锁?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,062評論 0 276
  • 序言:老撾萬榮一對情侶失蹤筛欢,失蹤者是張志新(化名)和其女友劉穎浸锨,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體版姑,經(jīng)...
    沈念sama閱讀 45,500評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡柱搜,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,706評論 3 335
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了剥险。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片聪蘸。...
    茶點(diǎn)故事閱讀 39,834評論 1 347
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖表制,靈堂內(nèi)的尸體忽然破棺而出健爬,到底是詐尸還是另有隱情,我是刑警寧澤么介,帶...
    沈念sama閱讀 35,559評論 5 345
  • 正文 年R本政府宣布娜遵,位于F島的核電站,受9級特大地震影響壤短,放射性物質(zhì)發(fā)生泄漏设拟。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,167評論 3 328
  • 文/蒙蒙 一久脯、第九天 我趴在偏房一處隱蔽的房頂上張望纳胧。 院中可真熱鬧,春花似錦帘撰、人聲如沸跑慕。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,779評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽核行。三九已至,卻和暖如春蹬耘,著一層夾襖步出監(jiān)牢的瞬間钮科,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,912評論 1 269
  • 我被黑心中介騙來泰國打工婆赠, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留绵脯,地道東北人。 一個月前我還...
    沈念sama閱讀 47,958評論 2 370
  • 正文 我出身青樓休里,卻偏偏與公主長得像蛆挫,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子妙黍,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,779評論 2 354

推薦閱讀更多精彩內(nèi)容