XcodeBuild iOS app 自动化构建打包

cuicui · 发布于 2016年3月07日 · 412 次阅读
54738

[b]签名:[/b] iOS app 签名方式比 android 复杂,需要做好3个配置前提: 1,证书: *.p12 文件,双击安装; 2,provisionfile:记录设备的UUID的文件,双击安装; 3,钥匙链解锁:远程 ssh 连接签名,需要先解锁方可签名: [code]$ security unlock-keychain -p 密码 /Users/haier/Library/Keychains/login.keychain[/code]

[b]编译:[/b] 是否基于 Cocoapods 对应的编译参数略有差异: 1,基于 CocoaPods,会把当前的工程包含在一个 “.xcworkspace” 中,包含两个(或以上)工程:一个是我们自己的工程 “.xcodeproj”,另外一个,就是名为 “Pods**” 的工程,需要使用 -workspace 参数: [code]$ xcodebuild -workspace xxx.xcworkspace -scheme xxx -configuration Release -sdk iphoneos ONLY_ACTIVE_ARCH=NO CODE_SIGN_IDENTITY="xxx" PROVISIONING_PROFILE="xxx" build $ xcrun -sdk iphoneos PackageApplication -v "/Users/xx/Library/Developer/Xcode/DerivedData/xxx-xxx/Build/Products/Release-iphoneos/xxx.app" -o "$WORKSPACE/xxx.ipa"[/code] 2,不基于 Cocoapods,就直接使用 -target/scheme 就可以了: [code]$ xcodebuild -target xxx -configuration Release -sdk iphoneos ONLY_ACTIVE_ARCH=NO CODE_SIGN_IDENTITY="xxx" PROVISIONING_PROFILE="xxx" build $ xcrun -sdk iphoneos PackageApplication -v "build/Release-iphoneos/xxx.app" -o "$WORKSPACE/xxx.ipa[/code]

[b]特殊参数说明:[/b] -scheme 参数:基于 Cocoapods 时必须指定 scheme。可是 scheme 是本地的,所以在编译之前,需要先生成 scheme: [code]#!/usr/bin/env ruby

require 'xcodeproj' xcproj = Xcodeproj::Project.open("Uplus.xcodeproj") xcproj.recreate_user_schemes xcproj.save[/code] CODE_SIGN_IDENTITY:是证书的名字,如:“iPhone Developer: xxx (xxx)” PROVISIONING_PROFILE:不是 provisionfile 的名字,而是其对应的 UUID,如“xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx” xrun 的 -v 参数:是 xcodebuild 执行后生成的 .app 包的路径。编译 project 的,编译工程默认会在工程根目录的 “build” 文件夹下;而编译 workspace 不会,在 Library 下(也可以使用 CONFIGURATION_BUILD_DIR 来显式指定) xrun 的 -o 参数:是输出 .ipa 包的路径,必须是绝对路径。

共收到 4 条回复
4
laofo · #1 · 2016年3月11日

感谢分享。app编译打包很多公司都在做,但很多都不很成熟,欢迎你把这块的内容都总结总结,分享给大家。

96
elian · #2 · 2016年3月11日
  1. xcodebuild 如何不带签名,而是在xcrun打包的时候在签名。
  2. 如果有的项目是只有一个scheme要编译,有的项目是要编译多个scheme又是如何做的。
96
rexuekonglong · #3 · 2016年3月18日

一次编译一个,多设置几个编译步骤就可以

54738
cuicui · #4 · 2016年6月23日

[i=s] 本帖最后由 cuicui 于 2016-6-23 10:51 编辑

补充另外几种场景: 1,pod,只编译: [code]/usr/local/bin/pod install --repo-update xcodebuild -list -workspace ${PROJECT_NAME}.xcworkspace /usr/local/bin/xctool build -workspace ${PROJECT_NAME}.xcworkspace \ -scheme ${SCHEME_NAME} \ CODE_SIGN_IDENTITY="" CODE_SIGNING_REQUIRED=NO \ || failed "xctool build"[/code]

