系列
iOS自动打包ipa(shell脚本)
iOS自动打包ipa(Python脚本)
安装Python库
$ pip install requests
Python脚本
$ cd iOS项目目录
$ vim build_using_python.py
#! /usr/bin/python
#coding=utf-8
# -*- coding:utf8 -*-
from optparse import OptionParser
import subprocess
import requests
import os
#configuration for iOS build setting
CODE_SIGN_IDENTITY = "iPhone Distribution:************.,Ltd"
PROVISIONING_PROFILE = "************"
CONFIGURATION = "Release"
SDK = "iphoneos"
WORKSPACE = "******.xcworkspace"
TARGET = "******"
SCHEME = "******"
# configuration for pgyer
PGYER_UPLOAD_URL = "http://www.pgyer.com/apiv1/app/upload"
DOWNLOAD_BASE_URL = "http://www.pgyer.com"
USER_KEY = "************"
API_KEY = "************"
def cleanBuildDir(buildDir):
cleanCmd = "rm -r %s" %(buildDir)
process = subprocess.Popen(cleanCmd, shell = True)
process.wait()
print "cleaned buildDir: %s" %(buildDir)
def parserUploadResult(jsonResult):
resultCode = jsonResult['code']
if resultCode == 0:
downUrl = DOWNLOAD_BASE_URL +"/"+jsonResult['data']['appShortcutUrl']
print "Upload Success"
print "DownUrl is:" + downUrl
showNotification("任务完成!", "Upload Success!");
else:
print "Upload Fail!"
print "Reason:"+jsonResult['message']
showNotification("Upload failure!", "Reason:"+jsonResult['message']);
def uploadIpaToPgyer(ipaPath, comments):
print "ipaPath:"+ipaPath
files = {'file': open(ipaPath, 'rb')}
headers = {'enctype':'multipart/form-data'}
payload = {'uKey':USER_KEY,'_api_key':API_KEY,'updateDescription':comments}
print "uploading...."
r = requests.post(PGYER_UPLOAD_URL, data = payload ,files=files,headers=headers)
if r.status_code == requests.codes.ok:
result = r.json()
parserUploadResult(result)
else:
print 'HTTPError,Code:'+r.status_code
showNotification("Upload failure!", 'HTTPError,Code:'+r.status_code);
def buildProject(project, target, output, comments):
buildCmd = 'xcodebuild -project %s -target %s -sdk %s -configuration %s build CODE_SIGN_IDENTITY="%s" PROVISIONING_PROFILE="%s"' %(project, target, SDK, CONFIGURATION, CODE_SIGN_IDENTITY, PROVISIONING_PROFILE)
process = subprocess.Popen(buildCmd, shell = True)
process.wait()
signApp = "./build/%s-iphoneos/%s.app" %(CONFIGURATION, target)
signCmd = "xcrun -sdk %s -v PackageApplication %s -o %s" %(SDK, signApp, output)
process = subprocess.Popen(signCmd, shell=True)
(stdoutdata, stderrdata) = process.communicate()
uploadIpaToPgyer(output, comments)
cleanBuildDir("./build")
def buildWorkspace(workspace, scheme, output, comments):
cleanWorkspace()
process = subprocess.Popen("pwd", stdout=subprocess.PIPE)
(stdoutdata, stderrdata) = process.communicate()
buildDir = stdoutdata.strip() + '/build'
cleanBuildDir(buildDir)
showNotification("Clean Success!", "Clean Success! Then Build.")
print "buildDir: " + buildDir
buildCmd = 'xcodebuild -workspace %s -scheme %s -sdk %s -configuration %s build CODE_SIGN_IDENTITY="%s" PROVISIONING_PROFILE="%s" SYMROOT=%s' %(workspace, scheme, SDK, CONFIGURATION, CODE_SIGN_IDENTITY, PROVISIONING_PROFILE, buildDir)
process = subprocess.Popen(buildCmd, shell = True)
process.wait()
showNotification("Build Success!", "Build Success! Then Sign.");
signApp = "./build/%s-iphoneos/%s.app" %(CONFIGURATION, scheme)
signCmd = "xcrun -sdk %s -v PackageApplication %s -o %s" %(SDK, signApp, output)
process = subprocess.Popen(signCmd, shell=True)
(stdoutdata, stderrdata) = process.communicate()
showNotification("Sign Success!", "Sign Success! Then upload to pgyer.");
uploadIpaToPgyer(output, comments)
cleanBuildDir(buildDir)
def showNotification(title, subtitle):
os.system("osascript -e 'display notification \"" + subtitle + "\" with title \"" + title + "\"'")
def cleanWorkspace():
buildCmd = 'xcodebuild clean -workspace ' + WORKSPACE + ' -scheme ' + SCHEME + ' -configuration ' + CONFIGURATION
print "clean workspace: " + buildCmd
process = subprocess.Popen(buildCmd, shell = True)
process.wait()
def xcbuild(options):
# project = options.project
# workspace = options.workspace
# target = options.target
# scheme = options.scheme
# output = options.output
# ipa输出到桌面
output = os.path.expanduser("~") + '/Desktop/' + TARGET + '.ipa'
comments = options.comments
if comments is None:
print "请输入更新日志"
# elif project is None and workspace is None:
# pass
# elif project is not None:
# buildProject(project, target, output, comments)
else:
buildWorkspace(WORKSPACE, SCHEME, output, comments)
def main():
parser = OptionParser()
# parser.add_option("-w", "--workspace", help="Build the workspace name.xcworkspace.", metavar="name.xcworkspace")
# parser.add_option("-p", "--project", help="Build the project name.xcodeproj.", metavar="name.xcodeproj")
# parser.add_option("-s", "--scheme", help="Build the scheme specified by schemename. Required if building a workspace.", metavar="schemename")
# parser.add_option("-t", "--target", help="Build the target specified by targetname. Required if building a project.", metavar="targetname")
# parser.add_option("-o", "--output", help="specify output filename", metavar="output_filename")
parser.add_option("-m", "--comments", help="specify the upgrade comments", metavar="upgrade_comments")
(options, args) = parser.parse_args()
print "options: %s, args: %s" % (options, args)
xcbuild(options)
if __name__ == '__main__':
main()
参数说明
其中,CODE_SIGN_IDENTITY
为开发者证书标识,可以在 Keychain Access
-> Certificates
-> 选中证书右键弹出菜单 -> Get Info
-> Common Name
获取,类似 iPhone Distribution: Company name Co. Ltd (xxxxxxxx9A)
, 包括括号内的内容。
PROVISIONING_PROFILE
: 这个是 mobileprovision
文件的 identifier
,获取方式:
Xcode
-> Preferences
-> 选中申请开发者证书的 Apple ID -> 选中开发者证书 -> View Details…
-> 根据 Provisioning Profiles 的名字选中打包所需的 mobileprovision 文件 -> 右键菜单 -> Show in Finder
-> 找到该文件后,除了该文件后缀名的字符串就是 PROVISIONING_PROFILE
字段的内容。
打包
$ python build_using_python.py -m "更新日志"
注意:Xcode 8 不再用
PROVISIONING_PROFILE
,而是用PROVISIONING_PROFILE_SPECIFIER
,它可以是teamID/Provisioning file名称
,也可以是Provisioning file ID
网友评论
python build_using_python.py -m "更新日志"
options: {'comments': '\xe6\x9b\xb4\xe6\x96\xb0\xe6\x97\xa5\xe5\xbf\x97'}, args: []
clean workspace: xcodebuild clean -workspace DianJingNet.xcworkspace -scheme ****** -configuration Release
xcodebuild: error: Unknown build action 'AlipaySDK.framework'.
如果不是状态的原因造成的,可以尝试着删除之前的`Provisioning Profile`,重新生成一个新的,然后下载并安装到你的开发机器上。
xcrun: note: PATH = '/Library/Frameworks/Python.framework/Versions/3.5/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin'
xcrun: note: SDKROOT = '/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS9.3.sdk'
xcrun: note: TOOLCHAINS = ''
xcrun: note: DEVELOPER_DIR = '/Applications/Xcode.app/Contents/Developer'
xcrun: note: XCODE_DEVELOPER_USR_PATH = ''
xcrun: note: xcrun_db = '/var/folders/1p/mnrmtw_j49z95k1m3f2jhz780000gn/T/xcrun_db'
xcrun: note: xcrun via PackageApplication (xcrun)
xcrun: note: database key is: PackageApplication|/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS9.3.sdk||/Applications/Xcode.app/Contents/Developer|
xcrun: note: lookup resolved in '/var/folders/1p/mnrmtw_j49z95k1m3f2jhz780000gn/T/xcrun_db' : '/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/PackageApplication'
cleaned buildDir: /Users/caijianhai/Documents/ProjectHX/TrainingClient/build
CODE_SIGN_IDENTITY = 发布证书
PROVISIONING_PROFILE = adhoc配置文件
CONFIGURATION = "Release"
SDK = "iphoneos"
谢谢
1 参数检查好几遍是没问题的。测试了下打包debug包是可以正常安装的。
2 控制台没有异常信息。
3 发布到蒲公英,提示内测版本,手机上快要下载完成时,提示重新下载。不能安装
log信息:
CodeSign build/Release-iphoneos/TrainingClient.app
cd /Users/caijianhai/Documents/ProjectHX/TrainingClient
export CODESIGN_ALLOCATE=/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/codesign_allocate
export PATH="/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin:/Applications/Xcode.app/Contents/Developer/usr/bin:/Library/Frameworks/Python.framework/Versions/3.5/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin"
Signing Identity: "iPhone Distribution: Hexun Information Technology Co.,Ltd. (X4P5KW4G5C)"
Provisioning Profile: "HXCai Dao Push Adhoc"
(ff6842b7-a81e-49e7-8b99-4c345956eb13)
/usr/bin/codesign --force --sign 8EE00516FF10D0CB28C7D9F2CA95D4C5203F06B0 --entitlements /Users/caijianhai/Library/Developer/Xcode/DerivedData/TrainingClient-cbzgawldcpiiyhgtjgxauyvxpfox/Build/Intermediates/TrainingClient.build/Release-iphoneos/TrainingClient.build/TrainingClient.app.xcent --timestamp=none /Users/caijianhai/Documents/ProjectHX/TrainingClient/build/Release-iphoneos/TrainingClient.app
Validate build/Release-iphoneos/TrainingClient.app
cd /Users/caijianhai/Documents/ProjectHX/TrainingClient
export PATH="/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin:/Applications/Xcode.app/Contents/Developer/usr/bin:/Library/Frameworks/Python.framework/Versions/3.5/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin"
export PRODUCT_TYPE=com.apple.product-type.application
builtin-validationUtility /Users/caijianhai/Documents/ProjectHX/TrainingClient/build/Release-iphoneos/TrainingClient.app -validate-for-store
** BUILD SUCCEEDED **
xcrun: note: PATH = '/Library/Frameworks/Python.framework/Versions/3.5/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin'
xcrun: note: SDKROOT = '/Appli
1)先确认`CODE_SIGN_IDENTITY `和`PROVISIONING_PROFILE `参数的赋值是否正确,可以参考本文`参数说明`章节。
2)看看打包过程中控制台的输出,是否有异常信息。
3)你是如何安装的?发布到蒲公英,然后在手机上下载的吗?