Android:搭建Tensorflow应用在手机上的编译环境

Bazel:


What is Bazel?

https://docs.bazel.build/versions/master/bazel-overview.html
Bazel is an open-source build and test tool similar to Make, Maven, and Gradle. 
It uses a human-readable, high-level build language. 
Bazel supports projects in multiple languages and builds outputs for multiple platforms.

下载tensorflow代码


https://github.com/tensorflow:包括tensorflow相关所有开源项目
git clone https://github.com/tensorflow/tensorflow.git

下载Android SDK


使用Android Studio下载


下载Android NDK


https://developer.android.com/ndk/downloads/older_releases#ndk-12b-downloads
Linux 64-bit (x86)    android-ndk-r13b-linux-x86_64.zip
这里看上去有点疑惑,在手机上运行,名字上怎么是android-ndk-r13b-linux-x86_64 包含linux/ x86什么的?
linux/x86说的是开发环境,最后解压后的名字也没有包含linux-x86_64


使用Bazel编译TensorFlow Mobile Demo


In your copy of the TensorFlow source, update the WORKSPACE file with the location of your SDK and NDK,
where it says <PATH_TO_NDK> and <PATH_TO_SDK>.

但是下载的tensorflow/WORKSPACE文件里没有“location of your SDK and NDK”

原来的代码是有的,肯定是更换了方法。


通过文件的提交记录看下:git log --pretty=oneline WORSPACE


5105350be955422169de1f22bb99f928c1f4c2ae Moves generated android_sdk() and android_ndk() repo rules out of WORKSPACE.
diff --git a/WORKSPACE b/WORKSPACE
index 4ddfb9a..fd7570a 100644
--- a/WORKSPACE
+++ b/WORKSPACE
@@ -22,26 +22,10 @@ check_bazel_version_at_least("0.10.0")
 
 load("//tensorflow:workspace.bzl", "tf_workspace")
 
-# 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 = "26.0.1",
-#    # Replace with path to Android SDK on your system
-#    path = "<PATH_TO_SDK>",
-#)
-#
-#android_ndk_repository(
-#    name="androidndk",
-#    path="<PATH_TO_NDK>",
-#    # 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=14)

这里删除了肯定有新的方法设置这些路径:
+"""Repository rule for Android SDK and NDK autoconfiguration.
+
+`android_configure` depends on the following environment variables:
+
+  * `ANDROID_NDK_HOME`: Location of Android NDK root.
+  * `ANDROID_SDK_HOME`: Location of Android SDK root.
+  * `ANDROID_SDK_API_LEVEL`: Desired Android SDK API version.
+  * `ANDROID_NDK_API_LEVEL`: Desired Android NDK API version.
+  * `ANDROID_BUILD_TOOLS_VERSION`: Desired Android build tools version.
+"""
+
+# TODO(mikecase): Move logic for getting default values for the env variables
+# from configure.py script into this rule.
+
+_ANDROID_NDK_HOME = "ANDROID_NDK_HOME"
+_ANDROID_SDK_HOME = "ANDROID_SDK_HOME"
+_ANDROID_NDK_API_VERSION = "ANDROID_NDK_API_LEVEL"
+_ANDROID_SDK_API_VERSION = "ANDROID_SDK_API_LEVEL"
+_ANDROID_BUILD_TOOLS_VERSION = "ANDROID_BUILD_TOOLS_VERSION"

现在把代码切到:5105350be955422169de1f22bb99f928c1f4c2ae Moves generated之前的分支
git checkout 22f3a97b8b089202f60bb0c7697feb0c8e0713cc 编译出现下面编译错误

从上面的代码看好像需要定义以上几个环境变量,实验后的编译错误是

contrib/lite/kernels/internal/BUILD:620:1: no such package '@androidndk//': The repository could not be resolved and referenced by '//tensorflow/contrib/lite/kernels/internal:cpu_check'

