總目錄:Mac + Apache + PHP+打包腳本 = Mac自動化打包
作為ios開發員,打包是家常便飯啦.....
所以,把復雜的流程簡單化,有助於減輕自己的工作量,還能有效的防止問題發生...最重要的,沒那么快禿頂!
打包共幾個步驟
1.編譯unity生成xcode工程
2.導入生成的文件到打包工程
3.更改打包工程配置,如版本號
4.編譯
5.簽名生成包
我們一步一步來
1.編譯unity生成Xcode工程
unity里面提供的方法進行打包,只用在腳本里調用unity的類方法就能執行unity里寫的打包類
1 #compileunity工程,導出iOS工程! 2 defexportIosProject(logfilePath): 3 print "\n--------------------------------------------------------------- compile unity project....... --------------------------------------------------------------\n" 4 command ="/Applications/Unity2017.2.3f1/Unity.app/Contents/MacOS/Unity -quit -batchmode -projectPath /Users/admin/projectName -executeMethod ProjectBuilderIOS.BuildForIosProjectIl2Cpp >>%s"%(logfilePath) 5 printcommand 6 unityCompile_statue = os.system(command) 7 unityCompile_statue>>=8 8 ifunityCompile_statue==0: 9 print "\n***************************************************************** compile unity project succeed ********************************************************\n" 10 else: 11 print "\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! compile unity project fail !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n" 12 exit(0)
2.導入生成的文件到打包工程
找到項目的路徑,更改.pbxproj文件
#################################################################################################################################################### #新加的編輯xcodeproj工程文件的代碼 pbxproj_dir = '%s/%s/project.pbxproj'%(pro_dir,fileName) pro_dir_arr = pro_dir.split('/') arr_len = len(pro_dir_arr) pro_dir_arr[arr_len-1] = 'Classes/Native/' Native_dir = '/'.join(pro_dir_arr) Native_files = file_name(Native_dir) for file in Native_files: data = '' uuid_f = ''.join(str(uuid.uuid4()).upper().split('-')[1:]) uuid_r = ''.join(str(uuid.uuid4()).upper().split('-')[1:]) if (isincludestr(pbxproj_dir, file)): continue while(isincludestr(pbxproj_dir, uuid_f)): uuid_f =''.join(str(uuid.uuid4()).upper().split('-')[1:]) while(isincludestr(pbxproj_dir, uuid_r)): uuid_r =''.join(str(uuid.uuid4()).upper().split('-')[1:]) buildfilesection_str = creat_buildfilesection(file, uuid_f, uuid_r) refsection_str = creat_refsection(file, uuid_r) PBXBuildFile_line = getstrlineinfile(pbxproj_dir, '/* Begin PBXBuildFile section */') insertline(pbxproj_dir, PBXBuildFile_line ,buildfilesection_str) PBXFileReference_line = getstrlineinfile(pbxproj_dir, '/* Begin PBXFileReference section */') insertline(pbxproj_dir, PBXFileReference_line ,refsection_str) folder_line = getstrlineinfile(pbxproj_dir, '/* Native */ = {')+2 insertline(pbxproj_dir, folder_line ,('%s /* %s */,' %(uuid_r,file))) PBXSourcesBuildPhase_line = getstrlineinfile(pbxproj_dir, '/* Begin PBXSourcesBuildPhase section */')+4 insertline(pbxproj_dir, PBXSourcesBuildPhase_line ,('%s /* %s in Sources */,' %(uuid_f,file))) ###################################################################################################################################################
3.更改打包工程配置,如版本號
獲取對應項目的info.plist文件進行更改版本號
#獲取項目的info配置文件 try: if os.path.exists(pro_dir+"/Info.plist"): plist=readPlist(pro_dir+"/Info.plist"); else: #這里需要注意工程目錄下是否有該目錄 plist=readPlist(pro_dir+"/"+target+"/Info.plist"); except InvalidPlistException,e: print "not a plist or plist invalid:",e #修改項目的info配置文件(修改了項目的游戲版本和compile版本號) try: plist['CFBundleVersion']=game_version plist['CFBundleShortVersionString']=build_version if os.path.exists(pro_dir+"/Info.plist"): writePlist(plist,pro_dir+"/Info.plist") else: writePlist(plist,pro_dir+"/"+target+"/Info.plist") except (InvalidPlistException, NotBinaryPlistException), e: print "Something bad happened:", e
4.編譯
在編譯之前需要先清理下xcode工程
def clean(dir,pro_name,logfilePath): print "\n--------------------------------------------------------------- ios begin clean....... --------------------------------------------------------------- \n" command = "cd %s; xcodebuild -target %s clean >>%s"% (dir,pro_name,logfilePath) print command os.system(command) iosBuild_status = os.system(command) iosBuild_status>>=8 if iosBuild_status==0 : print "\n***************************************************************** ios clean succeed ********************************************************\n" else: print "\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ios clean fail!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n"
編譯分為兩種:
第一種build,這種打出來的包里面會有調試信息,包體會增大,建議測試包使用,
def build(dir,pro_name,build_config,code_sign,logfilePath): print "\n--------------------------------------------------------------- ios compile begin --------------------------------------------------------------- \n" command = "cd %s;xcodebuild -target %s -sdk iphoneos -configuration %s CODE_SIGN_IDENTITY='%s' >> %s" % (dir,pro_name,build_config,code_sign,logfilePath) print command iosBuild_status = os.system(command) iosBuild_status>>=8 if iosBuild_status==0 : print "\n***************************************************************** ios compile succeed ********************************************************\n" else: print "\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ios compile fail !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n" exit(0)
第二種是Archive,這種是比較好的打包方式,沒有調試信息,發布包建議用這種方式
def buildForArchive(dir,pro_name,logfilePath): print "\n--------------------------------------------------------------- ios compile begin --------------------------------------------------------------- \n" ipa_out_put_archive = "%s/build/%s.xcarchive" % (dir,pro_name) command = "cd %s;xcodebuild archive -project %s.xcodeproj -scheme %s -archivePath %s >> %s" % (dir,pro_name,pro_name,ipa_out_put_archive,logfilePath) print command iosBuild_status = os.system(command) iosBuild_status>>=8 if iosBuild_status==0 : print "\n***************************************************************** ios compile succeed ********************************************************\n" else: print "\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ios compile fail !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n" exit(0)
5.簽名生成包
和編譯對應的,生成包的方式也有兩種,
第一種build,對build出來的app簽名生成ipa
def export(current_time,current_section,dir,build_config,pro_name,version,code_sign,code_profile,save_dir,logfilePath): #設置ipa包的包名和存儲位置 ipa_out_put_name = "%s-%s-%s-%s-%s"%(current_time,version,pro_name,current_section,build_config) ipa_out_put = os.path.join(sys.path[0],"pack/%s.ipa"%(ipa_out_put_name)) if not os.path.exists(os.path.join(sys.path[0],"pack")): os.makedirs(os.path.join(sys.path[0],"pack")) print "pack 文件夾不存在 新建一個" _appPath = "%s/build/%s" % (dir,build_config) _appName = getFileFromDir(_appPath,"app") print "\n--------------------------------------------------------------- ios pack begin --------------------------------------------------------------- \n" command = "cd %s;xcrun -sdk iphoneos PackageApplication -v %s/build/%s/%s.app -o '%s' —sign '%s' —embed %s >> %s" % (dir,dir,build_config,_appName,ipa_out_put,code_sign,code_profile,logfilePath) print command os.system(command) iosBuild_status = os.system(command) iosBuild_status>>=8 if iosBuild_status==0 : print "\n***************************************************************** ios pack succeed ********************************************************\n" else: print "\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ios pack fail !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n" exit(0) ``` 第二種Archive,對Archive出來的.xcarchive文件簽名生成ipa ``` def exportForArchive(current_time,current_section,dir,build_config,pro_name,version,save_dir,logfilePath): #設置ipa包的包名和存儲位置 ipa_out_put_name = "%s-%s-%s-%s-%s"%(current_time,version,pro_name,current_section,build_config) ipa_out_put = os.path.join(sys.path[0],"pack/%s"%(ipa_out_put_name)) if not os.path.exists(os.path.join(sys.path[0],"pack")): os.makedirs(os.path.join(sys.path[0],"pack")) print "pack 文件夾不存在 新建一個" #獲取項目的打包配置文件 pakegePlistPath = "%s/release.plist" % (dir) #開始打包 print "\n--------------------------------------------------------------- ios pack begin--------------------------------------------------------------- \n" command = "cd %s;xcodebuild -exportArchive -archivePath %s/build/%s.xcarchive -exportPath %s -exportOptionsPlist %s >> %s" % (dir,dir,pro_name,ipa_out_put,pakegePlistPath,logfilePath) print command iosBuild_status = os.system(command) iosBuild_status>>=8 if iosBuild_status==0 : print "\n***************************************************************** ios pack succeed ********************************************************\n" else: print "\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ios pack fail !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n" exit(0) #將生成的IPA重命名 copyfile(ipa_out_put+"/"+pro_name+".ipa",ipa_out_put+".ipa") deletetree(ipa_out_put);
以上最主要的幾步都已列出,按照自己的需要使用
下面貼上我使用的腳本,其中有些數據如"os.environ["BASKETBALL_PRO_DIR"]"這種,是在系統設置的環境變量,是路徑字符串
順帶貼上設置環境變量的方法
IOS : 環境變量 BASKETBALL_PRO_DIR (項目根目錄)如 : /444/111/www 配置指引:在終端 輸入命令 vim .bash_profile, vim打開 bash_profile 按鍵盤 ‘i’鍵進入編輯模式 在編輯模式下,配置項目跟目錄 export BASKETBALL_PRO_DIR=“pro_dir” 如:export BASKETBALL_PRO_DIR= /444/111/www 確認路徑正確之后,按esc 進入vim 的命令模式輸入 ":wq" 保存文件並退出vim 輸入命令 source .bash_profile 立即生效環境變量 驗證環境變量:在終端 輸入 echo $BASKETBALL_PRO_DIR 環境變量設置正確的情況下 終端將會輸出您所設置的籃球項目根目錄 在終端 輸入 echo $BASKETBALL_GAMERENDER_DIR 環境變量設置正確的情況下 終端將會輸出您所設置的gamerender 的根目錄路徑
點個贊再走唄。。。
如有疑問,聯系作者
博客園:這個我不知道誒
