Skip to content

Commit

Permalink
Merge #324 by Bencodes: Pass build_target to singlejar when extractin…
Browse files Browse the repository at this point in the history
…g aar contents

Passes the `build_target` argument to singlejar so that the extracted/merged jars can be associated with the original aar that created them.
Closes #324

COPYBARA_INTEGRATE_REVIEW=#324 from Bencodes:pass-build_target-to-singlejar-when-extracting-aar-contents 33189e5
PiperOrigin-RevId: 713069367
Change-Id: I2c9ac2321b9260162abe5074c85d208e923ed039
  • Loading branch information
Bencodes authored and copybara-github committed Jan 7, 2025
1 parent 4d8487c commit 83fd430
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
1 change: 1 addition & 0 deletions rules/aar_import/impl.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ def _extract_jars(
args = ctx.actions.args()
args.add("--input_aar", aar)
args.add("--output_dir", out_jars_tree_artifact.path)
args.add("--build_target", ctx.label)
args.add("--output_singlejar_param_file", out_jars_params_file)
ctx.actions.run(
executable = aar_embedded_jars_extractor_tool,
Expand Down
13 changes: 10 additions & 3 deletions tools/android/aar_embedded_jars_extractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
flags.mark_flag_as_required("input_aar")
flags.DEFINE_string("output_singlejar_param_file", None,
"Output parameter file for singlejar")
flags.DEFINE_string("build_target", None,
"Build target label to pass to singlejar")
flags.mark_flag_as_required("output_singlejar_param_file")
flags.DEFINE_string("output_dir", None, "Output directory to extract jars in")
flags.mark_flag_as_required("output_dir")
Expand All @@ -43,6 +45,7 @@
def ExtractEmbeddedJars(aar,
singlejar_param_file,
output_dir,
build_target=None,
output_dir_orig=None):
"""Extracts all embedded jars from an AAR.
Expand All @@ -56,6 +59,9 @@ def ExtractEmbeddedJars(aar,
output_dir_orig = output_dir
jar_pattern = re.compile("^(classes|libs/.+)\\.jar$")
singlejar_param_file.write(b"--exclude_build_data\n")
if build_target:
singlejar_param_file.write(b"--build_target\n")
singlejar_param_file.write(f"{build_target}\n".encode("utf-8"))
for name in aar.namelist():
if jar_pattern.match(name):
singlejar_param_file.write(b"--sources\n")
Expand All @@ -69,12 +75,13 @@ def ExtractEmbeddedJars(aar,
def _Main(input_aar,
output_singlejar_param_file,
output_dir,
build_target=None,
output_dir_orig=None):
if not output_dir_orig:
output_dir_orig = output_dir
with zipfile.ZipFile(input_aar, "r") as aar:
with open(output_singlejar_param_file, "wb") as singlejar_param_file:
ExtractEmbeddedJars(aar, singlejar_param_file, output_dir,
ExtractEmbeddedJars(aar, singlejar_param_file, output_dir, build_target,
output_dir_orig)


Expand All @@ -93,9 +100,9 @@ def main(unused_argv):
os.path.join(aar_junc, os.path.basename(aar_long)),
os.path.join(params_junc, os.path.basename(params_long)),
os.path.join(out_junc, os.path.basename(out_long)),
FLAGS.output_dir)
FLAGS.output_dir, FLAGS.build_target)
else:
_Main(FLAGS.input_aar, FLAGS.output_singlejar_param_file, FLAGS.output_dir)
_Main(FLAGS.input_aar, FLAGS.output_singlejar_param_file, FLAGS.output_dir, FLAGS.build_target)


if __name__ == "__main__":
Expand Down
8 changes: 6 additions & 2 deletions tools/android/aar_embedded_jars_extractor_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,14 @@ def testClassesJarAndLibsJars(self):
aar.writestr("libs/b.jar", "")
param_file = io.BytesIO()
os.makedirs("out_dir")
aar_embedded_jars_extractor.ExtractEmbeddedJars(aar, param_file, "out_dir")
aar_embedded_jars_extractor.ExtractEmbeddedJars(aar, param_file, "out_dir", "//foo:bar")
self.assertCountEqual(["classes.jar", "libs"], os.listdir("out_dir"))
self.assertCountEqual(["a.jar", "b.jar"], os.listdir("out_dir/libs"))
param_file.seek(0)
self.assertEqual(
[b"--exclude_build_data\n",
b"--build_target\n",
b"//foo:bar\n",
b"--sources\n",
b"out_dir/classes.jar\n",
b"--sources\n",
Expand All @@ -73,11 +75,13 @@ def testOnlyClassesJar(self):
aar.writestr("classes.jar", "")
param_file = io.BytesIO()
os.makedirs("out_dir")
aar_embedded_jars_extractor.ExtractEmbeddedJars(aar, param_file, "out_dir")
aar_embedded_jars_extractor.ExtractEmbeddedJars(aar, param_file, "out_dir", "//foo:bar")
self.assertEqual(["classes.jar"], os.listdir("out_dir"))
param_file.seek(0)
self.assertEqual(
[b"--exclude_build_data\n",
b"--build_target\n",
b"//foo:bar\n",
b"--sources\n",
b"out_dir/classes.jar\n"],
param_file.readlines())
Expand Down

0 comments on commit 83fd430

Please sign in to comment.