本教程不适用于windows系统,windows系统编译问题比较多。请自行准备翻墙工具,否则部分依赖无法下载。
环境配置
- Tensorflow: 2.5.0
- 操作系统: macOS Big Sur 11.4
- Python: 3.8.2
- Bazel: 3.7.2
- Terminal脚本:zsh
Android NDK:
- NDK version: 20.1.5948944
- API level: 23
Android SDK:
- API level:23
- Android build tools version: 28.0.3
1. 环境安装
以下指令均在Terminal完成
1.1 Homebrew
官方安装指令
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
1.2 Bazel
export BAZEL_VERSION=3.7.2
curl -fLO "https://github.com/bazelbuild/bazel/releases/download/${BAZEL_VERSION}/bazel-${BAZEL_VERSION}-installer-darwin-x86_64.sh"
chmod +x "bazel-${BAZEL_VERSION}-installer-darwin-x86_64.sh"
./bazel-${BAZEL_VERSION}-installer-darwin-x86_64.sh --user
在.zshrc文件添加以下代码
export PATH="$PATH:$HOME/bin"
检查是否生效
bazel --version
1.2.1 给bazel添加代理
ip和端口根据自己代理的设置进行填写
export HTTP_PROXY="http://127.0.0.1:1087";
export HTTPS_PROXY="http://127.0.0.1:1087";
1.3 Python
macOS 11.4系统自带两个版本的python
- python 对应2.7
- python3 对应3.8.2
1.3.1 安装需要的依赖
pip install -U --user pip numpy wheel
pip install -U --user keras_preprocessing --no-deps
1.3.2 修改python与pip指令的指向
个人把python和pip都指向python3和pip3
where python3
where pip3
sudo vim ~/.zshrc
在.zshrc文件添加以下代码
alias python='/usr/bin/python3'
alias pip='/usr/bin/pip3'
使用source指令让其生效
source ~/.zshrc
1.4 Android SDK&NDK
下载Android Studio,打开SDK Manager,勾选右下角的Show Package Details,下载对应的版本 
1.5 Tensorflow源码
git clone https://github.com/tensorflow/tensorflow.git
cd tensorflow
git checkout v2.5.0
2. 编译流程
- 进入tensorflow源码根目录
- 执行配置命令
./configure
以下为配置流程
alexleung@liaozhipengdeMacBook-Pro tensorflow % ./configure
You have bazel 3.7.2 installed.
Please specify the location of python. [Default is /Applications/Xcode.app/Contents/Developer/usr/bin/python3]:
Found possible Python library paths:
/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/site-packages
Please input the desired Python library path to use. Default is [/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/site-packages]
Do you wish to build TensorFlow with ROCm support? [y/N]: n
No ROCm support will be enabled for TensorFlow.
Do you wish to build TensorFlow with CUDA support? [y/N]: n
No CUDA support will be enabled for TensorFlow.
Do you wish to download a fresh release of clang? (Experimental) [y/N]: n
Clang will not be downloaded.
Please specify optimization flags to use during compilation when bazel option "--config=opt" is specified [Default is -Wno-sign-compare]:
Would you like to interactively configure ./WORKSPACE for Android builds? [y/N]: y
Searching for NDK and SDK installations.
Please specify the home path of the Android NDK to use. [Default is /Users/liaozhipeng/library/Android/Sdk/ndk-bundle]: /Users/liaozhipeng/Documents/SDK/ndk/20.1.5948944
WARNING: The NDK version in /Users/liaozhipeng/Documents/SDK/ndk/20.1.5948944 is 20, which is not supported by Bazel (officially supported versions: [10, 11, 12, 13, 14, 15, 16, 17, 18]). Please use another version. Compiling Android targets may result in confusing errors.
Please specify the (min) Android NDK API level to use. [Available levels: ['16', '17', '18', '19', '21', '22', '23', '24', '26', '27', '28', '29']] [Default is 21]: 23
Please specify the home path of the Android SDK to use. [Default is /Users/liaozhipeng/library/Android/Sdk]: /Users/liaozhipeng/Documents/SDK
Please specify the Android SDK API level to use. [Available levels: ['23', '31']] [Default is 31]: 23
Please specify an Android build tools version to use. [Available versions: ['28.0.3', '31.0.0']] [Default is 31.0.0]: 28.0.3
Do you wish to build TensorFlow with iOS support? [y/N]: n
No iOS support will be enabled for TensorFlow.
Preconfigured Bazel build configs. You can use any of the below by adding "--config=<>" to your build command. See .bazelrc for more details.
--config=mkl
--config=mkl_aarch64
--config=monolithic
--config=numa
--config=dynamic_kernels
--config=v2
Preconfigured Bazel build configs to DISABLE default on features:
--config=noaws
--config=nogcp
--config=nohdfs
--config=nonccl
Configuration finished
- 执行编译指令
bazel build -c opt --fat_apk_cpu=x86,x86_64,arm64-v8a,armeabi-v7a \
--host_crosstool_top=@bazel_tools//tools/cpp:toolchain \
//tensorflow/lite/java:tensorflow-lite
- 等待编译成功,中途会从google云盘下载很多依赖,需要确保网络通畅
- 最后在源码根目录下bazel-bin/tensorflow/lite/java/文件夹里面找到.arr文件

问题合集
1. bazel代理失效
错误提示含以下字眼
Proxy address 127.0.0.1:1087 is not a valid URL
可能导致的原因
- 代理参数(如:HTTP_PROXY)写成了小写
- 重新打开了terminal,zsh配置失效,需要重新source一次.zshrc文件
- 代理没连通
|