Skip to content

Commit c7b0a39

Browse files
authored
Merge pull request #154 from bandlab/optional-android-sdk-ndk
Allow code generation without android SDK/NDK
2 parents 6aa86dc + f6a5726 commit c7b0a39

File tree

3 files changed

+64
-3
lines changed

3 files changed

+64
-3
lines changed

WORKSPACE

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ djinni_setup_deps()
99

1010
# --- Everything below is only used for examples and tests
1111

12-
# android_sdk_repository fails to find build_tools if we don't explicitly set a version.
13-
android_sdk_repository(name = "androidsdk")
14-
android_ndk_repository(name = "androidndk", api_level = 21)
12+
load("//bzl:android_configure.bzl", "android_configure")
13+
android_configure(name = "local_config_android")
14+
load("@local_config_android//:android_configure.bzl", "android_workspace")
15+
android_workspace()
1516

1617
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
1718

bzl/android_configure.bzl

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
"""Repository rule for Android SDK and NDK autoconfiguration.
2+
3+
This rule is a no-op unless the required android environment variables are set.
4+
"""
5+
6+
# Based on: https://github.com/envoyproxy/envoy-mobile/pull/2039/
7+
# And: https://github.com/tensorflow/tensorflow/tree/34c03ed67692eb76cb3399cebca50ea8bcde064c/third_party/android
8+
# Workaround for https://github.com/bazelbuild/bazel/issues/14260
9+
10+
_ANDROID_NDK_HOME = "ANDROID_NDK_HOME"
11+
_ANDROID_SDK_HOME = "ANDROID_HOME"
12+
13+
def _android_autoconf_impl(repository_ctx):
14+
sdk_home = repository_ctx.os.environ.get(_ANDROID_SDK_HOME)
15+
ndk_home = repository_ctx.os.environ.get(_ANDROID_NDK_HOME)
16+
17+
if sdk_home == "" or ndk_home == "":
18+
print("ANDROID_HOME or ANDROID_NDK_HOME not set. Building android examples will fail.")
19+
20+
sdk_rule = ""
21+
if sdk_home:
22+
sdk_rule = """
23+
native.android_sdk_repository(
24+
name="androidsdk",
25+
path="{}",
26+
api_level=30,
27+
build_tools_version="30.0.2",
28+
)
29+
""".format(sdk_home)
30+
31+
ndk_rule = ""
32+
if ndk_home:
33+
ndk_rule = """
34+
native.android_ndk_repository(
35+
name="androidndk",
36+
path="{}",
37+
api_level=21,
38+
)
39+
""".format(ndk_home)
40+
41+
if ndk_rule == "" and sdk_rule == "":
42+
sdk_rule = "pass"
43+
44+
repository_ctx.file("BUILD.bazel", "")
45+
repository_ctx.file("android_configure.bzl", """
46+
47+
def android_workspace():
48+
{}
49+
{}
50+
""".format(sdk_rule, ndk_rule))
51+
52+
android_configure = repository_rule(
53+
implementation = _android_autoconf_impl,
54+
environ = [
55+
_ANDROID_NDK_HOME,
56+
_ANDROID_SDK_HOME,
57+
],
58+
)

examples/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
## Building the Android example app
22

3+
First make sure that the ANDROID_HOME and ANDROID_NDK_HOME environment variables are set and pointing to working installations of the Android SDK and NDK, respectively.
4+
35
Build with bazel: `bazel build //examples:android-app`.
46

57
Build and deploy to device: `bazel mobile-install //examples:android-app`.

0 commit comments

Comments
 (0)