title: Problems with using tensorflow lite C++ API in Android Studio Project
body: <p>I am currently working on a project regarding neural networks.
  For this, I want to build an Android Application which should use tensorflow [lite] to solve some object detection / recognition problems.</p>
  
  <p>As I want the code to be as portable as possible, I want to write most of the code in C++, thus using the C++ API of tensorflow lite over the Java API / wrapper.
  So, I modified tensorflow/contrib/lite/BUILD and added the following to be able to create a shared tensorflow library.</p>
  
  <pre><code>cc_binary(
  name = "libtensorflowLite.so",
  
  linkopts=["-shared", "-Wl"],
  linkshared=1,
  
  copts = tflite_copts(),
  deps = [
      ":framework",
      "//tensorflow/contrib/lite/kernels:builtin_ops",
  ],
  
  )
  </code></pre>
  
  <p>(Which is based on the answer to this issue: <a href="https://github.com/tensorflow/tensorflow/issues/17826" rel="noreferrer">https://github.com/tensorflow/tensorflow/issues/17826</a>)</p>
  
  <p>Then I used</p>
  
  <pre><code>bazel build //tensorflow/contrib/lite:libtensorflowLite.so --crosstool_top=//external:android/crosstool --cpu=arm64-v8a --host_crosstool_top=@bazel_tools//tools/cpp:toolchain --cxxopt="-std=c++11"
  </code></pre>
  
  <p>to finally build it.</p>
  
  <p>Afterwards I headed over to Android Studio and set up a basic project.
  For adding the shared library to the project, I refered to this example: </p>
  
  <p><a href="https://github.com/googlesamples/android-ndk/tree/840858984e1bb8a7fab37c1b7c571efbe7d6eb75/hello-libs" rel="noreferrer">https://github.com/googlesamples/android-ndk/tree/840858984e1bb8a7fab37c1b7c571efbe7d6eb75/hello-libs</a></p>
  
  <p>I also added the needed dependencies for flatbuffers.</p>
  
  <p>The build / compilation process succeeds without any linker errors (well, at least after trying around for some hours..).</p>
  
  <p>The APK is then successfully installed on an Android device, but immediately crashes after it starts. Logcat gives the following output:</p>
  
  <pre><code>04-14 20:09:59.084 9623-9623/com.example.hellolibs E/AndroidRuntime: FATAL EXCEPTION: main
      Process: com.example.hellolibs, PID: 9623
      java.lang.UnsatisfiedLinkError: dlopen failed: library "/home/User/tensorflowtest/app/src/main/cpp/../../../../distribution/tensorflow/lib/x86/libtensorflowLite.so" not found
          at java.lang.Runtime.loadLibrary0(Runtime.java:1016)
          at java.lang.System.loadLibrary(System.java:1657)
          at com.example.hellolibs.MainActivity.&lt;clinit&gt;(MainActivity.java:36)
          at java.lang.Class.newInstance(Native Method)
          at android.app.Instrumentation.newActivity(Instrumentation.java:1174)
          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2669)
          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
          at android.app.ActivityThread.-wrap11(Unknown Source:0)
          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
          at android.os.Handler.dispatchMessage(Handler.java:106)
          at android.os.Looper.loop(Looper.java:164)
          at android.app.ActivityThread.main(ActivityThread.java:6494)
          at java.lang.reflect.Method.invoke(Native Method)
          at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
  </code></pre>
  
  <p>I tried this on an android x86 emulator and on a real arm64-v8a android smartphone.</p>
  
  <p>So for me this looks like on startup the application tries to load the tensorflowLite shared library, but is unable to find it. 
  Opening the apk with a zip archive manager I can verify that the platform (arm, x86) dependent .so files are packed into the APK as expected (by adding the following to build.gradle: </p>
  
  <pre><code>sourceSets {
          main {
              // let gradle pack the shared library into apk
              jniLibs.srcDirs = ['../distribution/tensorflow/lib']
          }
  })
  </code></pre>
  
  <p>What I do not understand is why it looks for the library in the path where I placed it on my Ubuntu 17.10 PC.
  So, I thought I had done a mistake trying to adapt the example about adding external libraries to an Android Studio Project I mentioned earlier.
  That's why I downloaded the whole project and opened it in Android Studio and verified that the example works as expected. Afterwards I replaced the example libgperf.so by the libtensorflowLite.so and left everything else, especially the CMakeLists.txt, untouched.
  But I get the exact same error again, therefore I suspect this to be a problem with the libtensorflowLite library itself and not the android project (although that's just my guess).</p>
  
  <p>I am working on android studio 3.1.1, NDK Version 14 and API Level 24 (Android 7.0).
  If anyone has an idea what could be wrong, any help would be highly appreciated.
  I am also open for any other methods which allow me to use tensorflow lite with C++ for an android application.</p>
  
  <p>Thanks a lot,</p>
  
  <p>Martin</p>