-
Notifications
You must be signed in to change notification settings - Fork 0
/
BuildAndroid.py
394 lines (341 loc) · 15.2 KB
/
BuildAndroid.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
import os
import subprocess
import configparser
import shutil
import html.parser
import pathlib
import urllib
import urllib.request
import ssl
import re
import zipfile
import stat
import sys
TEMPLATES = {}
TEMPLATES["settings.gradle"] = """include ':app'
"""
TEMPLATES["build.gradle"] = """
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.5.2'
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
"""
TEMPLATES["app/build.gradle"] = """
apply plugin: 'com.android.application'
android {
compileSdkVersion 29
defaultConfig {
applicationId = '$$$Android.AppPackageName$$$'
minSdkVersion 14
targetSdkVersion 28
externalNativeBuild {
cmake {
arguments '-DANDROID_STL=c++_static'
}
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'),
'proguard-rules.pro'
}
}
externalNativeBuild {
cmake {
path 'src/main/cpp/CMakeLists.txt'
}
}
}
"""
"""
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
}
"""
TEMPLATES["app/src/main/AndroidManifest.xml"] = """
<!-- BEGIN_INCLUDE(manifest) -->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="$$$Android.AppPackageName$$$"
android:versionCode="$$$Build.Iteration$$$"
android:versionName="$$$Build.Version$$$">
<!-- This .apk has no Java code itself, so set hasCode to false. -->
<application
android:allowBackup="false"
android:fullBackupContent="false"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:hasCode="false">
<!-- Our activity is the built-in NativeActivity framework class.
This will take care of integrating with our NDK code. -->
<activity android:name="android.app.NativeActivity"
android:label="@string/app_name"
android:configChanges="orientation|keyboardHidden">
<!-- Tell NativeActivity the name of our .so -->
<meta-data android:name="android.app.lib_name"
android:value="native_$$$Application.BinaryName$$$" />
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
<!-- END_INCLUDE(manifest) -->
"""
TEMPLATES["app/src/main/cpp/CMakeLists.txt"] = """
cmake_minimum_required(VERSION 3.4.1)
project($$$Application.BinaryName$$$)
add_subdirectory("$$$SrcDir$$$" $$$Application.BinaryName$$$)
# build native_app_glue as a static lib
set(${CMAKE_C_FLAGS}, "${CMAKE_C_FLAGS}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11 -Wall -Werror")
# Export ANativeActivity_onCreate(),
# Refer to: https://github.com/android-ndk/ndk/issues/381.
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -u ANativeActivity_onCreate")
add_library(native_$$$Application.BinaryName$$$ SHARED ${ANDROID_NDK}/sources/android/native_app_glue/android_native_app_glue.c)
target_include_directories(native_$$$Application.BinaryName$$$ PRIVATE ${ANDROID_NDK}/sources/android/native_app_glue)
# add lib dependencies
#find_library(applib $$$Application.BinaryName$$$ HINTS ${CMAKE_CURRENT_LIST_DIR}/../../../../ ${CMAKE_CURRENT_LIST_DIR}../../../ ${CMAKE_CURRENT_LIST_DIR}../../../../../)
set(applib $$$Application.BinaryName$$$)
if (NOT TARGET ${applib})
message(FATAL_ERROR "Cannot find Target ${applib}"e)
endif()
target_include_directories(${applib} PRIVATE ${ANDROID_NDK}/sources/android/native_app_glue)
target_link_libraries(native_$$$Application.BinaryName$$$ ${applib} android EGL GLESv2 GLESv1_CM log)
"""
TEMPLATES["app/src/main/res/values/strings.xml"] = """<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">$$$Application.Name$$$</string>
</resources>
"""
IMAGES = {}
IMAGES["app/src/main/res/mipmap-hdpi/ic_launcher.png"] = {"img" : "Icon", "width": 72, "height": 72}
IMAGES["app/src/main/res/mipmap-mdpi/ic_launcher.png"] = {"img" : "Icon", "width": 48, "height": 48}
IMAGES["app/src/main/res/mipmap-xhdpi/ic_launcher.png"] = {"img" : "Icon", "width": 96, "height": 96}
IMAGES["app/src/main/res/mipmap-xxhdpi/ic_launcher.png"] = {"img" : "Icon", "width": 144, "height": 144}
class HTMLUrlExtractor(html.parser.HTMLParser):
def __init__(self, url):
text = urllib.request.urlopen(url, timeout=10, context=ssl._create_unverified_context()).read().decode("utf-8")
self.baseurl = url
self.urls = {}
self.href = None
self.text = None
super(HTMLUrlExtractor, self).__init__()
self.feed(text)
def handle_starttag(self, tag, attrs):
if tag == "a":
self.text = ""
self.href = next((urllib.parse.urljoin(self.baseurl, attr[1]) for attr in attrs if attr[0] == "href"), None)
def handle_endtag(self, tag):
if self.href is not None:
# print(self.href, self.text)
self.urls[self.href] = self.text
self.href = None
self.text = None
def handle_data(self, data):
if self.href is not None:
self.text = data
class BuildEnv:
def __init__ (self):
self._config = configparser.ConfigParser()
self._configfilename = os.path.join(os.path.dirname(os.path.realpath(__file__)), ".config")
if (os.path.exists(self._configfilename)):
self._config.read(self._configfilename)
def _ConfigValue(self, name):
val = self._config
for p in name.split("."):
if not p in val: return None
val = val[p]
return val
def _WriteValue(self, name, value):
names = name.split(".")
c = self._config
if (len(names) > 1):
for d in names[0:-1]:
if d not in c: c[d] = {}
c = c[d]
c[names[-1]] = str(value)
with open(self._configfilename, 'w') as fd: self._config.write(fd)
return value
def _DetectValue(self, name, envvars):
if envvars:
for e in envvars:
p = os.path.exists(os.path.expandvars(e))
if p: return p
return self._WriteValue(name, input("Cannot Detect Path for : " + name + " :: "))
def _SearchExeInPath(self, bindir, name):
path = None
if sys.platform == "win32" or sys.platform == "Windows":
path = path or next(pathlib.Path(bindir).rglob(name + ".exe"), None) or next(pathlib.Path(bindir).rglob(name + ".bat"), None)
path = path or next(pathlib.Path(bindir).rglob(name), None)
if path: os.chmod(path, os.stat(path).st_mode | stat.S_IEXEC)
return str(path) if path else None
def _Download(self, downloadpath, name, url, pattern):
if pattern != None:
url = [u for u in HTMLUrlExtractor(url).urls if re.search(pattern, u)][0]
downloadtofile = os.path.join(downloadpath,"tmp", name + ".zip")
os.makedirs(os.path.dirname(downloadtofile), exist_ok=True)
bindir = os.path.join(downloadpath, name)
if not os.path.exists(bindir):
print("Downloading {} at {} :: Url = {}".format(name, downloadpath, url))
if not os.path.exists(downloadtofile):
urllib.request.urlretrieve(url, downloadtofile)
zip_ref = zipfile.ZipFile(downloadtofile, 'r')
extractdir = os.path.join(downloadpath, name)
zip_ref.extractall(extractdir)
zip_ref.close()
os.remove(downloadtofile)
return self._SearchExeInPath(bindir, name)
def _FindOrGetConfig(self, name, envvars):
return self._ConfigValue(name) or self._DetectValue(name, envvars)
def _FindOrDownload(self, name, config, envvars, url, pattern):
p = shutil.which(name)
if p: return p
p = self._ConfigValue(config)
if p: p = self._SearchExeInPath(p, name)
if p: return p
path = self._FindOrGetConfig("BuildEnv.BinDownloadPath", ["BUILD_TOOLS_DOWNLOAD_PATH"])
return self._WriteValue(config, self._Download(path, name, url, pattern))
def GetJava8(self):
return self._SearchExeInPath(os.path.dirname(os.path.dirname(self.GetAndroidStudioPath())), 'java')
LinkDir = 'https://jdk.java.net/java-se-ri/8-MR3'
Pattern = 'openjdk-8u41-b04-windows-i586-14_jan_2020.zip'
return self._FindOrDownload('java', config = "Android.JavaPath", envvars = ['${JAVA_HOME}/bin'], url = LinkDir, pattern = Pattern)
def GetAndroidSdkRoot(self):
LinkDir = 'https://developer.android.com/studio'
Pattern = 'commandlinetools-win-6200805_latest.zip'
return self._FindOrDownload('sdkmanager', config = "Android.SdkManagerPath", envvars = ['${ANDROID_SDK_ROOT}/tools/bin'], url = LinkDir, pattern = Pattern)
#return self._FindOrGetConfig("Android.SdkRoot", ['${ANDROID_SDK_ROOT}', '${ANDROID_HOME}'])
def GetAndroidStudioPath(self):
LinkDir = 'https://developer.android.com/studio'
Pattern = 'android-studio-ide-192.6308749-windows.zip'
return self._FindOrDownload('studio', config = "Android.StudioPath", envvars = ['${ANDROID_STUDIO_PATH}/bin'], url = LinkDir, pattern = Pattern)
return self._FindOrGetConfig("Android.StudioPath", ['${ANDROID_HOME}'])
def GetImageMagick(self):
LinkDir = 'https://imagemagick.org/download/binaries/'
Pattern = '\\bImageMagick-7.*-portable-Q16-x64.zip\\b'
return os.path.dirname(self._FindOrDownload('magick', config = "ImageMagick.Path", envvars = None, url = LinkDir, pattern = Pattern))
def GetGradlePath(self):
LinkDir = "https://services.gradle.org/distributions/"
Pattern = 'gradle-5.4.1-bin.zip'
return self._FindOrDownload('gradle', config = "Android.GradlePath", envvars = None, url = LinkDir, pattern = Pattern)
def GetBuildPath(self):
return self._FindOrGetConfig("BuildEnv.BuildPath", ['${BUILD_PATH}'])
def GetCMakePath(self):
LinkDir = { "Windows" : 'https://github.com/Kitware/CMake/releases' }.get(sys.platform, None)
Pattern = { "Windows" : 'cmake-3\.??\..*-win64-x64.zip' }.get(sys.platform, None)
return self._FindOrDownload('cmake', config = "BuildEnv.CMakePath", envvars = None, url = LinkDir, pattern = Pattern)
def GetDownloadPath(self):
return self._FindOrGetConfig("BuildEnv.BinDownloadPath")
class AndroidApk:
def __init__(self, config):
self.buildenv = BuildEnv()
self.config = configparser.ConfigParser()
self.config.read(str(config))
self.configfile = config
self.gradle = self.buildenv.GetGradlePath()
self.imagemagick_convert = os.path.join(self.buildenv.GetImageMagick(), 'convert')
self.builddir = os.path.join(self.buildenv.GetBuildPath(), self._ConfigValue("Android.AppPackageName"))
self.sdkroot = os.path.dirname(os.path.dirname(os.path.dirname(self.buildenv.GetAndroidSdkRoot())))
self.javahome = os.path.dirname(os.path.dirname(self.buildenv.GetJava8()))
self.cmake = self.buildenv.GetCMakePath()
self.sdkmanager = self.buildenv.GetAndroidSdkRoot()
self.env = {
"ANDROID_HOME" : self.sdkroot,
"ANDROID_SDK_ROOT" : self.sdkroot,
"JAVA_HOME" : self.javahome
}
os.makedirs(self.builddir, exist_ok=True)
# self.studiopath = self.buildenv.GetAndroidStudioPath()
def Generate(self):
self._Run([self.gradle, "wrapper"])
for k, v in TEMPLATES.items():
self._GenerateFileWithContents(k, self._ExpandTemplate(v))
for k, v in IMAGES.items():
self._GenerateImage(k, v)
self._Run([self.sdkmanager, "--sdk_root=" + self.sdkroot, "--licenses"])
def AcceptLicenses(self):
p = subprocess.Popen([self.sdkmanager, "--sdk_root=" + self.sdkroot, "--licenses"], stdout=subprocess.PIPE,stdin=subprocess.PIPE)
p.communicate()[0]
p.stdin.close()
def _Run(self, cmd, extraenv = None):
my_env = os.environ
for k, v in self.env.items():
my_env[k] = v
r, w = os.pipe()
os.write(w, b'y\ny\ny\ny\ny\ny\ny\ny\ny\ny\ny\ny\ny\ny\ny\ny\ny\ny\ny\ny\ny\ny\ny\ny\ny\ny\n\ny\ny\ny\ny\ny\ny\ny\ny\ny\n') #expects a bytes type object
os.close(w)
rslt = subprocess.run(cmd, cwd=self.builddir, stdin=r, capture_output=True, env = my_env, shell=True)
if rslt.returncode != 0:
print(" ".join(cmd))
print(self.env)
print(self.builddir)
raise Exception("Error Building : Command = " + " ".join(cmd) + "\n\n STDOUT = " + rslt.stdout.decode() + "\n\nSTDERR = " + rslt.stderr.decode())
return rslt.stdout.decode()
def Build(self):
self._Run(["gradlew", "build"])
def _GenerateFileWithContents(self, path, newcontents):
fname = os.path.join(self.builddir, path)
os.makedirs(os.path.dirname(fname), exist_ok=True)
contents = open(fname, 'r').read() if os.path.exists(fname) else None
if contents == newcontents: return
open(fname, 'w').write(newcontents)
def _GenerateImage(self, path, imginfo):
if os.path.exists(path): return
srcsvg = os.path.join(os.path.dirname(self.configfile), self.config["Images"][imginfo["img"]])
if not os.path.exists(srcsvg): raise Exception("Cannot find Image Source: ", srcsvg)
fname = os.path.join(self.builddir, path)
os.makedirs(os.path.dirname(fname), exist_ok=True)
extent = str(imginfo["width"]) + "x" + str(imginfo["height"])
if "scalex" in imginfo.keys() and "scaley" in imginfo.keys():
scale = str(imginfo["scalex"]) + "x" + str(imginfo["scaley"])
else:
scale = extent
self._Run([
self.imagemagick_convert,
"-density", scale,
"-extent", extent ,
"-gravity", "center",
"-background", "none",
srcsvg,
fname,
])
def _ConfigValue(self, name):
if name == "SrcDir": return str(os.path.dirname(self.configfile)).replace("\\", "\\\\")
val = self.config
for p in name.split("."):
val = val[p]
return val
def _ExpandTemplate(self, tmpl):
startmarker = '$$$'
endmarker = '$$$'
retval = tmpl
offset= retval.find(startmarker, 0)
while(offset != -1):
s = offset + len(startmarker)
e = retval.find(endmarker, s)
val = self._ConfigValue(retval[s:e])
retval = retval[0:offset] + val + retval[e + len(endmarker):]
offset = retval.find(startmarker, offset)
return retval
builder = AndroidApk(os.path.join(os.path.dirname(os.path.realpath(__file__)), "appmanifest.config"))
builder.Generate()
builder.Build()