Created
April 13, 2022 22:24
-
-
Save jlaxson/deeaf2656935b37670ede38629a7a069 to your computer and use it in GitHub Desktop.
Bazel Transitive AAR Rule
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def _format_zip_path(f): | |
return "libs/{}={}".format(f.basename, f.path) | |
def _format_native_lib(item): | |
arch, file = item | |
return "jni/{}/{}={}".format(arch, file.basename, file.path) | |
def _android_aar_impl(ctx): | |
print([ d[JavaInfo].java_outputs[0].class_jar for d in ctx.attr.deps if JavaInfo in d]) | |
runtime_deps = depset(transitive=[ d[JavaInfo].transitive_runtime_jars for d in ctx.attr.deps if JavaInfo in d]) | |
output_aar = ctx.actions.declare_file(ctx.attr.name + ".aar") | |
output_manifest = ctx.actions.declare_file("AndroidManifest.xml") | |
ctx.actions.write(output_manifest, content="""<?xml version="1.0" encoding="utf-8"?> | |
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | |
package="native.libs"> | |
<application> | |
</application> | |
</manifest> | |
""") | |
args = ctx.actions.args() | |
args.add("cC") | |
args.add(output_aar.path) | |
args.add_all(runtime_deps, map_each=_format_zip_path) | |
args.add("AndroidManifest.xml=" + output_manifest.path) | |
transitive_deps = [runtime_deps] | |
if ctx.attr.binary: | |
for arch, libs in ctx.attr.binary.android.native_libs.items(): | |
transitive_deps.append(libs) | |
args.add_all([(arch, lib) for lib in libs.to_list()], map_each=_format_native_lib) | |
ctx.actions.run( | |
inputs=depset([output_manifest], transitive=transitive_deps), | |
outputs=[output_aar], | |
arguments=[args], | |
executable = ctx.executable._zipper | |
) | |
return DefaultInfo(files=depset([output_aar])) | |
_package_aar = rule( | |
implementation = _android_aar_impl, | |
attrs = { | |
"deps": attr.label_list(allow_files = True), | |
"binary": attr.label(allow_files = True), | |
"_zipper": attr.label(executable = True, default = "@bazel_tools//tools/zip:zipper", cfg = "exec"), | |
}, | |
) | |
def android_aar(name, deps): | |
android_binary( | |
name = name + "_native", | |
custom_package = "nativedeps", | |
tags = ["manual"], | |
deps = deps | |
) | |
_package_aar( | |
name = name, | |
binary = name + "_native", | |
deps = deps | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment