码迷,mamicode.com
首页 > 移动开发 > 详细

[原]Android打包之跨平台打包

时间:2015-01-22 17:52:03      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:

Android自动打包流程详细图:

技术分享

在前面一些博客我们知道了如何通过命令行打包,如何通过Eclipse打包,如何通过编写shell脚本来进行打包,但是这些都不能很好的跨平台进行打包。

因Python本身具有很好的跨平台特性,故本博就是通过使用Python来进行编写跨平台打包脚本,脚本如下(build.py):

#!/bin/python
# build Android application package (.apk) from the command line using the SDK tools

import os
import re
import sys
import time
import shutil
import hashlib

build_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 folder
def delete_file_folder(src):
    if os.path.isfile(src):
        try:
            os.remove(src)
        except:
            pass
    elif os.path.isdir(src):
        for item in os.listdir(src):
            itemsrc = os.path.join(src,item)
            delete_file_folder(itemsrc) 
        try:
            os.rmdir(src)
        except:
            pass

# calc file md5
def calcMD5(filepath):
    with open(filepath,rb) as f:
        md5obj = hashlib.md5()
        md5obj.update(f.read())
        hash = md5obj.hexdigest()
        return hash

# get source code from svn Repository
def get_source_code():
    if (os.path.exists(release_apk_path)):
        print release_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 folder
def clear_classes_folder():
    classes_abs_path = source_dir + / + build_bin_classes_path
    if (os.path.exists(classes_abs_path)):
        delete_file_folder(classes_abs_path)
    else:
        os.makedirs(classes_abs_path) 

# get svn revision
def get_revision():
    rev = os.popen(rev_cmd).read()
    m = re.search(rev_pattern, rev)
    if m == None:
        return None
    return m.group(1)

# get current time
def get_time():
    return time.strftime(%Y-%m-%d_%H-%M-%S, time.localtime(time.time()))

# ant build
def ant_build():
    os.system(android update project -p . --target android-19)
    os.system(ant clean & ant release)

# copy apk file to target folder
def copy_apk():
    apk_file = source_dir + / + build_bin_path + / + build_apk_name
    target_apk_file = release_apk_path + / + build_apk_name
    if (os.path.exists(apk_file)):
        shutil.copyfile(apk_file, target_apk_file)
    else:
        print apk_file +  is not exists!

# delete source code files
def delete_source_file():
    delete_file_folder(source_dir)

# publish apk 
def publish(date, rev):
    if (os.path.exists(publish_apks)):
        print publish_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 function 
def main():
    get_source_code()
    clear_classes_folder()
    rev = get_revision()
    date = get_time()
    ant_build()
    copy_apk()
    publish(date, rev)    

if __name__ == __main__:
    main()

 

最后执行:python build.py即可。

[原]Android打包之跨平台打包

标签:

原文地址:http://www.cnblogs.com/tanlon/p/4241887.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!