没有找到NDK

android_sdk_repository (     
    name = "androidsdk",     
    api_level = 23,     
    build_tools_version = "26.0.1",                                                                                                                                                                                
    path = "/home/mi/Android/Sdk/",     
)     
     
android_ndk_repository(      
    name = "androidndk",     
    path = "/home/mi/Android/Ndk/android-ndk-r14b/",     
    api_level = 19,     
)      

把上面的配置加到WORKSPACE的最后

ERROR: /home/mi/code/tensorflow_new/tensorflow/WORKSPACE:83:1: Cannot redefine repository after any load statement in the WORKSPACE file (for repository 'androidsdk')
ERROR: Error evaluating WORKSPACE file
直接删除关于androidsdk, 只保留androidndk 重新编译竟然通过!!!


Current Bazel version is 0.11.0, expected at least 0.5.4


继续看下面的git log: Bazel版本号和built tools的版本号也是要对应的
787d86a37d3c2c4197245e730b08bf00a5a90666 Update Android builds to use build tools 26.0.1 (required for Bazel 0.5.4)

更改buildToosVersion


更改build.gradle文件里的buildToolsVersion "26.0.1",会提示升级build Tools

看当前版本,卸载,安装


bazel version
Build label: 0.11.0

$ sudo apt-get --purge remove bazel
$ sudo apt autoremove

https://docs.bazel.build/versions/master/install-ubuntu.html

按设置更改WORKSPACE

bazel: 0.4.5/ build tools:26.0.1/ tensorflow代码分支

787d86a37d3c2c4197245e730b08bf00a5a90666 Update Android builds to use build tools 26.0.1 (required for Bazel 0.5.4)

在按如下配置,编译环境就可以了,后面可以各种编译

编译 demo
bazel build -c opt //tensorflow/examples/android:tensorflow_demo
adb install -r bazel-bin/tensorflow/examples/android/tensorflow_demo.apk

编译 动态库/jar
https://github.com/tensorflow/tensorflow/tree/master/tensorflow/contrib/android 该网址有详细的编译命令

换行符是存在的
bazel build -c opt //tensorflow/contrib/android:libtensorflow_inference.so \
   --crosstool_top=//external:android/crosstool \
   --host_crosstool_top=@bazel_tools//tools/cpp:toolchain \
   --cpu=armeabi-v7a

Replacing armeabi-v7a with your desired target architecture.

The library will be located at:
bazel-bin/tensorflow/contrib/android/libtensorflow_inference.so


To build the Java counterpart:
bazel build //tensorflow/contrib/android:android_tensorflow_inference_java
You will find the JAR file at:
bazel-bin/tensorflow/contrib/android/libandroid_tensorflow_inference_java.jar

使用Andorid Studio的方式

出现如下错误:

The specified Android SDK Build Tools version (26.0.2) is ignored, as it is below the minimum supported version (27.0.3) for Android Gradle Plugin 3.1.4.
Android SDK Build Tools 27.0.3 will be used.
To suppress this warning, remove "buildToolsVersion '26.0.2'" from your build.gradle file, as each version of the Android Gradle Plugin now has a default version of the build tools.
Update Build Tools version and sync project
 

这是选择了Android Gradle Plugin Update Recommended导致的

通过git diff 可以看到选择后更改的文件内容,git checkout 后重新打开project, 不使用该推荐就没有问题了,

可以看下此时的git diff

 buildscript {
     repositories {
         jcenter()
+        google() //自动添加的
     }
 
     dependencies {
@@ -36,11 +37,12 @@ buildscript {
 allprojects {
     repositories {
         jcenter()
+        google()
     }
 }
 
 // set to 'bazel', 'cmake', 'makefile', 'none' 手动更改的
-def nativeBuildSystem = 'bazel'
+def nativeBuildSystem = 'none'

 

猜你喜欢

转载自blog.csdn.net/u011279649/article/details/81736560