有的公司分工比較細,諸如項目打包、發布這些工作,都會有專門的測試人員去負責,這就為開發人員省去了大部分時間。當然,當你看到這篇文章時,就證明你所在的公司并不是這樣。
不過不要擔心,既然你找到了我,我就將Fastlane的使用技巧傳授給你。
Fastlane是麻省理工學院批準的開源項目,可以將Mac、iOS、android項目的自動打包、發布等一系列繁瑣的任務自動化。
Fastlane安裝
-
打開終端輸入xcode-select --install,若提示如下圖,則說明已經安裝了Xcode命令行工具;否則會彈出對話框,選擇安裝即可。
- 輸入ruby -v查看ruby版本,要求2.0及以上版本。可以通過gem管理ruby版本,這里需要注意的是,ruby的鏡像文件路徑已經改為https://gems.ruby-china.org/
-
輸入sudo gem install fastlane -NV ,通過gem安轉fastlane。最近因為Xcode 9的問題,升級了fastlane。
Fastlane配置
- 打開終端,切換目錄到包含xxx.xcodeproj的項目目錄下輸入fastlane init,期間會讓輸入Apple ID(開發者賬號)及app_identifier等信息,可以根據需要自行選擇填寫。最后會在當前目錄下生成fastlane文件夾。
-
進入fastlane文件夾,打開Appfile文件,里面是剛剛填寫的一些信息。可以在里面配置多個app_identifier、apple_id信息。
- 打開Fastfile文件,里面便是自動生成的fastlane使用方法,當然,需要根據需要進行修改。
在編寫fastfile文件之前,需要說明一下,Fastlane著實太強大,因此本文只介紹其中的一種方法:本機已經安裝Signing Certificate及其對應的Provisioning Profile,也就是說打開Xcode,將Automatically manage signing選項去掉,手動進行選擇,且能編譯運行。
Fastfile文件的編寫
-
App Store版本
# You can define as many lanes as you want
desc "Deploy a new version to the App Store"
lane :release do |op|
increment_version_number(version_number: op[:version]) #根據入參version獲取app版本號
increment_build_number(build_number: op[:version]) #將build號設置與app版本號相同# 設置app的info.plist文件項 set_info_plist_value(path: "./xxx/Info.plist", #info.plist文件目錄 key: "UIFileSharingEnabled", # key,將plist文件以Source Code形式打開可查詢對應的key value: false) # value # 設置自定義plist文件項,用于給app配置不同的服務器URL set_info_plist_value(path: "./xxx/hostAddress.plist", key: "host", value: "https:/zhengshiServer:xx/xxx/xxx") # 設置某些服務是否有效 # 還可以使用modify_services,具體參考官網相關文檔 produce( enable_services:{ push_notification: "on", } ) # 更新Provisioning Profile # 在項目當前目錄下創建provisions文件夾,并將App Store版本的.mobileprovision文件保存在里面,名稱隨意。 update_project_provisioning(profile: "./provisions/appstore.mobileprovision") # 更新項目團隊 update_project_team(path: "xxx.xcodeproj", teamid: "5JC8GZ432G") # 開始打包 gym(# use_legacy_build_api: true, # Xcode 9之后,需要去掉 output_name: "appstore", # 輸出的ipa名稱 silent: true, # 隱藏沒有必要的信息 clean: true, # 在構建前先clean configuration: "Release", # 配置為Release版本 codesigning_identity: "iPhone Distribution: xxx Co.,Ltd. (5JC8GZ432G)", # 代碼簽名證書 buildlog_path: "./fastlanelog", # fastlane構建ipa的日志輸出目錄 export_method: "app-store", # Xcode 9增加export_method標簽 output_directory: "/Users/xxx/Desktop") # ipa輸出目錄 end
-
Development版本
desc "Build a new version use the ceshi"
lane :ceshi do |op|
increment_version_number(version_number: op[:version])
increment_build_number(build_number: op[:version])set_info_plist_value(path: "./xxx/Info.plist", key: "UIFileSharingEnabled", value: true) set_info_plist_value(path: "./xxx/hostAddress.plist", key: "host", value: "https:/ceshiServer:xx/xxx/xxx") # 設置某些服務是否有效 # 還可以使用modify_services,具體參考官網相關文檔 produce( enable_services:{ push_notification: "off", } ) # 將Development版本的.mobileprovision文件保存在里面,名稱隨意。 update_project_provisioning(profile: "./provisions/development.mobileprovision") update_project_team(path: "xxx.xcodeproj", teamid: "5JC8GZ432G") gym(# use_legacy_build_api: true, output_name: "ceshi", silent: true, clean: true, configuration: "Debug", buildlog_path: "./fastlanelog", codesigning_identity: "iPhone Developer: xxx (xxxxxxxxxx)", export_method: "development", # Xcode 9增加export_method標簽 output_directory: "/Users/xxx/Desktop" ) end
-
其他版本類似,此處不在給出。其中export_method標簽對應的值有:
- export_method: "development"
- export_method: "enterprise"
- export_method: "app-store"
批量處理
desc "build all version ipa"
lane :all do |op|
t = op[:version]
ceshi version:t
release version:t
end
Fastlane使用
最后,只需在終端(相關項目目錄下)輕輕敲入:
fastlane ceshi version:1.0.0 // 打包ceshi環境ipa,app版本號為1.0.0
fastlane release version:1.0.0 // 打包App Store版本ipa,app版本號為1.0.0
fastlane all version:1.0.0 // 打包ceshi、App Store版本ipa,app版本號為1.0.0
我們便可以去喝咖啡了,執行打包過程就交給fastlane去完成,是不是很爽?
Fastlane還有很多的功能供大家使用,比如match(能夠使團隊通過git同步證書和配置文件)、sigh(生成配置文件)、snapshot(生成截圖)以及git的一些相關操作等等。大家可以到GitHub或者官網進行相關知識的學習。
授人以魚不如授人以漁,傳送門獻上:
GitHub_Fastlane工具文檔
Fastlane官網
關注微信公眾號CodingArtist,可以第一時間得到文章更新通知! _