Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot build android jni example #4407

Closed
balazsbanyai opened this issue Jan 9, 2018 · 7 comments
Closed

Cannot build android jni example #4407

balazsbanyai opened this issue Jan 9, 2018 · 7 comments

Comments

@balazsbanyai
Copy link

balazsbanyai commented Jan 9, 2018

Cannot build native sources that use jni

Reproduce this bug by checking out the master branch, creating a workspace file, and running:
bazel build //examples/android/java/bazel:jni

root@8f027c140453:/src# bazel build //examples/android/java/bazel:jni
INFO: Analysed target //examples/android/java/bazel:jni (0 packages loaded).
INFO: Found 1 target...
ERROR: /src/examples/android/java/bazel/BUILD:33:1: C++ compilation of rule '//examples/android/java/bazel:jni_dep' failed (Exit 1)
In file included from examples/android/java/bazel/jni_dep.cc:1:0:
./examples/android/java/bazel/jni_dep.h:3:17: fatal error: jni.h: No such file or directory
compilation terminated.
Target //examples/android/java/bazel:jni failed to build
Use --verbose_failures to see the command lines of failed build steps.
INFO: Elapsed time: 0.203s, Critical Path: 0.04s
FAILED: Build did NOT complete successfully

My WORKSPACE:

android_sdk_repository(
    name="androidsdk",
    path="/opt/android-sdk-linux",
    api_level=21,
)

android_ndk_repository(
    name="androidndk",
    path="/opt/android-ndk",
    api_level=21,
)

What operating system are you running Bazel on?

  • OSX 10.12.6 as host os -> did not work
  • Docker image: bitriseio/android-ndk -> did not work

What's the output of bazel info release?

OSX: release 0.9.0-homebrew
Docker: release 0.9.0

What's the output of git remote get-url origin ; git rev-parse master ; git rev-parse HEAD ?

git@github.com:bazelbuild/bazel.git
9c21b5cf530b7d2a2070a11465ca22e79d6d8729
9c21b5cf530b7d2a2070a11465ca22e79d6d8729

Have you found anything relevant by searching the web?

I have tried to build my own app using your example, but I couldn't build the example itself.

@balazsbanyai balazsbanyai changed the title Cannot build native sources that use jni Cannot android jni example Jan 9, 2018
@balazsbanyai balazsbanyai changed the title Cannot android jni example Cannot build android jni example Jan 9, 2018
@jin
Copy link
Member

jin commented Jan 9, 2018

What's your NDK version?

@jin
Copy link
Member

jin commented Jan 9, 2018

I can repro the build failure with r14b and r15c, so it doesn't seem like it's related to the NDK version.

@jin
Copy link
Member

jin commented Jan 9, 2018

Related comment: #348 (comment)

@balazsbanyai
Copy link
Author

I tried to build it using NDK r15b

@aj-michael
Copy link
Contributor

I'll take a look at this.

@aj-michael aj-michael self-assigned this Jan 10, 2018
@aj-michael
Copy link
Contributor

This is working as intended. bazel build //examples/android/java/bazel:jni is attempting to build a cc_library. Bazel doesn't know that this is an Android cc_library. With no flags, it assumes that you're building for the machine that you're running Bazel on using the default crosstool, which does not know about jni.h.

So the question is, what are you trying to do? If you really want to just build the cc_library, you will need to tell Bazel that you are building it for Android. The way you do this is with --crosstool_top=//external:android/crosstool. Then you need to tell it what CPU you're targeting, because Android runs on several architectures. So you need something like --cpu=armeabi-v7a. And finally, you need to tell Bazel to use the normal crosstool when building tools that will run as part of the build. Otherwise, it will try and build tools like compilers to run on Android, which will fail. So you will need --host_crosstool_top=@bazel_tools//tools/cpp:toolchain.

$ bazel build --crosstool_top=//external:android/crosstool --cpu=armeabi-v7a --host_crosstool_top=@bazel_tools//tools/cpp:toolchain //examples/android/java/bazel:jni

However, what you probably want to do is build a .apk that you can install on a device/emulator. The instructions in https://github.com/bazelbuild/bazel/tree/master/examples/android have the command to do that, which is bazel build //examples/android/java/bazel:hello_world. You don't need any flags for this, because hello_world is an android_binary and Bazel is smart enough to deduce that if an android_binary depends on a cc_library, the native code should be built using the Android crosstool.

@balazsbanyai
Copy link
Author

I was trying to build an aar android library as the main artifact. I've found my answer in the referenced thread! Thank you very much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants