1. 安装tensorflow
官方文档地址:https://www.tensorflow.org/install/ 。
2. 安装构建工具bazel
官方文档地址:https://bazel.build/versions/master/docs/install.html 。
安装JDK 8,并添加APT库,然后安装bazel
sudo apt-get install openjdk-8-jdk
echo "deb [arch=amd64] http://storage.googleapis.com/bazel-apt stable jdk1.8" | sudo tee /etc/apt/sources.list.d/bazel.list
curl https://bazel.build/bazel-release.pub.gpg | sudo apt-key add -
sudo apt-get update && sudo apt-get install bazel
3. 下载源码
使用Git下载代码。
git clone --recurse-submodules https:
4. 配置本地环境
在...\tensorflow\tensorflow\examples\android目录下。
1). 修改WORKSPACE文件
先修改/tensorflow目录下的WORKSPACE文件,找到如下一段代码,对应配置好你的sdk版本以及位置,还有ndk的版本和位置,并且去掉注释:
# Uncomment and update the paths in these entries to build the Android demo.
#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)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
比如:
android_sdk_repository(
name = "androidsdk",
# 设置成你安装sdk的最高版本,对应修改manifest.xml以及gradle的target
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.3",
# Replace with path to Android SDK on your system
path = "/home/cxq/android-sdk-linux",
)
# 推荐使用r12版本,更高的版本可能会造成bazel出错
android_ndk_repository(
name="androidndk",
path="/home/cxq/android-sdk-linux/ndk-bundle",
# This needs to be 14 or higher to compile TensorFlow.
# Note that the NDK version is not the API level.
api_level=14)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
注意:
1. api_level需要设置成你本地sdk最高的版本,推荐23及以上,在android:build.gradle 以及AndroidManifest.xml也对应修改targetSdkVersion
2. ndk的版本,推荐使用r12,否则bazel编译的时候会报错。
2. 构建
进入到../tensorflow目录下,输入以下命令:
bazel build -c opt //tensorflow/examples/android:tensorflow_demo
如果有报protocol buffers的相关错误,运行git submodule update --init
命令,并且确保WORKSPACE文件配置对了
3. 安装apk
开启手机的调试模式,运行:
adb install -r bazel-bin/tensorflow/examples/android/tensorflow_demo.apk
4. 使用Android Studio
可以在Android Studio中直接打开tensorflow/examples/android,但是需要配置好你的gradle、sdk、ndk
1. gradle必须要在3.3以上版本
2. build_tool_version需要指定在25以上
3. 配置好你的ndk
4. 在android:build.gradle 中配置好你的bazel路径,默认的是如下的:
def bazelLocation = '/usr/local/bin/bazel'
但是在该路径下并没有bazel,因此会报如下错误:
Error:Execution failed for task ':buildNativeBazel'.
A problem occurred starting process 'command '/usr/local/bin/bazel''
此时只需要把路径改成你的bazel路径就好,比如:
def bazelLocation = '/usr/bin/bazel'
这样就能直接在AS中使用run运行demo了。