iOS自動打包并發(fā)布腳本

本文轉(zhuǎn)自CaryaLiu's Blog。

本文最終實現(xiàn)的是使用腳本打 Ad-hoc 包吏垮,并發(fā)布測試,當(dāng)然稍微修改一下腳本參數(shù)就可以打其他類型的 ipa 包了。另外該腳本還實現(xiàn)了將生成的 ipa 包上傳至蒲公英進(jìn)行測試分發(fā)庭再。

xcodebuild 簡介

使用xcodebuild和xcrun打包簽名

將打包過程腳本化

xcodebuild 簡介

xcodebuild 是蘋果提供的打包項目或者工程的命令,了解該命令最好的方式就是使用 man xcodebuild 查看其 man page. 盡管是英文牺堰,一定要老老實實的讀一遍就好了拄轻。

DESCRIPTION

xcodebuild builds one or more targets contained in an Xcode project, or builds a scheme contained in an Xcode workspace or Xcode project.

Usage

To build an Xcode project, run xcodebuild from the directory containing your project (i.e. the directory containing the name.xcodeproj package). If you have multiple projects in the this directory you will need to use -project to indicate which project should be built. By default, xcodebuild builds the first target listed in the project, with the default build configuration. The order of the targets is a property of the project and is the same for all users of the project.

To build an Xcode workspace, you must pass both the -workspace and -scheme options to define the build. The parameters of the scheme will control which targets are built and how they are built, although you may pass other options to xcodebuild to override some parameters of the scheme.

There are also several options that display info about the installed version of Xcode or about projects or workspaces in the local directory, but which do not initiate an action. These include -list, -showBuildSettings, -showsdks, -usage, and -version.

總結(jié)一下:

需要在包含 name.xcodeproj 的目錄下執(zhí)行 xcodebuild 命令,且如果該目錄下有多個 projects伟葫,那么需要使用 -project 指定需要 build 的項目恨搓。

在不指定 build 的 target 的時候,默認(rèn)情況下會 build project 下的第一個 target

當(dāng) build workspace 時,需要同時指定 -workspace 和 -scheme 參數(shù)斧抱,scheme 參數(shù)控制了哪些 targets 會被 build 以及以怎樣的方式 build常拓。

有一些諸如 -list, -showBuildSettings, -showsdks 的參數(shù)可以查看項目或者工程的信息,不會對 build action 造成任何影響辉浦,放心使用弄抬。

那么,xcodebuild 究竟如何使用呢宪郊? 繼續(xù)看文檔:

NAME

xcodebuild – build Xcode projects and workspaces

SYNOPSIS

xcodebuild [-project name.xcodeproj] [[-target targetname] … | -alltargets] [-configuration configurationname] [-sdk [sdkfullpath | sdkname]] [action …] [buildsetting=value …] [-userdefault=value …]

xcodebuild [-project name.xcodeproj] -scheme schemename [[-destination destinationspecifier] …] [-destination-timeout value] [-configuration configurationname] [-sdk [sdkfullpath | sdkname]] [action …] [buildsetting=value …] [-userdefault=value …]

xcodebuild -workspace name.xcworkspace -scheme schemename [[-destination destinationspecifier] …] [-destination-timeout value] [-configuration configurationname] [-sdk [sdkfullpath | sdkname]] [action …] [buildsetting=value …] [-userdefault=value …]

xcodebuild -version [-sdk [sdkfullpath | sdkname]] [infoitem]

xcodebuild -showsdks

xcodebuild -showBuildSettings [-project name.xcodeproj | [-workspace name.xcworkspace -scheme schemename]]

xcodebuild -list [-project name.xcodeproj | -workspace name.xcworkspace]

xcodebuild -exportArchive -archivePath xcarchivepath -exportPath destinationpath -exportOptionsPlist path

xcodebuild -exportLocalizations -project name.xcodeproj -localizationPath path [[-exportLanguage language] …]

xcodebuild -importLocalizations -project name.xcodeproj -localizationPath path

挑幾個我常用的形式介紹一下掂恕,較長的使用方式以序列號代替:

xcodebuild -showsdks: 列出 Xcode 所有可用的 SDKs

xcodebuild -showBuildSettings: 上述序號6的使用方式,查看當(dāng)前工程 build setting 的配置參數(shù)弛槐,Xcode 詳細(xì)的 build setting 參數(shù)參考官方文檔 Xcode Build Setting Reference懊亡, 已有的配置參數(shù)可以在終端中以 buildsetting=value 的形式進(jìn)行覆蓋重新設(shè)置.

