-
Notifications
You must be signed in to change notification settings - Fork 628
Expand file tree
/
Copy pathoppia_android_application.bzl
More file actions
582 lines (518 loc) · 21.4 KB
/
Copy pathoppia_android_application.bzl
File metadata and controls
582 lines (518 loc) · 21.4 KB
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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
"""
Macros pertaining to building & managing Android app bundles.
"""
load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo")
load("@bazel_skylib//rules:copy_file.bzl", "copy_file")
def _convert_apk_to_aab_module_impl(ctx):
output_file = ctx.outputs.output_file
input_file = ctx.attr.input_file.files.to_list()[0]
# See aapt2 help documentation for details on the arguments passed here.
arguments = [
"convert",
"--output-format",
"proto",
"-o",
output_file.path,
input_file.path,
]
# Reference: https://docs.bazel.build/versions/master/skylark/lib/actions.html#run.
ctx.actions.run(
outputs = [output_file],
inputs = ctx.files.input_file,
tools = [ctx.executable._aapt2_tool],
executable = ctx.executable._aapt2_tool.path,
arguments = arguments,
mnemonic = "GenerateAndroidAppBundleModuleFromApk",
progress_message = "Generating app bundle AAB",
)
return DefaultInfo(
files = depset([output_file]),
runfiles = ctx.runfiles(files = [output_file]),
)
def _convert_module_aab_to_structured_zip_impl(ctx):
output_file = ctx.outputs.output_file
input_file = ctx.attr.input_file.files.to_list()[0]
command = """
# Extract AAB to working directory.
WORKING_DIR=$(mktemp -d)
unzip -q -d $WORKING_DIR {0}
# Create the expected directory structure for an app bundle.
# Reference for copying all other files to root: https://askubuntu.com/a/951768.
mkdir -p $WORKING_DIR/assets $WORKING_DIR/dex $WORKING_DIR/manifest $WORKING_DIR/root
mv $WORKING_DIR/*.dex $WORKING_DIR/dex/
mv $WORKING_DIR/AndroidManifest.xml $WORKING_DIR/manifest/
ls -d $WORKING_DIR/* | grep -v -w -E "res|assets|dex|manifest|root|resources.pb" | xargs -I{{}} sh -c "mv \\$0 $WORKING_DIR/root/ || exit 255" {{}} 2>&1 || exit $?
# Zip up the result--this will be used by bundletool to build a deployable AAB. Note that these
# strange file path bits are needed because zip will always retain the directory structure
# passed via arguments (necessitating changing into the working directory).
DEST_FILE_PATH="$(pwd)/{1}"
cd $WORKING_DIR
zip -q -r $DEST_FILE_PATH .
""".format(input_file.path, output_file.path)
# Reference: https://docs.bazel.build/versions/main/skylark/lib/actions.html#run_shell.
ctx.actions.run_shell(
outputs = [output_file],
inputs = ctx.files.input_file,
tools = [],
command = command,
mnemonic = "ConvertModuleAabToStructuredZip",
progress_message = "Correcting app bundle structure",
)
return DefaultInfo(
files = depset([output_file]),
runfiles = ctx.runfiles(files = [output_file]),
)
def _restrict_languages_in_raw_module_zip_impl(ctx):
input_file = ctx.file.input_file
output_file = ctx.outputs.output_file
arguments = ctx.actions.args()
arguments.add(input_file)
arguments.add(output_file)
ctx.actions.run(
outputs = [output_file],
inputs = [input_file],
tools = [ctx.executable._filter_per_language_resources_tool],
executable = ctx.executable._filter_per_language_resources_tool.path,
arguments = [arguments],
mnemonic = "RestrictLanguagesInAabModule",
progress_message = "Removing unused language resources",
)
return DefaultInfo(
files = depset([output_file]),
runfiles = ctx.runfiles(files = [output_file]),
)
def _bundle_module_zip_into_deployable_aab_impl(ctx):
output_file = ctx.outputs.output_file
input_file = ctx.attr.input_file.files.to_list()[0]
config_file = ctx.attr.config_file.files.to_list()[0]
# Reference: https://developer.android.com/studio/build/building-cmdline#build_your_app_bundle_using_bundletool.
arguments = [
"build-bundle",
"--modules=%s" % input_file.path,
"--config=%s" % config_file.path,
"--output=%s" % output_file.path,
]
# Reference: https://docs.bazel.build/versions/master/skylark/lib/actions.html#run.
ctx.actions.run(
outputs = [output_file],
inputs = ctx.files.input_file + ctx.files.config_file,
tools = [ctx.executable._bundletool_tool],
executable = ctx.executable._bundletool_tool.path,
arguments = arguments,
mnemonic = "GenerateDeployAabFromModuleZip",
progress_message = "Generating deployable AAB",
)
return DefaultInfo(
files = depset([output_file]),
runfiles = ctx.runfiles(files = [output_file]),
)
def _package_metadata_into_deployable_aab_impl(ctx):
output_aab_file = ctx.outputs.output_aab_file
input_aab_file = ctx.attr.input_aab_file.files.to_list()[0]
proguard_map_file = ctx.attr.proguard_map_file.files.to_list()[0]
command = """
# Extract deployable AAB to working directory.
WORKING_DIR=$(mktemp -d)
echo $WORKING_DIR
cp {0} $WORKING_DIR/temp.aab || exit 255
# Change the permissions of the AAB copy so that it can be overwritten later.
chmod 755 $WORKING_DIR/temp.aab || exit 255
# Create directory needed for storing bundle metadata.
mkdir -p $WORKING_DIR/BUNDLE-METADATA/com.android.tools.build.obfuscation
# Copy over the Proguard map file.
cp {1} $WORKING_DIR/BUNDLE-METADATA/com.android.tools.build.obfuscation/proguard.map || exit 255
$ Repackage the AAB file into the destination.
DEST_FILE_PATH="$(pwd)/{2}"
cd $WORKING_DIR
zip -q -Dur temp.aab BUNDLE-METADATA || exit 255
cp temp.aab $DEST_FILE_PATH || exit 255
""".format(input_aab_file.path, proguard_map_file.path, output_aab_file.path)
# Reference: https://docs.bazel.build/versions/main/skylark/lib/actions.html#run_shell.
ctx.actions.run_shell(
outputs = [output_aab_file],
inputs = ctx.files.input_aab_file + ctx.files.proguard_map_file,
tools = [],
command = command,
mnemonic = "PackageMetadataIntoDeployableAAB",
progress_message = "Packaging symbols file into AAB",
)
return DefaultInfo(
files = depset([output_aab_file]),
runfiles = ctx.runfiles(files = [output_aab_file]),
)
def _sign_and_rename_aab_impl(ctx):
# Extract jarsigner from the Bazel Java runtime.
java_runtime = ctx.toolchains["@bazel_tools//tools/jdk:runtime_toolchain_type"].java_runtime
java_bin_path = java_runtime.java_executable_exec_path
jarsigner_path = java_bin_path[:java_bin_path.rfind("/")] + "/jarsigner"
input_aab = ctx.file.input_aab
extracted_key_alias = ctx.attr.key_alias[BuildSettingInfo].value
bundletool = ctx.executable._bundletool_tool
# Determine which keystore to use.
extracted_keystore_filepath = ctx.attr.keystore[BuildSettingInfo].value
extracted_keystore_password_filepath = ctx.attr.keystore_password_file[BuildSettingInfo].value
additional_keystore_inputs = []
if extracted_keystore_filepath and extracted_keystore_password_filepath and extracted_key_alias:
keystore_filepath = extracted_keystore_filepath
keystore_password_filepath = extracted_keystore_password_filepath
key_alias = extracted_key_alias
else:
# Fall back to the default debug keystore (which requires a password file).
keystore_filepath = ctx.file._debug_keystore.path
additional_keystore_inputs.append(ctx.file._debug_keystore)
key_alias = ctx.attr._debug_key_alias
debug_password_file = ctx.actions.declare_file(ctx.label.name + "_debug_password.txt")
ctx.actions.write(
output = debug_password_file,
content = "android",
)
keystore_password_filepath = debug_password_file.path
additional_keystore_inputs.append(debug_password_file)
output_aab = ctx.actions.declare_file(ctx.label.name + ".aab")
output_dir = ctx.actions.declare_directory(ctx.label.name + "_release")
command = """
# Ensure that subshells correctly bubble their failures to the outer shell.
set -o pipefail
mkdir -p {output_dir}
cp {input_aab} {output_aab} || exit 255
VERSION_NAME=$({bundletool} dump manifest --bundle={input_aab} | grep -o 'android:versionName="[^"]*"' | cut -d'"' -f2) || exit 255
RENAMED_AAB_PATH="{output_dir}/oppia-android-$VERSION_NAME.aab"
cp {input_aab} $RENAMED_AAB_PATH || exit 255
# Fall back to system jarsigner if the derived path from the toolchain doesn't exist or is not executable
# (e.g. when using Bazel's embedded remotejdk which doesn't ship development tools like jarsigner).
JARSIGNER="{jarsigner_path}"
if [ ! -x "$JARSIGNER" ]; then
JARSIGNER="jarsigner"
fi
JARSIGNER_LOG_FILE=$(mktemp)
if ! $JARSIGNER -keystore {keystore} -storepass:file {keystore_password_file} -keypass:file {keystore_password_file} $RENAMED_AAB_PATH "{key_alias}" > "$JARSIGNER_LOG_FILE" 2>&1 ; then
cat "$JARSIGNER_LOG_FILE" >&2
rm -f "$JARSIGNER_LOG_FILE"
exit 255
fi
rm -f "$JARSIGNER_LOG_FILE"
echo "Dev-only AAB: bazel-bin/{name}.aab"
echo "Renamed Release AAB: bazel-bin/{name}_release/oppia-android-$VERSION_NAME.aab"
echo ""
""".format(
input_aab = input_aab.path,
keystore = keystore_filepath,
keystore_password_file = keystore_password_filepath,
key_alias = key_alias,
bundletool = bundletool.path,
jarsigner_path = jarsigner_path,
output_aab = output_aab.path,
output_dir = output_dir.path,
name = ctx.label.name,
)
ctx.actions.run_shell(
outputs = [output_aab, output_dir],
inputs = [input_aab, ctx.info_file] + additional_keystore_inputs,
tools = depset(
direct = [bundletool],
transitive = [java_runtime.files],
),
command = command,
mnemonic = "SignAndRenameAab",
progress_message = "Re-signing/renaming AAB for deployment",
execution_requirements = {
"no-cache": "1", # Disable caching to try and coerce re-printing the renamed binary.
"local": "1", # Ensure the local 'jarsigner' command can be accessed, if needed.
},
)
return DefaultInfo(
files = depset([output_aab, output_dir]),
runfiles = ctx.runfiles(files = [output_aab, output_dir]),
)
def _generate_universal_apk_impl(ctx):
input_aab_file = ctx.attr.input_aab_file.files.to_list()[0]
output_apk_file = ctx.outputs.output_apk_file
debug_keystore_file = ctx.attr.debug_keystore.files.to_list()[0]
apks_file = ctx.actions.declare_file("%s_processed.apks" % ctx.label.name)
# Reference: https://developer.android.com/tools/bundletool#generate_apks.
# See also the Bazel BUILD file for the keystore for details on its password and alias.
generate_universal_apk_arguments = [
"build-apks",
"--bundle=%s" % input_aab_file.path,
"--output=%s" % apks_file.path,
"--ks=%s" % debug_keystore_file.path,
"--ks-pass=pass:android",
"--ks-key-alias=androiddebugkey",
"--key-pass=pass:android",
"--mode=universal",
]
# bundletool only generates an APKs file, so the universal APK still needs to be extracted.
# Reference: https://docs.bazel.build/versions/master/skylark/lib/actions.html#run.
ctx.actions.run(
outputs = [apks_file],
inputs = ctx.files.input_aab_file + ctx.files.debug_keystore,
tools = [ctx.executable._bundletool_tool],
executable = ctx.executable._bundletool_tool.path,
arguments = generate_universal_apk_arguments,
mnemonic = "GenerateUniversalAPK",
progress_message = "Generating universal APK from AAB",
)
command = """
# Extract APK to working directory.
unzip -q "$(pwd)/{0}" universal.apk
mv universal.apk "$(pwd)/{1}"
""".format(apks_file.path, output_apk_file.path)
# Reference: https://docs.bazel.build/versions/main/skylark/lib/actions.html#run_shell.
ctx.actions.run_shell(
outputs = [output_apk_file],
inputs = [apks_file],
tools = [],
command = command,
mnemonic = "ExtractUniversalAPK",
progress_message = "Extracting universal APK from .apks file",
)
return DefaultInfo(
files = depset([output_apk_file]),
runfiles = ctx.runfiles(files = [output_apk_file]),
)
_convert_apk_to_module_aab = rule(
attrs = {
"input_file": attr.label(
allow_single_file = True,
mandatory = True,
),
"output_file": attr.output(
mandatory = True,
),
"_aapt2_tool": attr.label(
executable = True,
cfg = "host",
default = "@androidsdk//:aapt2_binary",
),
},
implementation = _convert_apk_to_aab_module_impl,
)
_convert_module_aab_to_structured_zip = rule(
attrs = {
"input_file": attr.label(
allow_single_file = True,
mandatory = True,
),
"output_file": attr.output(
mandatory = True,
),
},
implementation = _convert_module_aab_to_structured_zip_impl,
)
_restrict_languages_in_raw_module_zip = rule(
attrs = {
"input_file": attr.label(
allow_single_file = True,
mandatory = True,
),
"output_file": attr.output(
mandatory = True,
),
"_filter_per_language_resources_tool": attr.label(
executable = True,
cfg = "host",
default = "//scripts:filter_per_language_resources",
),
},
implementation = _restrict_languages_in_raw_module_zip_impl,
)
_bundle_module_zip_into_deployable_aab = rule(
attrs = {
"input_file": attr.label(
allow_single_file = True,
mandatory = True,
),
"config_file": attr.label(
allow_single_file = True,
mandatory = True,
),
"output_file": attr.output(
mandatory = True,
),
"_bundletool_tool": attr.label(
executable = True,
cfg = "host",
default = "//third_party:android_bundletool_binary",
),
},
implementation = _bundle_module_zip_into_deployable_aab_impl,
)
_package_metadata_into_deployable_aab = rule(
attrs = {
"input_aab_file": attr.label(
allow_single_file = True,
mandatory = True,
),
"proguard_map_file": attr.label(
allow_single_file = True,
mandatory = True,
),
"output_aab_file": attr.output(
mandatory = True,
),
},
implementation = _package_metadata_into_deployable_aab_impl,
)
_sign_and_rename_aab = rule(
attrs = {
"input_aab": attr.label(
allow_single_file = True,
mandatory = True,
),
"keystore": attr.label(
mandatory = True,
),
"keystore_password_file": attr.label(
mandatory = True,
),
"key_alias": attr.label(mandatory = True),
"_bundletool_tool": attr.label(
executable = True,
cfg = "host",
default = "//third_party:android_bundletool_binary",
),
"_debug_keystore": attr.label(
default = Label("@bazel_tools//tools/android:debug_keystore"),
allow_single_file = True,
),
"_debug_key_alias": attr.string(default = "androiddebugkey"),
},
toolchains = ["@bazel_tools//tools/jdk:runtime_toolchain_type"],
implementation = _sign_and_rename_aab_impl,
)
_generate_universal_apk = rule(
attrs = {
"input_aab_file": attr.label(
allow_single_file = [".aab"],
mandatory = True,
),
"output_apk_file": attr.output(
mandatory = True,
),
"debug_keystore": attr.label(
allow_single_file = True,
mandatory = True,
),
"_bundletool_tool": attr.label(
executable = True,
cfg = "host",
default = "//third_party:android_bundletool_binary",
),
},
implementation = _generate_universal_apk_impl,
)
def oppia_android_application(name, config_file, proguard_generate_mapping, production_release, **kwargs):
"""
Creates an Android App Bundle (AAB) binary with the specified name and arguments.
This generates a mobile-installable target that ends in '_binary'. For example, if there's an
Oppia Android application defined with the name 'oppia_dev' then its APK binary can be
mobile-installed using:
bazel mobile-install //:oppia_dev_binary
Args:
name: str. The name of the Android App Bundle to build. This will corresponding to the name
of the generated .aab file.
config_file: target. The path to the .pb.json bundle configuration file for this build.
proguard_generate_mapping: boolean. Whether to perform a Proguard optimization step &
generate Proguard mapping corresponding to the obfuscation step.
production_release: boolean. Whether this is a production-facing release build which will
undergo additional renaming and, if configured, signing.
**kwargs: additional arguments. See android_binary for the exact arguments that are
available.
"""
binary_name = "%s_binary" % name
binary_file_name = "%s.apk" % binary_name
proguard_map_file_name = ":%s_proguard.map" % binary_name
main_module_name = "%s_main_module" % name
main_module_file_name = "%s.aab" % main_module_name
corrected_structure_app_module_name = "%s_corrected_structure_app_module" % name
corrected_structure_app_module_file_name = "%s.zip" % corrected_structure_app_module_name
language_restricted_module_name = "%s_language_restricted_module" % name
language_restricted_module_file_name = "%s.zip" % language_restricted_module_name
deployable_name = "%s_deployable" % name
deployable_file_name = "%s.aab" % deployable_name
deployable_with_symbols_aab_name = "%s_deployable_with_symbols" % name
deployable_with_symbols_aab_file_name = "%s.aab" % deployable_with_symbols_aab_name
native.android_binary(
name = binary_name,
tags = ["manual"],
proguard_generate_mapping = proguard_generate_mapping,
**kwargs
)
_convert_apk_to_module_aab(
name = main_module_name,
input_file = binary_file_name,
output_file = main_module_file_name,
tags = ["manual"],
)
_convert_module_aab_to_structured_zip(
name = corrected_structure_app_module_name,
input_file = main_module_file_name,
output_file = corrected_structure_app_module_file_name,
tags = ["manual"],
)
_restrict_languages_in_raw_module_zip(
name = language_restricted_module_name,
input_file = corrected_structure_app_module_file_name,
output_file = language_restricted_module_file_name,
tags = ["manual"],
)
_bundle_module_zip_into_deployable_aab(
name = deployable_name,
input_file = language_restricted_module_file_name,
config_file = config_file,
output_file = deployable_file_name,
tags = ["manual"],
)
if proguard_generate_mapping:
_package_metadata_into_deployable_aab(
name = deployable_with_symbols_aab_name,
input_aab_file = deployable_file_name,
proguard_map_file = proguard_map_file_name,
output_aab_file = deployable_with_symbols_aab_file_name,
tags = ["manual"],
)
deployable_and_maybe_symbols_added_aab_file_name = deployable_with_symbols_aab_file_name
else:
deployable_and_maybe_symbols_added_aab_file_name = deployable_file_name
if production_release:
_sign_and_rename_aab(
name = name,
input_aab = ":%s" % deployable_and_maybe_symbols_added_aab_file_name,
keystore = "//config:keystore_file",
keystore_password_file = "//config:keystore_password_file",
key_alias = "//config:key_alias",
tags = ["manual"],
)
else:
# Copy over the file to its expected location.
copy_file(
name = name,
src = deployable_and_maybe_symbols_added_aab_file_name,
out = "%s.aab" % name,
)
def generate_universal_apk(name, aab_target):
"""
Creates a new 'bazel mobile-install'-able universal APK target for the provided AAB target.
Example usage in a top-level BUILD.bazel file and CLI:
generate_universal_apk(
name = "oppia_prod_universal_apk",
aab_target = "//:oppia_prod",
)
$ bazel mobile-install //:oppia_prod_universal_apk
Note that, sometimes, you may not want to use mobile-install such as for production builds that
may have functional disparity from incremental installations of the app. In those cases, it's
best to uninstall the app from the target device and install the APK directly using
'adb install' as so (per the above example):
$ adb install bazel-bin/oppia_prod_universal_apk.apk
Args:
name: str. The name of the runnable target to install an AAB file on a local device.
aab_target: target. The target (declared via oppia_android_application) that should be made
installable.
"""
_generate_universal_apk(
name = name,
input_aab_file = aab_target,
output_apk_file = "%s.apk" % name,
debug_keystore = "@bazel_tools//tools/android:debug_keystore",
tags = ["manual"],
)