2,非 pod,只编译: [code]/usr/local/bin/xctool build -project ${PROJECT_NAME}.xcodeproj \ -scheme ${SCHEME_NAME} \ CODE_SIGN_IDENTITY="" CODE_SIGNING_REQUIRED=NO \ || failed "xctool build"[/code]

3,pod,ipa 打包: [code]# archive echo "--------------------- BUILD ---------------------" /usr/local/bin/pod install --repo-update xcodebuild -list -workspace ${PROJECT_NAME}.xcworkspace xcodebuild archive -workspace ${PROJECT_NAME}.xcworkspace \ -scheme ${SCHEME_NAME} \ -sdk iphoneos \ -configuration ${CONFIGURATION} \ -archivePath ${bash_dir}/build/${PROJECT_NAME}.xcarchive \ CODE_SIGN_IDENTITY="${IDENTITY}" PROVISIONING_PROFILE="${UUID}" \ || failed "xcodebuild archive"

export ipa

echo "--------------------- PACKAGE ---------------------" xcodebuild -exportArchive -archivePath ${bash_dir}/build/${PROJECT_NAME}.xcarchive \ -exportPath ${bash_dir}/build/ \ -exportOptionsPlist ${bash_dir}/exportOptions.plist \ -verbose \ || failed "xcodebuild export archive"[/code]

4,非 pod,ipa 打包: [code]# archive echo "--------------------- BUILD ---------------------" xcodebuild archive -project ${PROJECT_NAME}.xcodeproj \ -scheme ${SCHEME_NAME} \ -sdk iphoneos \ -configuration ${CONFIGURATION} \ -archivePath ${bash_dir}/build/${PROJECT_NAME}.xcarchive \ CODE_SIGN_IDENTITY="${IDENTITY}" PROVISIONING_PROFILE="${UUID}" \ || failed "xcodebuild archive"

export ipa

echo "--------------------- PACKAGE ---------------------" xcodebuild -exportArchive -archivePath ${bash_dir}/build/${PROJECT_NAME}.xcarchive \ -exportPath ${bash_dir}/build/ \ -exportOptionsPlist ${bash_dir}/exportOptions.plist \ -verbose \ || failed "xcodebuild export archive"[/code]

5,pod,framework打包: [code]# build echo "--------------------- BUILD ---------------------" /usr/local/bin/pod install --repo-update xcodebuild -list -workspace ${PROJECT_NAME}.xcworkspace xcodebuild build -workspace ${PROJECT_NAME}.xcworkspace \ -scheme ${LIB_SCHEME_NAME} \ -configuration ${CONFIGURATION} \ -sdk iphonesimulator ONLY_ACTIVE_ARCH=NO \ -destination 'platform=iOS Simulator,name=iPhone 6' \ -derivedDataPath build \ || failed "xcodebuild build iphonesimulator" xcodebuild build -workspace ${PROJECT_NAME}.xcworkspace \ -scheme ${LIB_SCHEME_NAME} \ -configuration ${CONFIGURATION} \ -sdk iphoneos ONLY_ACTIVE_ARCH=NO \ -derivedDataPath build \ CODE_SIGN_IDENTITY="" CODE_SIGNING_REQUIRED=NO \ || failed "xcodebuild build iphoneos"

recreate framework

echo "-------------- RECREATE FRAMEWORK ---------------"

build results dir

simulator_dir=build/Build/Products/Release-iphonesimulator/${LIB_SCHEME_NAME}.framework device_dir=build/Build/Products/Release-iphoneos/${LIB_SCHEME_NAME}.framework

new dir for final output

framework_dir=${LIB_SCHEME_NAME}.framework if [ -d "${framework_dir}" ]; then rm -rf ${framework_dir} fi mkdir ${framework_dir}

merge binary files

/usr/bin/lipo -create ${device_dir}/${LIB_SCHEME_NAME} ${simulator_dir}/${LIB_SCHEME_NAME} \ -output ${framework_dir}/${LIB_SCHEME_NAME} \ || failed "lipo merge framework" echo "recreate framework dir: ${framework_dir}"[/code]

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册