xcodebuild -list: 上述序號7的使用方式,查看 project 中的 targets 和 configurations乎串,或者 workspace 中 schemes, 輸出如下:

1

Information about project "NavTabBar":

Targets:

NavTabBar

NavTabBarTests

NavTabBarUITests

Build Configurations:

Debug

Release

Ad-hoc

If no build configuration is specified and -scheme is not passed then "Release" is used.

Schemes:

NavTabBar

xcodebuild [-project name.xcodeproj] [[-target targetname] ... | -alltargets] build: 上述序號1的使用方式斋配,會 build 指定 project,其中 -target 和 -configuration 參數(shù)可以使用 xcodebuild -list 獲得灌闺,-sdk 參數(shù)可由 xcodebuild -showsdks 獲得艰争,[buildsetting=value ...] 用來覆蓋工程中已有的配置」鸲裕可覆蓋的參數(shù)參考官方文檔 Xcode Build Setting Reference, action... 的可用選項如下, 打包的話當(dāng)然用 build甩卓,這也是默認(rèn)選項。

build

Build the target in the build root (SYMROOT). This is the default action, and is used if no action is given.

analyze

Build and analyze a target or scheme from the build root (SYMROOT). This requires specifying a scheme.

archive

Archive a scheme from the build root (SYMROOT). This requires specifying a scheme.

test

Test a scheme from the build root (SYMROOT). This requires specifying a scheme and optionally a destination.

installsrc

Copy the source of the project to the source root (SRCROOT).

install

Build the target and install it into the target’s installation directory in the distribution root (DSTROOT).

clean

Remove build products and intermediate files from the build root (SYMROOT).

xcodebuild -workspace name.xcworkspace -scheme schemename build: 上述序號3的使用方式蕉斜,build 指定 workspace逾柿,當(dāng)我們使用 CocoaPods 來管理第三方庫時,會生成 xcworkspace 文件宅此,這樣就會用到這種打包方式.

使用xcodebuild和xcrun打包簽名

開始之前机错,可以新建一個測試工程 TestImg 來練習(xí)打包,在使用終端命令打包之前父腕,請確認(rèn)該工程也可以直接使用 Xcode 真機(jī)調(diào)試成功弱匪。

然后,打開終端璧亮,進(jìn)入包含 TestImg.xcodeproj 的目錄下萧诫,運(yùn)行以下命令:

xcodebuild -project TestImg.xcodeproj -target TestImg -configuration Release

如果 build 成功,會看到 ** BUILD SUCCEEDED ** 字樣枝嘶,且在終端會打印出這次 build 的簽名信息帘饶,如下:

Signing Identity: “iPhone Developer: xxx(59xxxxxx)”

Provisioning Profile: “iOS Team Provisioning Profile: *"

且在該目錄下會多出一個 build 目錄,該目錄下有 Release-iphoneos 和 TestImg.build 文件群扶,根據(jù)我們 build -configuration 配置的參數(shù)不同及刻,Release-iphoneos 的文件名會不同镀裤。

在 Release-iphoneos 文件夾下,有我們需要的TestImg.app文件缴饭,但是要安裝到真機(jī)上暑劝,我們需要將該文件導(dǎo)出為ipa文件,這里使用 xcrun 命令茴扁。

xcrun -sdk iphoneos -v PackageApplication ./build/Release-iphoneos/TestImg.app -o ~/Desktop/TestImg.ipa

這里又冒出一個 PackageApplication, 我剛開始也不知道這是個什么玩意兒,萬能的google告訴我汪疮,這是 Xcode 包里自帶的工具峭火,使用 xcrun -sdk iphoneos -v PackageApplication -help 查看幫助信息.

Usage:

PackageApplication [-s signature] application [-o output_directory] [-verbose] [-plugin plugin] || -man || -help

Options:

[-s signature]: certificate name to resign application before packaging

[-o output_directory]: specify output filename

[-plugin plugin]: specify an optional plugin

-help: brief help message

-man: full documentation

-v[erbose]: provide details during operation

如果執(zhí)行成功,則會在你的桌面生成 TestImg.ipa 文件智嚷,這樣就可以發(fā)布測試了卖丸。如果你遇到以下警告信息:

Warning: –resource-rules has been deprecated in Mac OS X >= 10.10! ResourceRules.plist: cannot read resources

請參考 stackoverflow 這個回答

將打包過程腳本化

