可以参考官网:https://github.com/tensorflow/tensorflow/tree/master/tensorflow/examples/android#android-studio
官网讲的很简单:
Android Studio may be used to build the demo in conjunction with Bazel. First, make sure that you can build with Bazel following the above directions. Then, look at build.gradle and make sure that the path to Bazel matches that of your system.
At this point you can add the tensorflow/examples/android directory as a new Android Studio project. Click through installing all the Gradle extensions it requests, and you should be able to have Android Studio build the demo like any other application (it will call out to Bazel to build the native code with the NDK).
简单地讲:就是
1、安装bazel
2、下载tensorflow官网代码,在tensorflow/examples/android下的build.gradle中修改bazel的路径
3、用android studio 打开tensorflow/examples/android
4、gradle会根据配置说缺少什么什么版本的api,就直接anzhuang(安装)
5、run
然后报错:
* What went wrong:
Execution failed for task ‘:buildNativeBazel’.
> Process ‘command ‘/usr/bin/bazel” finished with non-zero exit value 1
其实,这就是buildNativeBazel,用ctrl+shift+f,输入buildNativeBazel,有两个结果:
def nativeBuildRule = 'buildNativeBazel'
task buildNativeBazel(type: Exec) {
workingDir '../../..'
commandLine bazelLocation, 'build', '-c', 'opt', \
'tensorflow/examples/android:tensorflow_native_libs', \
'--crosstool_top=//external:android/crosstool', \
'--cpu=' + cpuType, \
'--host_crosstool_top=@bazel_tools//tools/cpp:toolchain'
}
第一条结果就是一个定义,没什么用。主要是第二条,发现执行这个命令,bazel build ….执行的目录是在tensorflow下,所以想到
官网上的Building the Demo with TensorFlow from Source
需要配置
Edit WORKSPACE
NOTE: As long as you have the SDK and NDK installed, the ./configure
script will create these rules for you. Answer “Yes” when the script asks to automatically configure the ./WORKSPACE
.
The Android entries in <workspace_root>/WORKSPACE
must be uncommented with the paths filled in appropriately depending on where you installed the NDK and SDK. Otherwise an error such as: “The external label ‘//external:android/sdk’ is not bound to anything” will be reported.
Also edit the API levels for the SDK in WORKSPACE to the highest level you have installed in your SDK. This must be >= 23 (this is completely independent of the API level of the demo, which is defined in AndroidManifest.xml). The NDK API level may remain at 14.
也就是直接打开WORKSPACE文件,将下面的填上就好了,注意andriod studio 最新的是安装ndk16,现在bazel还不支持,所以自己下载r14b,解压,然后路径填写自己解压的就可以,而sdk version 也就是api level,填写和在tensorflow/examples/android下的build.gradle中使用的版本一致就可以,因为android studio 的gradle会自动下载相应的版本,就是“4、gradle会根据配置说缺少什么什么版本的api,就直接anzhuang(安装)”
#android_sdk_repository(
# name = “androidsdk”,
# api_level = 23,
# # Ensure that you have the build_tools_version below installed in the
# # SDK manager as it updates periodically.
# build_tools_version = “25.0.2”,
# # Replace with path to Android SDK on your system
# path = “<PATH_TO_SDK>”,
#)
#
# Android NDK r12b is recommended (higher may cause issues with Bazel)
#android_ndk_repository(
# name=”androidndk”,
# path=”<PATH_TO_NDK>”,
# # This needs to be 14 or higher to compile TensorFlow.
# # Note that the NDK version is not the API level.
# api_level=14)
取消#注释,然后填入,再run就ok啦。
参考:
http://blog.csdn.net/masa_fish/article/details/54585537
http://blog.csdn.net/masa_fish