Snake's Home

python实现自动化构件和持续集成Android

公司的打包效率太低下了, 没有持续构建和集成。
每次要等开发打包。碰到那个开发请假,其他的开发又不会,要等。
而且没有版本管理。想找一个以前的版本,都没有。
开始想用gradle打包,当时还是1.0.0,坑太多。
用maven,有个依赖总是不成功。
最后还是用原始的Ant打包,实现了自动化构建。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
from os import walk as oswalk  
from os import chdir as oschdir
from os import popen as ospopen
from os.path import exists as pathexists
from os.path import join as pathjoin
from shutil import copyfile as shcopy
from shutil import rmtree as shrmtree
import shutil
import sys
import time
import os

#consts
LIBRARY = "library"
TRUNK = "trunk"
CIRCLE = "Circle"
PROJECT_PROPERTY = "project.properties"
BIN = "bin"
OBJ = "obj"
APK = "Circle-debug.apk"
remote_path=r"\\10.21.101.100\XXX\build\android"
local_path=r"D:\dailyAPK"

class AndroidBuilder:
__base_path = ""
__output_path = ""
__use_ndk = False

def __init__(self, base_path, output_path, use_ndk):
self.__base_path = base_path
self.__output_path = output_path
self.__use_ndk = use_ndk

def __execmd(self, cmd):
ret = ospopen(cmd).readlines()
for line in ret:
print(line)

def __re_native(self):
print("re-build shared objects")
if (pathexists(OBJ)):
shrmtree(OBJ)
self.__execmd("ndk-build clean")
self.__execmd("ndk-build")


def __move_qrcode(self):
zxing_path = pathjoin(self.__base_path, TRUNK, CIRCLE, "qrcode", "com", "XXX", "zxing")
dest_path = pathjoin(self.__base_path, TRUNK, CIRCLE, "src", "com", "XXX")
print "aaa",zxing_path
print "bbb",dest_path
shutil.copy(zxing_path, dest_path)
#shutil.copy2(zxing_path, dest_path)
#shutil.copytree(zxing_path, dest_path,True)
print "wo cao"
if (pathexists(dest_path)):
shrmtree(dest_path)
print "ccc",zxing_path
print "ddd",dest_path
shcopy(zxing_path, dest_path)


def __re_ant(self):
idx = 0
project_properties = "target=android-19\n\n"
for dirpath, dirname, filename in oswalk(pathjoin(self.__base_path, LIBRARY)):
print "------------------",dirname
for dir in dirname:
print("re-ant project %s" % (dir))
oschdir(pathjoin(self.__base_path, LIBRARY, dir))
idx += 1
project_properties += "android.library.reference.%d=../../%s/%s\n" % (idx, LIBRARY, dir)
self.__execmd("android update project -n %s -p . -t android-19" % (dir))
self.__execmd("ant clean")
break
print("re-ant project Circle")
oschdir(pathjoin(self.__base_path, TRUNK, CIRCLE))
self.__execmd("android update project -n Circle -p . -t android-19")
property_file = open(PROJECT_PROPERTY, "w")
property_file.write(project_properties)
property_file.flush()
property_file.close()


def make_apk(self):
self.__re_ant()
oschdir(pathjoin(self.__base_path, TRUNK, CIRCLE))
if (self.__use_ndk == True):
self.__re_native()
print("build apk")
#self.__move_qrcode()
self.__execmd("ant debug")
apk_path = pathjoin(self.__base_path, TRUNK, CIRCLE, BIN, APK)
if pathexists(apk_path):
shcopy(apk_path, pathjoin(self.__output_path, APK))
print("Build completed.!")
else:
print("Build failed.")

def move_remote(self, base_path, output_path,oldname):
print "aaa",base_path
print "bbb",output_path
base_file = base_path + '\\'+oldname
output_file=output_path+ '\\'+oldname
print base_file
shutil.copy2(base_file, output_file)
now1 = time.strftime('%Y-%m-%d-%H_%M_%S', time.localtime(time.time()))
newfname="%sCircle-debug.apk"%(now1)
newfpath="%s/%s"%(output_path,newfname)
oldfpath="%s/%s"%(output_path,oldname)
os.rename(oldfpath, newfpath)

pass


def print_help():
print("usage:")
print(" python make_android.py <base_path> <output_path> <use-ndk>")
print(" path must be absolute path, do NOT accept relative path")
print("sample:")
print(" python make_android.py /home/XXX/android /home/XXX/Desktop true")
pass

if __name__ == "__main__":
if len(sys.argv) != 4:
print_help()
exit()
# base_path, output_path
base_path = sys.argv[1]
output_path = sys.argv[2]
use_ndk = sys.argv[3]
if (base_path.startswith(".") or output_path.startswith(".")):
print_help()
exit()
ab = AndroidBuilder(base_path, output_path, use_ndk == "true")
ab.make_apk()
ab.move_remote(local_path, remote_path, APK)

公司的source管理工具,是svn,结果每次拉的代码不是最新的。
后面找到原因了,就是要在命令里面加@head.

文章发表在 testerhome