Tutorial on how to integrate Tensorflow (JAVA-API) into Android-Studio – from scratch


There are several ways, how one can integrate the powerful machine-learning framework of Google into an Android App, but most of the examples – at least the ones I found – only showed how the example build has to be built using the Bazel build tools. I wanted tot he „old-school“ way using just the Android Studio and use the Tensorflow Framework as a 3rd party dependency.

At first I followed this great library on GitHub: https://github.com/miyosuda/TensorFlowAndroidMNIST

But eventually I came to the point where some of the OPs (i.e. Dropout) were not implemented in the existing Library. Therefore I started to find a way to build the Libs. I know hardly anything about all these fancy software things, thus I was searching for the easiest solution and came up with the followin recipe which works on a Macbook OSX El Capitan. I’m pretty sure, that it’ll work on Linux as well if you’ve all the necessary libraries installed already. I’m using Anaconda 2 and istalled it from this source: https://www.continuum.io/downloads


Fort he first steps I refer to this great guide:



Install BREW


/usr/bin/ruby -e „$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)“


Use brew to install bazel and swig

$ brew install bazel swig


Install python dependencies

$ sudo easy_install -U six
$ sudo easy_install -U numpy
$ sudo easy_install wheel


Download the TensorFlow git repo

$ git clone https://github.com/tensorflow/tensorflow


Configure TensorFlow Build

Type  ./configure in the Terminal and hit enter
$ ./configure

I used default inputs.

Once configure is complete, you need to edit the WORKSPACE file in in the TensorFlow root dir to setup your android SDK


Add the following lines (i.e. uncomment and edit the paths)


# Uncomment and update the paths in these entries to build the Android libraries:


name = „androidsdk“,

api_level = 23,

build_tools_version = „24.0.2“,




# Replace with path to Android SDK on your systempath = „/Users/USER/Library/Android/sdk/“,)




ctlr+x and yes for saving.


Then open the Terminal and in tensorflow-root type:

$ bazel build //tensorflow/contrib/android:libtensorflow_inference.so    –crosstool_top=//external:android/crosstool    –host_crosstool_top=@bazel_tools//tools/cpp:toolchain    –cpu=armeabi-v7a

libtensorflow_inference.so   file gets build and can be found in the following path:


This file has to be copied into you apps lib folder. In my case:



Now start Android Studio and open an existing project. To add the tensorflow framework choose “import module” and direct to the path:



Android Studio will ask you to integrate CMAKE plugin  – install it from the Package Manager. After a while of compiling heavy stuff, the module should be integrated in your App.



Then I did the following (I had to upgrade the XCODE Version! 8.1.2 or so):

$ build_all_android.sh

Add the dependencies to your project

Open the file settings.gradle and add the following lines

include ‚:TensorFlow-Android-Inference’findProject(„:TensorFlow-Android-Inference“).projectDir =             new File(„${/path/to/tensorflow_repo}/contrib/android/cmake“)

Open the file build.gradle of your main app and add the dependency:

debugCompile project(path: ‚: TensorFlow-Android-Inference, configuration: ‚debug‘)releaseCompile project(path: ‚: TensorFlow-Android-Inference ‚, configuration: ‚release‘)


Note: this makes native code in the lib traceable from your app.

Dependencies (see: https://github.com/tensorflow/tensorflow/tree/master/tensorflow/contrib/android/cmake)

TensorFlow-Android-Inference depends on the TensorFlow static libs already built in your local TensorFlow repo directory. For Linux/Mac OS, build_all_android.sh is used in build.gradle to build it. It DOES take time to build the core libs; so, by default, it is commented out to avoid confusion (otherwise Android Studio would appear to hang during opening the project). To enable it, refer to the comment in

  • build.gradle


  • TensorFlow-Inference-debug.aar
  • TensorFlow-Inference-release.aar

File libtensorflow_inference.so should be packed under jni/${ANDROID_ABI}/ in the above aar, and it is transparent to the app as it will acccess them via equivalent java APIs.



The project can be found in my Github Repo:




I hope that helps. In case something’s unclear, feel free to leave a comment 🙂


Kommentar verfassen

Trage deine Daten unten ein oder klicke ein Icon um dich einzuloggen:


Du kommentierst mit Deinem WordPress.com-Konto. Abmelden / Ändern )


Du kommentierst mit Deinem Twitter-Konto. Abmelden / Ändern )


Du kommentierst mit Deinem Facebook-Konto. Abmelden / Ändern )

Google+ Foto

Du kommentierst mit Deinem Google+-Konto. Abmelden / Ändern )

Verbinde mit %s