tensorflow: Unable to build Android Example App using Bazel on Windows

What related GitHub issues or StackOverflow threads have you found by searching the web for your problem?

https://github.com/tensorflow/tensorflow/issues/6101 https://github.com/tensorflow/tensorflow/issues/6383 http://stackoverflow.com/questions/40978859/creating-simple-android-app-using-android-studio-and-tensorflow

Environment info

Operating System: Windows

Installed version of CUDA and cuDNN: (please attach the output of ls -l /path/to/cuda/lib/libcud*): Cuda 8.0 cuDNN 5.0

If installed from source, provide

  1. The commit hash (git rev-parse HEAD) I downloaded the latest ZIP on 18 Dec 2016. Sorry, I have no idea how to get the version.

  2. The output of bazel version Build label: 0.4.2 Build target: bazel-out/local-fastbuild/bin/src/main/java/com/google/devtools/build/lib/bazel/BazelServer_deploy.jar Build time: Wed Dec 7 18:47:13 2016 (1481136433) Build timestamp: 1481136433 Build timestamp as int: 1481136433

My goal is to build an Android App which loads pre-trained TensorFlow model and runs it on an Android device. I am working on Android Studio on Windows. Unfortunately using Android Studio on Linux is currently not an option.

Following previous advice from this forum, I am trying to build an Android example using Bazel on Windows. I’ve successfully installed Bazel using Chocolatey. Following the build instructions, I changed the SDK and NDK paths in WORKSPACE file and ran: bazel build //tensorflow/examples/android:tensorflow_demo

So far I did the following to fix errors:

  1. copied aapt.exe to aapt, zipalign.exe to zipalign, since their name is different on Windows/Linux
  2. installed using pacman gcc

But I am stuck again. I am getting the following error which I have no idea how to fix:

ERROR: C:/Users/Andrey/AppData/Local/Temp/_bazel_Andrey/gd3-gSwg/external/protobuf/BUILD:73:1: C++ compilation of rule '@protobuf//:protobuf_lite' failed: arm-linux-androideabi-gcc failed: error executing command external/androidndk/ndk/toolchains/arm-linux-androideabi-4.9/prebuilt/windows-x86_64/bin/arm-linux-androideabi-gcc -fstack-protector-strong -fpic -ffunction-sections -funwind-tables ... (remaining 44 argument(s) skipped): com.google.devtools.build.lib.shell.BadExitStatusException: Process exited with status 1.
external/protobuf/src/google/protobuf/stubs/structurally_valid.cc:588:1: fatal error: opening dependency file bazel-out/android-arm-linux-androideabi-4.9-v7a-gnu-libstdcpp-fastbuild/bin/external/protobuf/_objs/protobuf_lite/external/protobuf/src/google/protobuf/stubs/structurally_valid.d: No such file or directory
 }  // namespace google
 ^
compilation terminated.
ERROR: C:/Tools/tensorflow-master/tensorflow/examples/android/BUILD:58:1: output 'tensorflow/examples/android/tensorflow_demo_symbols/R.txt' was not created.
ERROR: C:/Tools/tensorflow-master/tensorflow/examples/android/BUILD:58:1: output 'tensorflow/examples/android/tensorflow_demo.srcjar' was not created.
ERROR: C:/Tools/tensorflow-master/tensorflow/examples/android/BUILD:58:1: output 'tensorflow/examples/android/proguard/tensorflow_demo/_tensorflow_demo_proguard.cfg' was not created.
ERROR: C:/Tools/tensorflow-master/tensorflow/examples/android/BUILD:58:1: output 'tensorflow/examples/android/tensorflow_demo_processed_manifest/AndroidManifest.xml' was not created.
ERROR: C:/Tools/tensorflow-master/tensorflow/examples/android/BUILD:58:1: output 'tensorflow/examples/android/tensorflow_demo_files/resource_files.zip' was not created.
ERROR: C:/Tools/tensorflow-master/tensorflow/examples/android/BUILD:58:1: output 'tensorflow/examples/android/tensorflow_demo.ap_' was not created.
ERROR: C:/Users/Andrey/AppData/Local/Temp/_bazel_Andrey/gd3-gSwg/external/protobuf/BUILD:73:1: output 'external/protobuf/_objs/protobuf_lite/external/protobuf/src/google/protobuf/stubs/stringprintf.o' was not created.
ERROR: C:/Users/Andrey/AppData/Local/Temp/_bazel_Andrey/gd3-gSwg/external/protobuf/BUILD:73:1: output 'external/protobuf/_objs/protobuf_lite/external/protobuf/src/google/protobuf/arenastring.o' was not created.
ERROR: C:/Users/Andrey/AppData/Local/Temp/_bazel_Andrey/gd3-gSwg/external/protobuf/BUILD:113:1: output 'external/protobuf/_objs/protobuf/external/protobuf/src/google/protobuf/io/strtod.o' was not created.
ERROR: C:/Users/Andrey/AppData/Local/Temp/_bazel_Andrey/gd3-gSwg/external/protobuf/BUILD:73:1: output 'external/protobuf/_objs/protobuf_lite/external/protobuf/src/google/protobuf/repeated_field.o' was not created.
Target //tensorflow/examples/android:tensorflow_demo failed to build
Use --verbose_failures to see the command lines of failed build steps.
INFO: Elapsed time: 4.722s, Critical Path: 1.16s

  1. Please advice how to proceed?
  2. Isn’t there an easier way to achieve what I want? I just need pre-built TensorFlow binaries for Android Studio on Windows, to build an example application. So far I found only pre-built Python distribution and Bazel installation on Windows

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Reactions: 1
  • Comments: 51 (25 by maintainers)

Commits related to this issue

Most upvoted comments

@AndreyRub Please see http://ci.tensorflow.org/view/Nightly/job/nightly-android/ for a full selection of prebuilt Android TF libs, which has just come online.

You can either download libtensorflow_inference.so for something that can be dropped into a standalone app, or grab libtensorflow_demo.so if you’re trying to get the TF Android demo to run (contains libtensorflow_inference.so + demo-specific native code).

In either case you will need libandroid_tensorflow_inference_java.jar as well for the Java counterpart.

To update on this, a prebuilt TensorFlow AAR is now provided at https://ci.tensorflow.org/view/Nightly/job/nightly-android/lastSuccessfulBuild/artifact/out/tensorflow.aar

This contains tensorflow_inference_java.jar and tensorflow_inference.so native libs for armeabi-v7a, arm64-v8a, x86, and x86_64.

This means to integrate TF into your app built with Gradle, you can simply download the file to e.g. “aarDir/”, and then add the following into your gradle build file:

allprojects {
    repositories {
        jcenter()
        flatDir {
            dirs 'aarDir'
        }
    }
}

dependencies {
    compile(name:'tensorflow', ext:'aar')
}

This will provide TensorFlowInferenceInterface as well as the full TF Java API. We’ll be putting this up on jcenter soon so no manual downloading will be necessary, just a couple of Gradle lines.

Note that for the official Android demo in particular, you’ll still need to download/build libtensorflow_demo.so as well (which provides image conversion/object tracking support). It also needs to be built for the highest archictecture your device uses, since tensorflow.aar now provides more than armeabi-v7a and all native libs must be present at e.g. arm64 if your device uses arm64. It’s a straightforward library, so we’ll be adding a cmake build for libtensorflow_demo.so specifically so it can be built on Windows directly.

You’ll also want to remove references to libtensorflow_inference.so (but not libtensorflow_demo.so), tensorflow/java, and tensorflow/contrib/android from build.gradle since these are now provided by the AAR.

Coupled with the AAR download it will then be possible to build the entire demo on Windows without much, if any, manual tweaking; we should be able to auto-detect Windows and choose the appropriate build process.

@Anmolk22 I’ll try to help you. Bazel does not work properly on Windows, so for building project you should use prebuilded libs.

  1. (Open this link and download last stable build archive http://ci.tensorflow.org/view/Nightly/job/nightly-android/) It should contains following files image
  2. Remove buildNative and copyNativeLibs tasks from build.gradle file.
  3. Copy libs from archive to libs derectory of android project.

Ok, after trying to build the Bazel example Android project on Linux, and reverse engineering the .apk package, it looks like it depends only on a single .so native TensorFlow Android library, called libtensorflow_demo.so.

Currently in order to build this simple example on Windows, I needed to:

  1. Install chocolatey
  2. Install bazel
  3. Install pacman
  4. Download gcc using pacman
  5. Change CONFIGURATION file and find my SDK and NDK
  6. Change some sdk .exe files to no extenstion, to fit Linux
  7. And it still does not work 👎 … That is a very hard and tedious process

I think that it could really help if there was an example Android Studio project for windows which does not use Bazel at all. Instead let’s put the mentioned above pre-compiled TensorFlow library available for download (for all possible Android architectures - arm64-v8a, armeabi, armeabi-v7a ,x86_64) ,together with a simple tutorial.

Obviously, it will not be as flexible as the ability to re-build TensorFlow, but for 99% of users it will suffice to update the pre-built libraries once in a while.

So still no Windows support after a year when this issue was first brought up? Just tried to build the demo.

org.gradle.api.tasks.TaskExecutionException: Execution failed for task ‘:buildNativeBazel’. at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(ExecuteActionsTaskExecuter.java:100) at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.execute(ExecuteActionsTaskExecuter.java:70) at org.gradle.api.internal.tasks.execution.OutputDirectoryCreatingTaskExecuter.execute(OutputDirectoryCreatingTaskExecuter.java:51) at … Caused by: java.io.IOException: Cannot run program “/usr/local/bin/bazel” (in directory “C:\GitProjects\tensorflow”): CreateProcess error=2, The system cannot find the file specified at java.lang.ProcessBuilder.start(ProcessBuilder.java:1048) at net.rubygrapefruit.platform.internal.DefaultProcessLauncher.start(DefaultProcessLauncher.java:25) … 10 more Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified at java.lang.ProcessImpl.create(Native Method) at java.lang.ProcessImpl.<init>(ProcessImpl.java:386) at java.lang.ProcessImpl.start(ProcessImpl.java:137) at java.lang.ProcessBuilder.start(ProcessBuilder.java:1029) … 11 more

As a note on performance, I’ve recently updated the TensorFlow for Poets script with the option to save out MobileNet models, which can be a lot smaller and faster than the default Inception v3, at the cost of some accuracy: https://github.com/tensorflow/tensorflow/blob/master/tensorflow/docs_src/tutorials/image_retraining.md#other-model-architectures

It looks like the original issue is fixed, or at least obsolete now though? I’m closing this for the moment, please reopen with more information if that’s incorrect.

Ok, I’ve got something sort of working. It needs some more tweaking, and I would recommend sticking with the prebuilts as described by @bulatyauheni above, but if you’re feeling adventurous here’s how you can build the Android TF libraries on Windows 10.

First, install Bash for Windows as described here: https://msdn.microsoft.com/en-us/commandline/wsl/about

Once that’s done, start bash and run the following, adjusting directories appropriately:

# Install necessary components
sudo apt-get install make unzip autoconf ccache autogen libtool g++ zlibc-dev

# Download and install the NDK (has to be the Linux NDK)
wget https://dl.google.com/android/repository/android-ndk-r12b-linux-x86_64.zip
mkdir -p ~/android
unzip android-ndk-r12b-linux-x86_64.zip -d ~/android

# Build once to confirm it works and generate proto libs (which will be reused).
export NDK_ROOT=~/android/android-ndk-r12b
export CC_PREFIX=ccache
tensorflow/contrib/makefile/build_all_android.sh \
-s tensorflow/contrib/makefile/sub_makefiles/android/Makefile.in \
-t "libtensorflow_inference.so libtensorflow_demo.so"

Now create a file named “makefile_helper.bat” in tensorflow/ and add the following to it:

echo "BUILDING!"
bash -c 'NDK_ROOT=~/android/android-ndk-r12b CC_PREFIX=ccache tensorflow/contrib/makefile/build_all_android.sh -T -s tensorflow/contrib/makefile/sub_makefiles/android/Makefile.in -t "libtensorflow_inference.so libtensorflow_demo.so"'
exit

(The ‘-T’ will prevent it from redownloading and compiling protobuf every time)

Then update build.gradle buildNativeMade to the following:

task buildNativeMake(type: Exec) {
    workingDir '../../..'
    commandLine = ['cmd', '/C', 'start', 'makefile_helper.bat']
}

Now, if you set buildWithMake = true, it should create the native libs exactly where gradle expects to find them and copy them into the app dir.

This should work similarly for bazel on Windows, if you wish to install that under bash instead.

I’d prefer not to check in a separate helper script, so if anybody has luck getting the command to run inline in the Exec task itself please let me know.