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

Is it possible to import the project int android Studio and compile using NDK? #3444

Closed
vishnudevk opened this issue Jul 21, 2016 · 7 comments
Assignees

Comments

@vishnudevk
Copy link

I was trying to import the project into android studio and work on it.
Getting lot of compilation issues on jni compilation.
Is it possible to do that?
Any constraints we have ?

@andrewharp andrewharp self-assigned this Jul 21, 2016
@andrewharp
Copy link
Contributor

andrewharp commented Jul 21, 2016

Hi, I've just given this a whirl and managed to successfully open the Android Tensorflow Demo under Android Studio and build it from there. You'll still need Bazel for building the native libs, but you'll be able to get full Java support through Android Studio.

Some of these steps are shared with
https://github.com/tensorflow/tensorflow/blob/master/tensorflow/examples/android/README.md, so feel free to skip if you've already done them.

Instructions:
Download and install bazel 0.3.0 for your system: https://github.com/bazelbuild/bazel/releases

Download Android NDK r11c and unzip it somewhere:

Clone repo:

git clone --recursive https://github.com/tensorflow/tensorflow/
cd tensorflow

Now edit WORKSPACE to point to your NDK (the SDK should be unnecessary, since only AS will be using it, not Bazel).

Install Assets for Demo:

wget https://storage.googleapis.com/download.tensorflow.org/models/inception5h.zip -O /tmp/inception5h.zip
unzip /tmp/inception5h.zip -d tensorflow/examples/android/assets/

Build native libs:

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

NATIVE_FOLDER=tensorflow/examples/android/libs/$CPU
mkdir -p $NATIVE_FOLDER
cp bazel-bin/tensorflow/examples/android/libtensorflow_demo.so $NATIVE_FOLDER 

Set up the Android Studio project:

  • Select "Open an existing Android Studio project" (it's not, but don't worry about that)
  • It will detect an AndroidManifest.xml, and ask you to configure. Do so.
  • Open "File/Project Structure"
    • click "Modules"
      • select the "android" module (not the "Android" below it)
        • On the "Sources" tab, make sure that res is a resource directory, and src is a source directory.
        • On the "Paths" tab, make sure that the paths are set to a directory you want to output to.

Build the APK
Under Build, click "Generate Signed APK"

With any luck, this should create a working APK! You'll need to re-run the "Build Native Libs" instructions any time you modify .cc or .h files, but you should be able to edit the Java code as you normally would for any other Android project.

In the future we may better support Android Studio with cmake support, so that some of these steps would be unnecessary.

@andrewharp
Copy link
Contributor

Just to followup on this, there is now a build.gradle for the Android demo that will automate most of this if you just add the tensorflow/examples/android dir as an existing AS project. You'll still need to have Bazel installed, though.

@riless
Copy link

riless commented Oct 17, 2016

Error on Sync build.gradle

Error:(72, 0) Could not get unknown property 'assembleDebug' for root project 'android' of type org.gradle.api.Project.

Works fine when I comment the line assembleDebug.dependsOn copyNativeLibs

@andrewharp
Copy link
Contributor

@riless Seems to be a change/bug in Gradle 2.2: https://code.google.com/p/android/issues/detail?id=219732

A workaround may be to rewrite it as:

tasks.whenTaskAdded { task ->
    if (task.name == 'assembleDebug') {
        task.dependsOn 'copyNativelibs'
    }
}

@riless
Copy link

riless commented Nov 15, 2016

copyNativeLibs (with capital L)

@hannan-sultan
Copy link

hannan-sultan commented Dec 6, 2016

Can anyone guide me? I want to take the scoreView result as recog.getTitle() in a texview.

@andrewharp

@andrewharp
Copy link
Contributor

@CodeLoverr It sounds like you just want to call view.setText(recog.getTitle()). But please direct general Android programming questions to StackOverflow, you'll find better support for that there.

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

No branches or pull requests

4 participants