Android Automatic Packaging process detail diagram:
In some of the previous blogs we know how to package through the command line, how to package through Eclipse, how to write shell scripts to package, but these are not very good cross-platform packaging.
Because Python itself has a good cross-platform feature, this blog uses Python to write cross-platform packaging scripts, the script is as follows (build.py):
#!/bin/python#Build Android Application package (. apk) from the command line using the SDK toolsImportOSImportReImportSYSImport TimeImportShutilImportHashlibbuild_root= Os.path.dirname (Os.path.abspath ('__file__')) Build_source_name='Source'Build_apk_name='fullscreenactivity-release.apk'Release_apk_path= Build_root +'/release_apks'Publish_apks= Build_root +'/publish_apks'ANDROID_SDK= Os.environ.get ('ANDROID_SDK') Build_tool_ant_path= Android_sdk +'/tools/ant'Build_bin_path='bin'Build_bin_classes_path='Classes'Source_dir= Build_root +'/'+build_source_name Rev_cmd="SVN info"Rev_pattern= R"Last Changed Rev: ([0-9]*)"#Delete file FolderdefDelete_file_folder (SRC):ifos.path.isfile (SRC):Try: Os.remove (SRC)except: Pass elifOs.path.isdir (SRC): forIteminchOs.listdir (SRC): itemsrc=Os.path.join (Src,item) delete_file_folder (ITEMSRC)Try: Os.rmdir (SRC)except: Pass#Calc File MD5defcalcMD5 (filepath): With open (filepath,'RB') as F:md5obj=hashlib.md5 () md5obj.update (F.read ()) hash=md5obj.hexdigest ()returnHash#get source code from SVN RepositorydefGet_source_code ():if(Os.path.exists (Release_apk_path)):PrintRelease_apk_path +'exists!' Else: Os.makedirs (Release_apk_path)if(Os.path.exists (Source_dir)): Os.chdir (Source_dir) Os.system ('SVN update') Else: Os.makedirs (Source_dir) os.chdir (source_dir) Os.system ('svn checkout https://github.com/clarck/AutoBuildProject/trunk.')#Clear Classes File FolderdefClear_classes_folder (): Classes_abs_path= Source_dir +'/'+Build_bin_classes_pathif(Os.path.exists (Classes_abs_path)): Delete_file_folder (Classes_abs_path)Else: Os.makedirs (Classes_abs_path)#Get SVN revisiondefGet_revision (): Rev=Os.popen (rev_cmd). Read () m=Re.search (Rev_pattern, rev.)ifm = =None:returnNonereturnM.group (1)#Get Current timedefget_time ():returnTime.strftime ('%y-%m-%d_%h-%m-%s', Time.localtime (Time.time ()))#Ant Builddefant_build (): Os.system ('android update project-p.--target android-19') Os.system ('ant clean & ant Release')#copy apk file to target folderdefcopy_apk (): Apk_file= Source_dir +'/'+ Build_bin_path +'/'+build_apk_name Target_apk_file= Release_apk_path +'/'+Build_apk_nameif(Os.path.exists (apk_file)): Shutil.copyfile (Apk_file, Target_apk_file)Else: PrintApk_file +'is not exists!'#Delete source code filesdefdelete_source_file (): Delete_file_folder (Source_dir)#Publish apkdefPublish (date, rev):if(Os.path.exists (PUBLISH_APKS)):PrintPublish_apks +'exists!' Else: Os.makedirs (PUBLISH_APKS) MD5= CalcMD5 (Release_apk_path +'/'+build_apk_name) Apk_file= Release_apk_path +'/'+build_apk_name Publish_file= Publish_apks +'/'+ Date +'_'+ Rev +'_'+'release.apk'Shutil.move (Apk_file, Publish_file) Delete_file_folder (Release_apk_path)#Main functiondefMain (): Get_source_code () Clear_classes_folder () Rev=get_revision () Date=get_time () ant_build () copy_apk () Publish (date, rev)if __name__=='__main__': Main ()
Last execution: Python build.py.
Original Android packaged cross-platform packaging