工作中,特別是所做項目進(jìn)入測試階段盏道,肯定會經(jīng)常打 Ad-hoc 包給測試人員進(jìn)行測試稍浆,但是我們肯定不想每次進(jìn)行打包的時候都要進(jìn)行一些工程的設(shè)置修改,以及一系列的 next 按鈕點(diǎn)擊操作猜嘱,現(xiàn)在就讓這些操作都交給腳本化吧衅枫。

腳本化中使用如下的命令打包:

xcodebuild -project name.xcodeproj -target targetname -configuration Release -sdk iphoneos build CODE_SIGN_IDENTITY="$(CODE_SIGN_IDENTITY)" PROVISIONING_PROFILE="$(PROVISIONING_PROFILE)"

或者

xcodebuild -workspace name.xcworkspace -scheme schemename -configuration Release -sdk iphoneos build CODE_SIGN_IDENTITY="$(CODE_SIGN_IDENTITY)" PROVISIONING_PROFILE="$(PROVISIONING_PROFILE)"

然后使用 xcrun 生成 ipa 文件:

`xcrun -sdk iphoneos -v PackageApplication ./build/Release-iphoneos/$(target|scheme).app"

清除 build 過程中產(chǎn)生的中間文件

結(jié)合蒲公英分發(fā)平臺,將 ipa 文件上傳至蒲公英分發(fā)平臺朗伶,同時在終端會打印上傳結(jié)果以及上傳應(yīng)用后該應(yīng)用的 URL弦撩。蒲公英分發(fā)平臺能夠方便地將 ipa 文件盡快分發(fā)到測試人員,該平臺有開放 API论皆,可避免人工上傳益楼。

該腳本的使用可使用 python autobuild.py -h 查看,與 xcodebuild 的使用相似:

Usage: autobuild.py [options]

Options:

-h, --help: show this help message and exit

-w name.xcworkspace, --workspace=name.xcworkspace: Build the workspace name.xcworkspace.

-p name.xcodeproj, --project=name.xcodeproj: Build the project name.xcodeproj.

-s schemename, --scheme=schemename: Build the scheme specified by schemename. Required if building a workspace.

-t targetname, --target=targetname: Build the target specified by targetname. Required if building a project.

-o output_filename, --output=output_filename: specify output filename

在腳本頂部点晴,有幾個全局變量感凤,根據(jù)自己的項目情況修改。

1

2

3

4

5

6

7

CODE_SIGN_IDENTITY = "iPhone Distribution: companyname (9xxxxxxx9A)"

PROVISIONING_PROFILE = "xxxxx-xxxx-xxx-xxxx-xxxxxxxxx"

CONFIGURATION = "Release"

SDK = "iphoneos"

USER_KEY = "15d6xxxxxxxxxxxxxxxxxx"

API_KEY = "efxxxxxxxxxxxxxxxxxxxx"

其中粒督,CODE_SIGN_IDENTITY 為開發(fā)者證書標(biāo)識陪竿,可以在 Keychain Access -> Certificates -> 選中證書右鍵彈出菜單 -> Get Info -> Common Name 獲取,類似 iPhone Distribution: Company name Co. Ltd (xxxxxxxx9A), 包括括號內(nèi)的內(nèi)容屠橄。

PROVISIONING_PROFILE: 這個是 mobileprovision 文件的 identifier萨惑,獲取方式:

Xcode -> Preferences -> 選中申請開發(fā)者證書的 Apple ID -> 選中開發(fā)者證書 -> View Details… -> 根據(jù) Provisioning Profiles 的名字選中打包所需的 mobileprovision 文件 -> 右鍵菜單 -> Show in Finder -> 找到該文件后,除了該文件后綴名的字符串就是 PROVISIONING_PROFILE 字段的內(nèi)容仇矾。

當(dāng)然也可以使用腳本獲取, 此處參考 命令行獲取mobileprovision文件的UUID:

1

2

3

4

5

6

7

8

9

10

#!/bin/bash

if [ $# -ne 1 ]

then

echo "Usage: getmobileuuid the-mobileprovision-file-path"

exit 1

fi

mobileprovision_uuid=`/usr/libexec/PlistBuddy -c "Print UUID" /dev/stdin <<< $(/usr/bin/security cms -D -i $1)`

echo "UUID is:"

echo ${mobileprovision_uuid}

USER_KEY, API_KEY: 是蒲公英開放 API 的密鑰庸蔼。

將autobuild.py腳本放在你項目的根目錄下,進(jìn)入根目錄贮匕,如下使用:

1

./autobuild.py -w yourname.xcworkspace -s schemename -o ~/Desktop/yourname.ipa

或者

1

./autobuild.py -p yourname.xcodeproj -t targetname -o ~/Desktop/yourname.ipa

該腳本可在 github 查看姐仅,如有任何問題,請留言回復(fù)。

github地址:https://github.com/carya/Util

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末掏膏,一起剝皮案震驚了整個濱河市劳翰,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌馒疹,老刑警劉巖佳簸,帶你破解...
    沈念sama閱讀 219,366評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異颖变,居然都是意外死亡生均,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,521評論 3 395
  • 文/潘曉璐 我一進(jìn)店門腥刹,熙熙樓的掌柜王于貴愁眉苦臉地迎上來马胧,“玉大人,你說我怎么就攤上這事衔峰∨寮梗” “怎么了?”我有些...
    開封第一講書人閱讀 165,689評論 0 356
  • 文/不壞的土叔 我叫張陵垫卤,是天一觀的道長威彰。 經(jīng)常有香客問我,道長穴肘,這世上最難降的妖魔是什么抱冷? 我笑而不...
    開封第一講書人閱讀 58,925評論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮梢褐,結(jié)果婚禮上旺遮,老公的妹妹穿的比我還像新娘。我一直安慰自己盈咳,他們只是感情好耿眉,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,942評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著鱼响,像睡著了一般鸣剪。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上丈积,一...
    開封第一講書人閱讀 51,727評論 1 305
  • 那天筐骇,我揣著相機(jī)與錄音,去河邊找鬼江滨。 笑死铛纬,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的唬滑。 我是一名探鬼主播告唆,決...
    沈念sama閱讀 40,447評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼棺弊,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了擒悬?” 一聲冷哼從身側(cè)響起模她,我...
    開封第一講書人閱讀 39,349評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎懂牧,沒想到半個月后侈净,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,820評論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡僧凤,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,990評論 3 337
  • 正文 我和宋清朗相戀三年畜侦,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片拼弃。...
    茶點(diǎn)故事閱讀 40,127評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡夏伊,死狀恐怖摇展,靈堂內(nèi)的尸體忽然破棺而出吻氧,到底是詐尸還是另有隱情,我是刑警寧澤咏连,帶...
    沈念sama閱讀 35,812評論 5 346
  • 正文 年R本政府宣布盯孙,位于F島的核電站,受9級特大地震影響祟滴,放射性物質(zhì)發(fā)生泄漏振惰。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,471評論 3 331
  • 文/蒙蒙 一垄懂、第九天 我趴在偏房一處隱蔽的房頂上張望骑晶。 院中可真熱鬧,春花似錦草慧、人聲如沸桶蛔。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,017評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽仔雷。三九已至,卻和暖如春舔示,著一層夾襖步出監(jiān)牢的瞬間碟婆,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,142評論 1 272
  • 我被黑心中介騙來泰國打工惕稻, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留竖共,地道東北人。 一個月前我還...
    沈念sama閱讀 48,388評論 3 373
  • 正文 我出身青樓俺祠,卻偏偏與公主長得像肘迎,于是被迫代替她去往敵國和親甥温。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,066評論 2 355

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

  • 本文最終實現(xiàn)的是使用腳本打 Ad-hoc 包妓布,并發(fā)布測試姻蚓,當(dāng)然稍微修改一下腳本參數(shù)就可以打其他類型的 ipa 包了...
    Crazy2015閱讀 323評論 0 0
  • 直接上代碼: 文件:autobuild.py #!/usr/bin/env python# -*- coding:...
    閑云清煙閱讀 724評論 0 6
  • 1、xcodebuild 簡介 在終端中輸入man xcodebuild 查看其 man page介紹 NAME ...
    暗物質(zhì)閱讀 10,574評論 7 20
  • 因為需要匣沼,最近在研究用腳本編譯.ipa文件狰挡,在網(wǎng)上查找了不少資料,發(fā)現(xiàn)都是一些比較舊的操作了释涛,在應(yīng)用的時候難免會有...
    wendy_le閱讀 1,712評論 1 2
  • (一)加叁、中西文化的差異 好,說完了中西醫(yī)的來源及他的特點(diǎn)唇撬,我們從中西文化的差異來看下有什么不同之處它匕。 ①、我們先從...
    古典悠夢閱讀 1,096評論 0 2