• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1"""Generate zipped aar file including different variants of .so in jni folder."""
2
3load("@build_bazel_rules_android//android:rules.bzl", "android_binary")
4
5def aar_with_jni(
6        name,
7        android_library,
8        headers = None,
9        flatten_headers = False):
10    """Generates an Android AAR given an Android library target.
11
12    Args:
13      name: Name of the generated .aar file.
14      android_library: The `android_library` target to package. Note that the
15          AAR will contain *only that library's .jar` sources. It does not
16          package the transitive closure of all Java source dependencies.
17      headers: Optional list of headers that will be included in the
18          generated .aar file. This is useful for distributing self-contained
19          .aars with native libs that can be used directly by native clients.
20      flatten_headers: Whether to flatten the output paths of included headers.
21    """
22
23    # Generate dummy AndroidManifest.xml for dummy apk usage
24    # (dummy apk is generated by <name>_dummy_app_for_so target below)
25    native.genrule(
26        name = name + "_binary_manifest_generator",
27        outs = [name + "_generated_AndroidManifest.xml"],
28        cmd = """
29cat > $(OUTS) <<EOF
30<manifest
31  xmlns:android="http://schemas.android.com/apk/res/android"
32  package="dummy.package.for.so">
33  <uses-sdk android:minSdkVersion="999"/>
34</manifest>
35EOF
36""",
37    )
38
39    # Generate dummy apk including .so files and later we extract out
40    # .so files and throw away the apk.
41    android_binary(
42        name = name + "_dummy_app_for_so",
43        manifest = name + "_generated_AndroidManifest.xml",
44        custom_package = "dummy.package.for.so",
45        deps = [android_library],
46        # In some platforms we don't have an Android SDK/NDK and this target
47        # can't be built. We need to prevent the build system from trying to
48        # use the target in that case.
49        tags = [
50            "manual",
51            "no_cuda_on_cpu_tap",
52        ],
53    )
54
55    srcs = [android_library + ".aar", name + "_dummy_app_for_so_unsigned.apk"]
56
57    cmd = """
58cp $(location {0}.aar) $(location :{1}.aar)
59chmod +w $(location :{1}.aar)
60origdir=$$PWD
61cd $$(mktemp -d)
62unzip $$origdir/$(location :{1}_dummy_app_for_so_unsigned.apk) "lib/*"
63cp -r lib jni
64zip -r $$origdir/$(location :{1}.aar) jni/*/*.so
65""".format(android_library, name)
66
67    if headers:
68        srcs += headers
69        cmd += """
70        mkdir headers
71        """
72        for src in headers:
73            if flatten_headers:
74                cmd += """
75                    cp -RL $$origdir/$(location {0}) headers/$$(basename $(location {0}))
76                """.format(src)
77            else:
78                cmd += """
79                    mkdir -p headers/$$(dirname $(location {0}))
80                    cp -RL $$origdir/$(location {0}) headers/$(location {0})
81                """.format(src)
82        cmd += "zip -r $$origdir/$(location :{0}.aar) headers".format(name)
83
84    native.genrule(
85        name = name,
86        srcs = srcs,
87        outs = [name + ".aar"],
88        tags = ["manual"],
89        cmd = cmd,
90    )
91