-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathandroid.py
374 lines (283 loc) · 9.31 KB
/
android.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
import os, subprocess, shutil
class Project:
def __init__(self, dir_project, package, dir_sdk, ver_buildtool, ver_platform, dir_keystore):
self.dir_project = dir_project
self.package = package
self.dir_sdk = get_default_sdk(dir_sdk)
self.ver_buildtool = get_default_buildtool(self.dir_sdk, ver_buildtool)
self.ver_platform = get_default_platform(self.dir_sdk, ver_platform)
self.dir_keystore = dir_keystore
def make_directory(project):
# # new structure
# "build"
# "libs"
# "src/main/java/com/example/package"
# "src/main/gen"
# "src/main/res"
# "src/main/assets"
# "src/main/java/AndroidManifest.xml"
print("Making directories and files...")
# generate a folder with a default project structure
os.makedirs(get_dir_src_package(project))
os.makedirs(get_dir_obj(project))
os.makedirs(get_dir_bin(project))
os.makedirs(os.path.join(get_dir_res(project), "layout"))
os.makedirs(os.path.join(get_dir_res(project), "values"))
os.makedirs(os.path.join(get_dir_res(project), "drawable"))
make_mainactivity_java(project)
make_string_xml(project)
make_activity_main_xml(project)
make_androidmanifest_xml(project)
print("Finished.")
def build(project):
# builds a project (makes final apk)
# make r.java
print("Making R.java...")
make_r_java(project)
# compile .java files into .class files
print("Compiling Java code...")
make_classes(project)
# compile .class files into classes.dex
print("Compiling into Dalvik code...")
make_dex(project)
# compile into apk
print("Making APK file...")
make_apk(project)
# apk alignment
print("Aligning APK...")
apk_align(project)
# apk signing
print("Signing APK...")
apk_sign(project)
print("Finished.")
def launch(project):
print("Installing APK...")
# install apk
adb_install(project)
print("Launching...")
# launch application
adb_launch(project)
print("Finished.")
## internal
def get_default_sdk(sdk):
if sdk == None:
sdk = os.getenv('ANDROID_SDK_ROOT')
if sdk == None:
raise Exception("ANDROID_SDK_ROOT env var missing!")
return sdk
def get_default_buildtool(sdk, buildtool):
if buildtool == None:
list_buildtools = sorted(os.listdir( os.path.join(sdk, "build-tools") ))
return list_buildtools[-1]
def get_default_platform(sdk, platform):
if platform == None:
list_platforms = sorted(os.listdir( os.path.join(sdk, "platforms") ))
return list_platforms[-1]
def make_mainactivity_java(project):
with open(os.path.join(get_dir_src_package(project), "MainActivity.java"), "w") as file:
file.write(R"""package """+ project.package +R""";
import android.app.Activity;
import android.os.Bundle;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}""")
def make_string_xml(project):
with open(os.path.join(get_dir_res(project), "values", "strings.xml"), "w") as file:
file.write(R"""<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">"""+ get_name(project) +R"""</string>
<string name="hello_world">Hello, world!</string>
</resources>""")
def make_activity_main_xml(project):
with open(os.path.join(get_dir_res(project), "layout", "activity_main.xml"), "w") as file:
file.write(R"""<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/hello_world"
tools:context=".MainActivity"
/>
</RelativeLayout>""")
def make_androidmanifest_xml(project):
with open(get_dir_android_manifest(project), "w") as file:
file.write(R"""<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:a='http://schemas.android.com/apk/res/android'
package='"""+ project.package +R"""'
a:versionCode='1'
a:versionName='1'
>
<application a:label='"""+ get_name(project) +R"""'>
<activity a:name='"""+ project.package +R""".MainActivity'>
<intent-filter>
<category a:name='android.intent.category.LAUNCHER'/>
<action a:name='android.intent.action.MAIN'/>
</intent-filter>
</activity>
</application>
</manifest>""")
def make_r_java(project):
# os.remove(os.path.join(get_dir_src_package(project), "R.java"))
subprocess.check_call(
[
get_dir_aapt(project), "package", "-f", "-m",
"-J", get_dir_src(project),
"-M", get_dir_android_manifest(project),
"-S", get_dir_res(project),
"-I", get_dir_android_jar(project)
]
)
def make_classes(project):
shutil.rmtree(get_dir_obj(project))
os.makedirs(get_dir_obj(project))
# -bootclasspath is deprecated, I'm not sure how to replace it properly, just putting it in the classpath now.
# I'm having to use Java 11, d8 doesn't work with anything higher, don't know why.
subprocess.check_call(
[
get_dir_javac(project),
"-d", get_dir_obj(project),
"-classpath", ";".join([get_dir_android_jar(project), get_dir_src(project)]),
# "-classpath", get_dir_src(project),
# "-bootclasspath", get_dir_android_jar(project),
"-source", "11",
"-target", "11",
os.path.join(get_dir_src_package(project), "*.java"),
os.path.join(get_dir_src_package(project), "R.java"),
],
)
def make_dex(project):
# os.remove(get_dir_classes_dex(project))
# Aparently d8.bat and dx.bat are bugged and require manual adjustments to work. Because of that I'm directly calling the jar file from here.
#'''
buildlib = os.path.join(project.dir_sdk, "build-tools", project.ver_buildtool, "lib")
d8jar = os.path.join(buildlib, "d8.jar")
subprocess.check_call(
[
"java",
"-classpath", ";".join([buildlib, d8jar]),
"com.android.tools.r8.D8",
os.path.join(get_dir_obj(project), project.package.replace(".", os.sep), "*.class"),
"--release",
"--output", project.dir_project,
"--lib", get_dir_android_jar(project),
"--classpath", get_dir_src(project),
]
)
#'''
'''
subprocess.check_call(
[
get_dir_d8(project),
os.path.join(get_dir_obj(project), project.package.replace(".", os.sep), "*.class"),
"--release",
"--output", project.dir_project,
"--lib", get_dir_android_jar(project),
# "--classpath", get_dir_src(project),
]
)
'''
'''
subprocess.check_call(
[
get_dir_dx(project), "--dex",
"--output", get_dir_classes_dex(project),
get_dir_obj(project)
]
)
'''
def make_apk(project):
# os.remove(get_dir_apk(project))
subprocess.check_call(
[
get_dir_aapt(project), "package", "-f", "-m",
"-F", get_dir_apk(project),
"-M", get_dir_android_manifest(project),
"-S", get_dir_res(project),
"-I", get_dir_android_jar(project)
]
)
subprocess.check_call(
[
get_dir_aapt(project), "add", "-k",
get_dir_apk(project),
get_dir_classes_dex(project)
]
)
def apk_align(project):
subprocess.check_call(
[
get_dir_zipalign(project), "-f", "4",
get_dir_apk(project), get_dir_apk(project) + '.temp'
]
)
os.replace(get_dir_apk(project) + '.temp', get_dir_apk(project))
def apk_sign(project):
subprocess.check_call(
[
get_dir_apksigner(project), "sign",
"--ks", get_dir_keystore(project), get_dir_apk(project)
]
)
def adb_install(project):
subprocess.check_call(
[
get_dir_adb(project), "install", "-r", get_dir_apk(project)
]
)
def adb_launch(project):
# adb shell monkey -p app.package.name -c android.intent.category.LAUNCHER 1
subprocess.check_call(
[
get_dir_adb(project), "shell", "monkey",
"-p", project.package,
"-c", "android.intent.category.LAUNCHER", "1"
]
)
## getting stuff
def get_dir_aapt(project):
return os.path.join(project.dir_sdk, "build-tools", project.ver_buildtool, "aapt")
def get_dir_javac(project):
return "javac"
def get_dir_dx(project):
return os.path.join(project.dir_sdk, "build-tools", project.ver_buildtool, "dx.bat")
def get_dir_d8(project):
return os.path.join(project.dir_sdk, "build-tools", project.ver_buildtool, "d8.bat")
def get_dir_zipalign(project):
return os.path.join(project.dir_sdk, "build-tools", project.ver_buildtool, "zipalign")
def get_dir_apksigner(project):
return os.path.join(project.dir_sdk, "build-tools", project.ver_buildtool, "apksigner.bat")
def get_dir_adb(project):
return os.path.join(project.dir_sdk, "platform-tools", "adb")
def get_dir_src(project):
return os.path.join(project.dir_project, "src")
def get_dir_src_package(project):
return os.path.join(project.dir_project, "src", project.package.replace(".", os.sep))
def get_dir_res(project):
return os.path.join(project.dir_project, "res")
def get_dir_obj(project):
return os.path.join(project.dir_project, "obj")
def get_dir_bin(project):
return os.path.join(project.dir_project, "bin")
def get_dir_apk(project):
return os.path.join(project.dir_project, "bin", get_name(project) + ".apk")
def get_dir_android_manifest(project):
return os.path.join(project.dir_project, "AndroidManifest.xml")
def get_dir_classes_dex(project):
return os.path.join(project.dir_project, "classes.dex")
def get_dir_keystore(project):
return os.path.join(project.dir_keystore)
def get_dir_android_jar(project):
return os.path.join(project.dir_sdk, "platforms", project.ver_platform, "android.jar")
def get_name(project):
return os.path.basename(project.dir_project)