-
Notifications
You must be signed in to change notification settings - Fork 0
/
buildAAR.py
134 lines (98 loc) · 3.89 KB
/
buildAAR.py
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
#
# Build an Android .aar file suitable for including in projects in Android Studio
# (c) 2014 Paul LeBeau [email protected]
# Released under the Apache v2 license
#
import sys, os, zipfile, StringIO, ConfigParser
# Configration
# The default locations assume that this script has been placed in a subfolder
# of the project (for example: "<MyProject>/aar").
libraryProjectDir = ".\\"
def main():
if not os.path.isdir(libraryProjectDir):
fatal_error("Bad project directory (projectDir) setting")
# We need the canonical name
projectDir = os.path.abspath(libraryProjectDir)
# Where to put the resulting .aar file
outputDir = os.path.join(projectDir, "bin")
# Location of the "bin" dir
binDir = os.path.join(projectDir, "bin")
# Location of AndroidManifest.xml file
manifestFile = os.path.join(projectDir, "AndroidManifest.xml")
# Location of the "res" dir
resDir = os.path.join(projectDir, "res")
# Project name
projectName = os.path.split(projectDir)[1].lower()
# Read properties file
propertiesFile = os.path.join(projectDir, "project.properties")
if not os.path.exists(propertiesFile):
fatal_error("Could not find project.properties file: "+propertiesFile)
projectProperties = read_properties(propertiesFile)
if getProperty(projectProperties, "android.library") != "true":
fatal_error("This is not a Android library project.")
# Check that jar file is present
jarFile = os.path.join(binDir, projectName +".jar")
if not os.path.exists(jarFile):
fatal_error('Could not find "' + projectName + '.jar". Did this project build correctly?')
# Check that manifest file is present (mandatory)
if not os.path.exists(manifestFile):
fatal_error("Could not find AndroidManifest.xml file. Check config at the top of this script.")
# Check that R.txt file is present (mandatory)
RtxtFile = os.path.join(binDir, "R.txt")
if not os.path.exists(RtxtFile):
fatal_error("Could not find R.txt file. Did this project build correctly?")
log("Constructing aar file...")
tmpFile = os.path.join(binDir, "tmp.aar")
# Cleanup old tmp files
#if os.path.exists(tmpFile):
# os.remove(tmpFile)
with zipfile.ZipFile(tmpFile, 'a') as newZip:
# Add manifest file to aar
newZip.write(manifestFile, "AndroidManifest.xml")
# Add classes.jar, which is just the jar file that the Eclipse ADT tools built
newZip.write(jarFile, "classes.jar")
# Add res dir
if os.path.exists(resDir):
log("Copying res dir")
zipWriteDir(newZip, resDir, "res")
# Add R.txt
newZip.write(RtxtFile, "R.txt")
# Add assets dir
assetsDir = os.path.join(projectDir, "assets")
if os.path.exists(assetsDir):
log("Copying assets dir")
zipWriteDir(newZip, assetsDir, "assets")
# New aar (tmp) file successfully created. Rename it to it final name.
aarFile = os.path.join(binDir, projectName + ".aar")
if os.path.exists(aarFile):
os.remove(aarFile)
os.rename(tmpFile, aarFile);
# Done
print '\nDone! Generated aar file can be found at: ' + aarFile
def read_properties(fn):
config = StringIO.StringIO()
config.write("[dummysection]\n")
config.write(open(fn).read())
config.seek(0, os.SEEK_SET)
cp = ConfigParser.ConfigParser()
cp.readfp(config)
return cp
def getProperty(cp, key):
return cp.get("dummysection", key)
def zipWriteDir(myzipfile, dirname, arcdirname):
for dirpath,dirs,files in os.walk(dirname):
relpath = os.path.relpath(dirpath, dirname)
for f in files:
fn = os.path.join(dirpath, f)
myzipfile.write(fn, os.path.join(arcdirname, relpath, f))
def error(msg):
print "ERROR: "+ msg
def warning(msg):
print "WARNING: "+ msg
def log(msg):
print msg
def fatal_error(msg):
error(msg)
exit()
if __name__ == "__main__":
main()