一、安装
需要MacOS 环境
- xcode 命令行工具
xcode-select --install
- 安装fastlane
# 使用ruby gems安装 # Using RubyGems sudo gem install fastlane -NV # 或者使用homebrew安装 # Alternatively using Homebrew brew cask install fastlane
- 设置环境变量
fastlane
需要设置一些环境变量才能正确运行。特别是,如果未将区域设置设置为UTF-8
语言环境,则会导致构建和上载构建时出现问题。在您的shell
配置文件中添加以下行:
~/.bashrc,~/.bash_profile,~/.profile或者~/.zshrc
export LC_ALL=en_US.UTF-8 export LANG=en_US.UTF-8
二、使用
iOS
-
初始化
在项目根目录执行fastlane init
如果是
swift
项目,需要特别指定(swift
设置仍处于测试阶段,具体使用方式可能有所不同,需要注意)fastlane init swift
-
添加自动打包命令
打开fastlane/Fastfile
文件,这个文件是自动打包的配置文件
我们在这里添加或修改打包指令
fastlane
预设了很多可用操作,可以根据自己的需要组合成为一个指令#一个例子beta lane :beta do #设置证书 sync_code_signing(type: "appstore") #打包 build_app(scheme: "MyApp") #上传到testflight测试 upload_to_testflight #slack上发送消息 slack(message: "Successfully distributed a new beta build") end
在项目根目录,命令行输入
fastlane beta
就可以执行上面的命令了
三、实际使用
-
场景
一个项目,根据不同的环境,打包成APP
实际项目中,要构建多个APP,使用Xcode打包时,要反复确认打包环境,即使这样,也不能保证打包的正确
实际上,每天要打8个包,区分不同环境
服务器环境(sit,uat),平台环境(phone,pad),包环境(A,B)
也就是要打8个包,对应不同环境 - - !!! -
编写功能
-
服务器环境
sit
和uat
环境是根据包名bundle id
区分的,可以通过修改设置plist.info
文件达到切换环境的目的# update app identifier string update_info_plist( #plist文件的路径,相对于项目根目录 plist_path: "./Platform_Mobile/Info.plist", #修改bundle id, #{$env}代表服务器环境 app_identifier: "com.package.name#{$env}", #APP名称,#{$buildPlatform.capitalize}代表平台环境 display_name: "APPNAME_I#{$buildPlatform.capitalize}_#{$env.upcase}" #可以使用ruby语法,capitalize首字母大写,upcase全部大写 )
-
平台环境
查阅相关可用操作,发现并没有直接修改build setting的方法
但是Xcode命令行是可以修改的,修改有多种方式,这里只介绍一种# build setting key-values # APP Icon资源图 # ASSETCATALOG_COMPILER_APPICON_NAME=AppIcon # 首屏资源图 # ASSETCATALOG_COMPILER_LAUNCHIMAGE_NAME=LaunchImage # 运行设备 iPhone为"1",iPad为"2" # TARGETED_DEVICE_FAMILY=1 # ASSETCATALOG_COMPILER_APPICON_NAME="" ASSETCATALOG_COMPILER_LAUNCHIMAGE_NAME="" output_name = ($kAppId=="A" ? "A" : "B") + "_" + $env.upcase + "_I" + $buildPlatform.upcase + "_V" + get_build_number + ".ipa" # app icon(应用图标)文件, uat环境设置为空,安装后用以区分 appIcon = $env == "sit" ? "AppIcon" : "" # launch image,首屏图片,pad没有对应资源,取消设置了 launchImage = $env == "sit" && $buildPlatform == "phone" ? "LaunchImage" : "" deviceFamily = $buildPlatform == "phone" ? "1" : "2" xcargs = $buildPlatform = %Q{TARGETED_DEVICE_FAMILY=#{deviceFamily} ASSETCATALOG_COMPILER_APPICON_NAME=#{appIcon} ASSETCATALOG_COMPILER_LAUNCHIMAGE_NAME=#{launchImage}} build_ios_app( workspace: "Platform_Mobile.xcworkspace", # error : You can only pass either a 'workspace' or a 'project', not both # 不能同时制定 workspace和 project # project: "Platform_Mobile.xcodeproj", scheme: "Platform_Mobile", # configuration: "Release",#默认是Release,可以根据需要设置为Debug export_method: "enterprise", # app-store,ad-hoc,package,enterprise,development,developer-id # silent: true, #静默 # clean: true, #打包前先clean # Destination directory. Defaults to current directory.输出目录,默认当前目录 output_directory: "./build/", # specify the name of the .ipa file to generate (including file extension) # 输出文件名 output_name: output_name, # use SDK as the name or path of the base SDK when building the project. # sdk: "iOS 11.1" # 导出时的选项 export_xcargs: "-allowProvisioningUpdates", # 不显示Xcode build时的相关信息,输出非常多,需要错误定位的时候可以设置为false suppress_xcode_output: true, # 打包时的设置项 xcargs: xcargs, export_options: { allowProvisioningUpdates: true, # 手动指定了打包的证书文件 provisioningProfiles: { "com.package.name" => "provisioning name", "com.package.name2" => "provisioning name", } } )
打包时,xcargs参数可以设置build setting的值,例如
在build setting选中.pngTARGETED_DEVICE_FAMILY
怎么知道要修改的值的key呢?
在build setting中选中要修改的值,例如Other Linker Flags
,粘贴到文本
//:configuration = Debug OTHER_LDFLAGS = $(inherited) -lc++ -Objc -ObjC -dead_strip -lxml2 //:configuration = Release OTHER_LDFLAGS = $(inherited) -lc++ -Objc -ObjC -dead_strip -lxml2 //:completeSettings = some OTHER_LDFLAGS
OTHER_LDFLAGS
就是Key了,上面是xcconfig
文件的格式,打包时已经指定过Debug/Release
环境了 -
包环境
包环境是根据info.plist
文件中的一个自定义key的值来区分的
所以直接修改info.plist
文件就可以了set_info_plist_value( path: "./Platform_Mobile/Info.plist", key: "kAppId", value: $kAppId )
-
附录
# This file contains the fastlane.tools configuration
# You can find the documentation at https://docs.fastlane.tools
#
# For a list of all available actions, check out
#
# https://docs.fastlane.tools/actions
#
# For a list of all available plugins, check out
#
# https://docs.fastlane.tools/plugins/available-plugins
#
# Uncomment the line if you want fastlane to automatically update itself
# update_fastlane
default_platform(:ios)
$env="sit" # uat sit pro
$kAppId="A" # A B
$buildPlatform="phone" # phone pad
platform :ios do
desc "all lane actions"
lane :all do
# increment_build_number
# increment_version_number
increment_version_number(
version_number: "0.0.9" # Set a specific version number
)
increment_build_number(
build_number: get_version_number().gsub(/\./,"").gsub(/^0+/,"") # set a specific number
)
build(env:"sit",kAppId:"A",buildPlatform:"phone")
build(env:"sit",kAppId:"A",buildPlatform:"pad")
#install
end
desc "set environment"
lane :setConfig do |options|
$env ||= options[:env]
$kAppId ||= options[:kAppId]
$buildPlatform ||= options[:buildPlatform]
getConfig
end
desc "get environment"
lane :getConfig do
puts "environment : #{$env.upcase}"
puts "kAppId : #{$kAppId}" + ($kAppId=="A" ? "A" : "B")
puts "buildPlatform : I#{$buildPlatform.capitalize}"
end
desc "build app"
lane :build do |options|
if options
setConfig(options)
end
# add actions here: https://docs.fastlane.tools/actions
# this is UAT environment
# set package A B
set_info_plist_value(
path: "./Platform_Mobile/Info.plist",
key: "kAppId",
value: $kAppId
)
# update for info.plist
update_info_plist( # update app identifier string
plist_path: "./Platform_Mobile/Info.plist",
app_identifier: "com.package.name#{$env}",
display_name: "APPNAMEI#{$buildPlatform.capitalize}_#{$env.upcase}"
)
# build setting key-values
# APP Icon资源图
# ASSETCATALOG_COMPILER_APPICON_NAME=AppIcon
# 首屏资源图
# ASSETCATALOG_COMPILER_LAUNCHIMAGE_NAME=LaunchImage
# 运行设备 iPhone为"1",iPad为"2"
# TARGETED_DEVICE_FAMILY=1
# ASSETCATALOG_COMPILER_APPICON_NAME="" ASSETCATALOG_COMPILER_LAUNCHIMAGE_NAME=""
output_name = ($kAppId=="A" ? "A" : "B") + "_" + $env.upcase + "_I" + $buildPlatform.upcase + "_V" + get_build_number + ".ipa"
appIcon = $env == "sit" ? "AppIcon" : ""
launchImage = $env == "sit" && $buildPlatform == "phone" ? "LaunchImage" : ""
deviceFamily = $buildPlatform == "phone" ? "1" : "2"
xcargs = $buildPlatform = %Q{TARGETED_DEVICE_FAMILY=#{deviceFamily} ASSETCATALOG_COMPILER_APPICON_NAME=#{appIcon} ASSETCATALOG_COMPILER_LAUNCHIMAGE_NAME=#{launchImage}}
build_ios_app(
workspace: "Platform_Mobile.xcworkspace",
# error : You can only pass either a 'workspace' or a 'project', not both
# project: "Platform_Mobile.xcodeproj",
scheme: "Platform_Mobile",
# configuration: "Release",
export_method: "enterprise", # app-store,ad-hoc,package,enterprise,development,developer-id
# silent: true,
# clean: true,
output_directory: "./build/", # Destination directory. Defaults to current directory.
output_name: output_name, # specify the name of the .ipa file to generate (including file extension)
# sdk: "iOS 11.1" # use SDK as the name or path of the base SDK when building the project.
export_xcargs: "-allowProvisioningUpdates",
suppress_xcode_output: true,
xcargs: xcargs,
export_options: {
allowProvisioningUpdates: true,
provisioningProfiles: {
"com.package.name1" => "provisioning name",
"com.package.name1" => "provisioning name",
}
}
)
end
desc "install app to online device"
lane :install do
install_on_device(
ipa: "./build/ipa_name.ipa"
)
end
end
参考
网友评论