-
Notifications
You must be signed in to change notification settings - Fork 535
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix #5660: Ensure mobile-install works #5664
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -158,61 +158,57 @@ def _package_metadata_into_deployable_aab_impl(ctx): | |||||
runfiles = ctx.runfiles(files = [output_aab_file]), | ||||||
) | ||||||
|
||||||
def _generate_apks_and_install_impl(ctx): | ||||||
input_file = ctx.attr.input_file.files.to_list()[0] | ||||||
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) | ||||||
deploy_shell = ctx.actions.declare_file("%s_run.sh" % ctx.label.name) | ||||||
|
||||||
# Reference: https://developer.android.com/studio/command-line/bundletool#generate_apks. | ||||||
# 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_apks_arguments = [ | ||||||
generate_universal_apk_arguments = [ | ||||||
"build-apks", | ||||||
"--bundle=%s" % input_file.path, | ||||||
"--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 generats 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_file + ctx.files.debug_keystore, | ||||||
inputs = ctx.files.input_aab_file + ctx.files.debug_keystore, | ||||||
tools = [ctx.executable._bundletool_tool], | ||||||
executable = ctx.executable._bundletool_tool.path, | ||||||
arguments = generate_apks_arguments, | ||||||
mnemonic = "BuildApksFromDeployAab", | ||||||
progress_message = "Preparing AAB deploy to device", | ||||||
arguments = generate_universal_apk_arguments, | ||||||
mnemonic = "GenerateUniversalAPk", | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
progress_message = "Generating universal APK from AAB", | ||||||
) | ||||||
|
||||||
# References: https://github.com/bazelbuild/bazel/issues/7390, | ||||||
# https://developer.android.com/studio/command-line/bundletool#deploy_with_bundletool, and | ||||||
# https://docs.bazel.build/versions/main/skylark/rules.html#executable-rules-and-test-rules. | ||||||
# Note that the bundletool can be executed directly since Bazel creates a wrapper script that | ||||||
# utilizes its own internal Java toolchain. | ||||||
ctx.actions.write( | ||||||
output = deploy_shell, | ||||||
content = """ | ||||||
#!/bin/sh | ||||||
{0} install-apks --apks={1} | ||||||
echo The APK should now be installed | ||||||
""".format(ctx.executable._bundletool_tool.short_path, apks_file.short_path), | ||||||
is_executable = True, | ||||||
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", | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
progress_message = "Extracting universal APK from .apks file", | ||||||
) | ||||||
|
||||||
# Reference for including necessary runfiles for Java: | ||||||
# https://github.com/bazelbuild/bazel/issues/487#issuecomment-178119424. | ||||||
runfiles = ctx.runfiles( | ||||||
files = [ | ||||||
ctx.executable._bundletool_tool, | ||||||
apks_file, | ||||||
], | ||||||
).merge(ctx.attr._bundletool_tool.default_runfiles) | ||||||
return DefaultInfo( | ||||||
executable = deploy_shell, | ||||||
runfiles = runfiles, | ||||||
files = depset([output_apk_file]), | ||||||
runfiles = ctx.runfiles(files = [output_apk_file]), | ||||||
) | ||||||
|
||||||
_convert_apk_to_module_aab = rule( | ||||||
|
@@ -303,10 +299,13 @@ _package_metadata_into_deployable_aab = rule( | |||||
implementation = _package_metadata_into_deployable_aab_impl, | ||||||
) | ||||||
|
||||||
_generate_apks_and_install = rule( | ||||||
_generate_universal_apk = rule( | ||||||
attrs = { | ||||||
"input_file": attr.label( | ||||||
allow_single_file = True, | ||||||
"input_aab_file": attr.label( | ||||||
allow_single_file = [".aab"], | ||||||
mandatory = True, | ||||||
), | ||||||
"output_apk_file": attr.output( | ||||||
mandatory = True, | ||||||
), | ||||||
"debug_keystore": attr.label( | ||||||
|
@@ -319,14 +318,19 @@ _generate_apks_and_install = rule( | |||||
default = "//third_party:android_bundletool_binary", | ||||||
), | ||||||
}, | ||||||
executable = True, | ||||||
implementation = _generate_apks_and_install_impl, | ||||||
implementation = _generate_universal_apk_impl, | ||||||
) | ||||||
|
||||||
def oppia_android_application(name, config_file, proguard_generate_mapping, **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. | ||||||
|
@@ -390,30 +394,30 @@ def oppia_android_application(name, config_file, proguard_generate_mapping, **kw | |||||
tags = ["manual"], | ||||||
) | ||||||
|
||||||
def declare_deployable_application(name, aab_target): | ||||||
def generate_universal_apk(name, aab_target): | ||||||
""" | ||||||
Creates a new target that can be run with 'bazel run' to install an AAB file. | ||||||
Creates a new installable universal APK target for the provided AAB target. | ||||||
This new target is installable via 'adb install'. This should generally never be used for direct | ||||||
development installs as it will be far less performant than 'bazel mobile-install'. | ||||||
Comment on lines
+401
to
+402
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I find this ambiguous, it is not clear to me whether the warning is against using 'adb install' or |
||||||
Example: | ||||||
declare_deployable_application( | ||||||
name = "install_oppia_prod", | ||||||
generate_universal_apk( | ||||||
name = "oppia_prod_universal_apk", | ||||||
aab_target = "//:oppia_prod", | ||||||
) | ||||||
$ bazel run //:install_oppia_prod | ||||||
This will build (if necessary) and install the correct APK derived from the Android app bundle | ||||||
on the locally attached emulator or device. Note that this command does not support targeting a | ||||||
specific device so it will not work if more than one device is available via 'adb devices'. | ||||||
$ bazel mobile-install //:oppia_prod_universal_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_apks_and_install( | ||||||
_generate_universal_apk( | ||||||
name = name, | ||||||
input_file = aab_target, | ||||||
input_aab_file = aab_target, | ||||||
output_apk_file = "%s.apk" % name, | ||||||
debug_keystore = "@bazel_tools//tools/android:debug_keystore", | ||||||
tags = ["manual"], | ||||||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: