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 with repo root license 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 multidex = "native", 47 # In some platforms we don't have an Android SDK/NDK and this target 48 # can't be built. We need to prevent the build system from trying to 49 # use the target in that case. 50 tags = [ 51 "manual", 52 "no_cuda_on_cpu_tap", 53 ], 54 ) 55 56 srcs = [ 57 android_library + ".aar", 58 name + "_dummy_app_for_so_unsigned.apk", 59 "//:LICENSE", 60 ] 61 62 cmd = """ 63cp $(location {0}.aar) $(location :{1}.aar) 64chmod +w $(location :{1}.aar) 65origdir=$$PWD 66cd $$(mktemp -d) 67unzip $$origdir/$(location :{1}_dummy_app_for_so_unsigned.apk) "lib/*" 68cp -r lib jni 69zip -r $$origdir/$(location :{1}.aar) jni/*/*.so 70cp $$origdir/$(location //:LICENSE) ./ 71zip $$origdir/$(location :{1}.aar) LICENSE 72""".format(android_library, name) 73 74 if headers: 75 srcs += headers 76 cmd += """ 77 mkdir headers 78 """ 79 for src in headers: 80 if flatten_headers: 81 cmd += """ 82 cp -RL $$origdir/$(location {0}) headers/$$(basename $(location {0})) 83 """.format(src) 84 else: 85 cmd += """ 86 mkdir -p headers/$$(dirname $(location {0})) 87 cp -RL $$origdir/$(location {0}) headers/$(location {0}) 88 """.format(src) 89 cmd += "zip -r $$origdir/$(location :{0}.aar) headers".format(name) 90 91 native.genrule( 92 name = name, 93 srcs = srcs, 94 outs = [name + ".aar"], 95 # In some platforms we don't have an Android SDK/NDK and this target 96 # can't be built. We need to prevent the build system from trying to 97 # use the target in that case. 98 tags = ["manual"], 99 cmd = cmd, 100 ) 101 102def aar_without_jni( 103 name, 104 android_library): 105 """Generates an Android AAR with repo root license given a pure Java Android library target. 106 107 Args: 108 name: Name of the generated .aar file. 109 android_library: The `android_library` target to package. Note that the 110 AAR will contain *only that library's .jar` sources. It does not 111 package the transitive closure of all Java source dependencies. 112 """ 113 114 srcs = [ 115 android_library + ".aar", 116 "//:LICENSE", 117 ] 118 119 cmd = """ 120cp $(location {0}.aar) $(location :{1}.aar) 121chmod +w $(location :{1}.aar) 122origdir=$$PWD 123cd $$(mktemp -d) 124cp $$origdir/$(location //:LICENSE) ./ 125zip $$origdir/$(location :{1}.aar) LICENSE 126""".format(android_library, name) 127 128 native.genrule( 129 name = name, 130 srcs = srcs, 131 outs = [name + ".aar"], 132 cmd = cmd, 133 ) 134