01 基本配置
参考官网说明:https://www.tensorflow.org/install/source_windows
bazal/msys64的配置参考: https://blog.youkuaiyun.com/longji/article/details/82897664
安装studio 最新版本。重点关注
android sdk/ndk
set NDK_ROOT=D:\install\Android\AndroidSDK\ndk-bundle
%NDK_ROOT%\bin
加入PATH环境变量。
关注一下自己下载的build_tools版本
D:\install\Android\AndroidSDK\build-tools\28.0.2
D:\install\Android\AndroidSDK\build-tools\28.0.3
tensorflow代码下载 ,android版本的demo在examples\android里面,切换到当期相对稳定tag下。
git clone --recurse-submodules https://github.com/tensorflow/tensorflow.git
git checkout -b b1.11.0-rc2 v1.11.0-rc2
02 修改基本信息
使用android studio 打开 \tensorflow\tensorflow\examples\android
项目。
做些简单修改。
项目sdk/ndk基本配置,修改文件 tensorflow\WORKSPACE
把bazel修改为自己本地安装的版本。本地安装的是0.17.2
check_bazel_version_at_least("0.15.0")
==> check_bazel_version_at_least("0.17.2")
api_level、build_tools_version、sdk的path,ndk的path一定要根据自己的实际情况设置。如下。
# Uncomment and update the paths in these entries to build the Android demo.
android_sdk_repository(
name = "androidsdk",
api_level = 28,
# Ensure that you have the build_tools_version below installed in the
# SDK manager as it updates periodically.
build_tools_version = "28.0.3",
# Replace with path to Android SDK on your system
# path = "/Users/baishixian/Library/Android/sdk",
path = "D:/install/Android/AndroidSDK",
)
#
android_ndk_repository(
name="androidndk",
# path="/Users/baishixian/Library/Android/sdk/android-ndk-r14b",
path= "D:/install/Android/AndroidSDK/ndk-bundle",
# This needs to be 14 or higher to compile TensorFlow.
# Please specify API level to >= 21 to build for 64-bit
# archtectures or the Android NDK will automatically select biggest
# API level that it supports without notice.
# Note that the NDK version is not the API level.
api_level=28)
修改生成apk名称,tensorflow_demo ==> zdx_demo。
android_binary(
#name = "tensorflow_demo", # 修改项目名称
name = "zdx_demo",
更换图标资源
tensorflow\tensorflow\examples\android\res\drawable-hdpi\ic_launcher.png
tensorflow\tensorflow\examples\android\res\drawable-mdpi\ic_launcher.png
tensorflow\tensorflow\examples\android\res\drawable-xhdpi\ic_launcher.png
tensorflow\tensorflow\examples\android\res\drawable-xxhdpi\ic_launcher.png
修改字符串资源
tensorflow\tensorflow\examples\android\res\values\base-strings.xml
TensorFlow Demo ==> Zdx Demo
TF Classify ⇒ Zdx Classify
TF Stylize ⇒ Zdx Stylize
TF Speech ⇒ Zdx Speech
03 编译
在tensorflow目录下运行如下命令:
bazel build -c opt //tensorflow/examples/android:zdx_demo
如果前面没有修改apk,这里就还使用tensorflow_demo
bazel build -c opt //tensorflow/examples/android:tensorflow_demo
编译完成后,根据提示找到自己的目标文件。
本例中生成目标文件如下:
C:/users/soft/_bazel_soft/qe65roea/execroot/org_tensorflow/bazel-out/x64_windows-opt/bin/tensorflow/examples/android/zdx_demo.apk
发送到android手机后模拟器即可。