IOS 自動(dòng)化打包需要幾步:
一、安裝fastlane
1.fastlane是用Ruby語(yǔ)言編寫的工具,需要有Ruby開(kāi)發(fā)環(huán)境,先查看是否安裝
ruby -v
- 如果終端提示一下信息則表示已安裝過(guò):
ruby 2.6.10p210 (2022-04-12 revision 67958) [universal.x86_64-darwin22]
- 沒(méi)安裝過(guò)的可以參考:Ruby 的安裝與卸載
2.安裝fastlane
- 安裝fastlane,安裝過(guò)程中可能會(huì)出現(xiàn)錯(cuò)誤,比如權(quán)限錯(cuò)誤,可根據(jù)錯(cuò)誤提示自行百度
brew install fastlane
- 查看fastlan版本
fastlane --version,提示下面信息,則表示已經(jīng)安裝成功
image.png
3.安裝 Xcode command line tools:
xcode-select--install
-
安裝過(guò)會(huì)提示已經(jīng)安裝
image.png
二、 配置fastlane
1.到項(xiàng)目根路徑執(zhí)行初始化命令
cd 項(xiàng)目跟路徑
fastlane init
終端會(huì)顯示一下內(nèi)容:選擇 4 手動(dòng)設(shè)置,中間會(huì)讓你點(diǎn)擊此 enter 鍵,進(jìn)行確認(rèn)
What would you like to use fastlane for?
///自動(dòng)化截圖
1. ?? Automate screenshots
///
2. ???? Automate beta distribution to TestFlight
///上傳appstore,
3. ?? Automate App Store distribution
/// 自定義上傳
4. ?? Manual setup - manually setup your project to automate your tasks
-
初始化成功后項(xiàng)目中會(huì)多出一下幾個(gè)文件image.png
編輯Fastfile
# Uncomment the line if you want fastlane to automatically update itself
# update_fastlane
fastlane_version "2.214.0"
default_platform(:ios)
# 項(xiàng)目ID
project_identifier_Dev = "com.onlytest.dev"
project_identifier_Dis = "com.onlytest"
# 測(cè)試環(huán)境scheme名稱
project_Dev_scheme = "x_Dev"
# 正式環(huán)境scheme名稱
project_Dis_scheme = "x_Dis"
# 發(fā)布環(huán)境scheme名稱
project_App_scheme = "x_AppStore"
# 項(xiàng)目的描述文件名稱(這里也可以配置一個(gè)通用的Adhoc文件)
project_provisioningProfiles_dev = "onlytest_AdHoc_dev"
project_provisioningProfiles_dis = "onlytest_AdHoc_dis"
project_provisioningProfiles_appstore = "onlytest_appstore"
# 默認(rèn)內(nèi)測(cè)打包方式,目前支持app-store, package, ad-hoc, enterprise, development
# 注:由于如果使用手動(dòng)配置證書(shū),在export_options指定打包方式!
ipa_exportMethod_adhoc = "ad-hoc"
ipa_exportMethod_appstore = "app-store"
#蒲公英pgyer_apiKey和pgyer_userkey 換成你的
pgyer_apiKey ="5db239fd960xxxxxx"
#這個(gè)可以不用
pgyer_userkey ="bf1cd97e6bb3xxxxxx"
currentTime = Time.new.strftime("%Y%m%d")
ipa_name = "應(yīng)用名"
# .ipa文件輸出路徑
ipa_outputDirectory_Debug = "~/Desktop/IPA/#{ipa_name}/Dev/#{currentTime}"
ipa_outputDirectory_Release = "~/Desktop/IPA/#{ipa_name}/Dis/#{currentTime}"
ipa_outputDirectory_AppStore = "~/Desktop/IPA/#{ipa_name}/AppStore/#{currentTime}"
# 計(jì)算buildNumber
def updateProjectBuildNumber
currentTime = Time.new.strftime("%Y%m%d")
build = get_build_number()
if build.include?"#{currentTime}."
# => 為當(dāng)天版本 計(jì)算迭代版本號(hào)
lastStr = build[build.length-2..build.length-1]
lastNum = lastStr.to_i
lastNum = lastNum + 1
lastStr = lastNum.to_s
if lastNum < 10
lastStr = lastStr.insert(0,"0")
end
build = "#{currentTime}.#{lastStr}"
else
# => 非當(dāng)天版本 build 號(hào)重置
build = "#{currentTime}.01"
end
puts("*************| 更新build #{build} |*************")
# => 更改項(xiàng)目 build 號(hào)
increment_build_number(
build_number: "#{build}"
)
end
platform :ios do
desc "Description of what the lane does"
lane :ad_dev do
puts "*************| 開(kāi)始打包.ipa文件... |*************"
# 更新項(xiàng)目build號(hào)
updateProjectBuildNumber
#指定Xcode路徑,裝多個(gè)版本xcode時(shí)指定xcode版本
xcode_select("/Applications/Xcode.app")
#build_app(export_method: "ad-hoc")
# 開(kāi)始打包
gym(
# 指定輸出的ipa名稱
output_name:"#{project_Dev_scheme}_#{get_build_number()}",
# 指定項(xiàng)目的scheme
scheme:"#{project_Dev_scheme}",
# 是否清空以前的編譯信息 true:是
clean:true,
# 指定打包方式,Release 或者 Debug
configuration:"Release",
# 指定打包方式,目前支持app-store, package, ad-hoc, enterprise, development
# 注:由于使用手動(dòng)配置證書(shū),在export_options指定打包方式
export_method:"#{ipa_exportMethod_adhoc}",
# 指定輸出文件夾
output_directory:"#{ipa_outputDirectory_Debug}",
# Xcode9將不會(huì)允許你訪問(wèn)鑰匙串里的內(nèi)容,除非設(shè)置allowProvisioningUpdates
export_xcargs:"-allowProvisioningUpdates",
# 隱藏沒(méi)有必要的信息
silent:true,
# 手動(dòng)配置證書(shū),注意打包方式需在export_options內(nèi)使用method設(shè)置,不可使用export_method
export_options: {
method:"#{ipa_exportMethod_adhoc}",
provisioningProfiles: {
"#{project_identifier_Dev}":"#{project_provisioningProfiles_dev}",
"#{project_identifier_Dis}":"#{project_provisioningProfiles_dis}"
},
}
)
puts "*************| 開(kāi)始上傳蒲公英... |*************"
# 開(kāi)始上傳蒲公英
# pgyer(api_key: "#{pgyer_apiKey}", password: "123456", install_type: "2")
pgyer(api_key: "#{pgyer_apiKey}", password: "123456", install_type: "2", update_description: "更新內(nèi)容")
puts "*************| 上傳蒲公英成功?? |*************"
end
desc "Description of what the lane does"
lane :ad_dis do
puts "*************| 開(kāi)始打包.ipa文件... |*************"
# 更新項(xiàng)目build號(hào)
updateProjectBuildNumber
#指定Xcode路徑,裝多個(gè)版本xcode時(shí)需指定
#xcode_select("/Applications/Xcode.app")
#build_app(export_method: "ad-hoc")
# 開(kāi)始打包
gym(
# 指定輸出的ipa名稱
output_name:"#{project_Dis_scheme}_#{get_build_number()}",
# 指定項(xiàng)目的scheme
scheme:"#{project_Dis_scheme}",
# 是否清空以前的編譯信息 true:是
clean:true,
# 指定打包方式,Release 或者 Debug
configuration:"Release",
# 指定打包方式,目前支持app-store, package, ad-hoc, enterprise, development
# 注:由于使用手動(dòng)配置證書(shū),在export_options指定打包方式
export_method:"#{ipa_exportMethod_adhoc}",
# 指定輸出文件夾
output_directory:"#{ipa_outputDirectory_Debug}",
# Xcode9將不會(huì)允許你訪問(wèn)鑰匙串里的內(nèi)容,除非設(shè)置allowProvisioningUpdates
export_xcargs:"-allowProvisioningUpdates",
# 隱藏沒(méi)有必要的信息
silent:true,
# 手動(dòng)配置證書(shū),注意打包方式需在export_options內(nèi)使用method設(shè)置,不可使用export_method
export_options: {
method:"#{ipa_exportMethod_adhoc}",
provisioningProfiles: {
"#{project_identifier_Dev}":"#{project_provisioningProfiles_dev}",
"#{project_identifier_Dis}":"#{project_provisioningProfiles_dis}"
},
}
)
puts "*************| 開(kāi)始上傳蒲公英... |*************"
# 開(kāi)始上傳蒲公英
# pgyer(api_key: "#{pgyer_apiKey}", password: "123456", install_type: "2")
# pgyer(api_key: "#{pgyer_apiKey}", password: "123456", install_type: "2", update_description: "哈哈哈哈哈哈哈")
puts "*************| 上傳蒲公英成功?? |*************"
end
# ----------------------- 上傳AppStore -----------------------
lane :aps do
puts "*************| 開(kāi)始打包.ipa文件... |*************"
# 更新項(xiàng)目build號(hào)
updateProjectBuildNumber
gym(
# 指定輸出的ipa名稱
output_name:"#{project_App_scheme}_#{get_build_number()}",
# 指定項(xiàng)目的scheme
scheme:"#{project_App_scheme}",
# 是否清空以前的編譯信息 true:是
clean:true,
# 指定打包方式,Release 或者 Debug
configuration:"Release",
# 指定打包方式,目前支持app-store, package, ad-hoc, enterprise, development
# 注:由于使用手動(dòng)配置證書(shū),在export_options指定打包方式
#export_method:"#{app-store}",
# 指定輸出文件夾
output_directory:"#{ipa_outputDirectory_AppStore}",
# Xcode9將不會(huì)允許你訪問(wèn)鑰匙串里的內(nèi)容,除非設(shè)置allowProvisioningUpdates
export_xcargs:"-allowProvisioningUpdates",
# 隱藏沒(méi)有必要的信息
silent:true,
# 手動(dòng)配置證書(shū),注意打包方式需在export_options內(nèi)使用method設(shè)置,不可使用export_method
export_options: {
method:"app-store",
provisioningProfiles: {
"#{project_identifier_Dev}":"#{project_provisioningProfiles_dev}",
"#{project_identifier_Dis}":"#{project_provisioningProfiles_appstore}"
},
}
)
puts "*************| 上傳AppStore成功?? |*************"
#發(fā)布testflight測(cè)試
# pilot
end
end
三、打包發(fā)布
-
需要打包時(shí),進(jìn)入項(xiàng)目目錄,
需要打測(cè)試環(huán)境時(shí):執(zhí)行命令 fastlane ad_dev
需要打正式環(huán)境時(shí):執(zhí)行命令 fastlane ad_dis
開(kāi)始打包.png
上傳蒲公英成功.png

打包成功后ipa自動(dòng)保存.png
配置過(guò)程中出現(xiàn)的一些問(wèn)題
- 打包或?qū)С鍪?wèn)題,其實(shí)在配置過(guò)程中大部分問(wèn)題都是證書(shū)引起的,特別是配置多環(huán)境多個(gè)Bundle Identifier 多Target或多Scheme。
-
export_method 與 method 方式不一致image.png
手動(dòng)配置證書(shū)時(shí),需在export_options內(nèi)使用method設(shè)置,不可使用export_method
/// 手動(dòng)配置證書(shū),注意打包方式需在export_options內(nèi)使用method設(shè)置,不可使用export_method
export_options: {
method:"ad-hoc"
}
-
提示需要再fastfile文件中指定xcode版本
image.png
根據(jù)錯(cuò)誤提示再文件中進(jìn)行設(shè)置即可,例如:
#指定Xcode路徑,裝多個(gè)版本xcode時(shí)指定xcode版本
xcode_select("/Applications/Xcode14.2.app")
- Archive 及 Export成功,上傳報(bào)錯(cuò)
沒(méi)有添加蒲公英命令:在項(xiàng)目更目錄下,執(zhí)行:image.pngfastlane add_plugin pgyer







