背景是github上下载的工程,说明仅支持bazel编译,折腾了一天Android studio,失败。
不得不尝试单价bazel编译环境,并不复杂,过程记录如下
说明:ubuntu环境是20.04,pve虚拟机安装
sudo apt install openjdk-11-jdk
Google网站下载linux上的二进制包,也可以apt安装,前者定制方便些
下载地址:下载 Android Studio 和应用工具 - Android 开发者 | Android Developers
为了方便描述,加速下载目录是用户根目录,即$HOME
cd ~
tar -xvf android-studio-*.tar.gz
运行:
cd ~/android-studio/bin
./studio.sh
随后的界面与Windows上没什么区别,我是ssh登录的,居然也可以图形交互,运行过程会下载安装android sdk,这个是最主要的,google不让单独下载android sdk了,没办法。
记录下,默认的sdk目录~/Android/Sdk/
出于某些考虑,没有安装最新版本,挑了个中间点的版本。例如4.2.2
下载地址:https://github.com/bazelbuild/bazel/releases/download/4.2.2/bazel-4.2.2-dist.zip
解压编译:
unzip bazel-4.2.2-dist.zip
sudo ./compile.sh
编译过程出现了python报错,做个软链接解决
ERROR: /mnt/sdb/code/05_AS/src/BUILD:319:9: Executing genrule //src:embedded_tools_nojdk failed: (Exit 127): bash failed: error executing command
(cd /tmp/bazel_SpFJt8tb/out/execroot/io_bazel && \
exec env - \
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin \
/bin/bash -c 'source external/bazel_tools/tools/genrule/genrule-setup.sh; bazel-out/host/bin/src/create_embedded_tools "bazel-out/k8-opt/bin/src/embedded_tools_nojdk.zip" bazel-out/k8-opt/bin/src/embedded_tools_nojdk.params')
Execution platform: //:default_host_platform
/usr/bin/env: 'python': No such file or directory
Target //src:bazel_nojdk failed to build
INFO: Elapsed time: 398.790s, Critical Path: 53.56s
INFO: 1055 processes: 31 internal, 1024 local.
FAILED: Build did NOT complete successfully
原因是/usr/bin目录下找不到python,加软连接
ln -s /usr/bin/python3.8 /usr/bin/python
编译ok后,把out目录的编译结果拷贝到/usr/bin
cp ./output/bazel /usr/bin
chmod 777 /usr/bin/bazel
在下载好的Android工程下执行
bazel build testdpc
报错:
obana@pve-ubt20:/mnt/sdb/code/03_github/android-testdpc$ bazel build testdpc
Extracting Bazel installation...
Starting local Bazel server and connecting to it...
ERROR: While resolving toolchains for target //:testdpc: no matching toolchains found for types @bazel_tools//tools/android:sdk_toolchain_type
ERROR: Analysis of target '//:testdpc' failed; build aborted: no matching toolchains found for types @bazel_tools//tools/android:sdk_toolchain_type
INFO: Elapsed time: 20.400s
INFO: 0 processes.
FAILED: Build did NOT complete successfully (12 packages loaded, 19 targets configured)
原因是没有加入android sdk环境变量
export ANDROID_HOME=$HOME/Android/Sdk/
后续编译方便,可以把这行加入~/.bashrc
接着下一个报错:
obana@pve-ubt20:/mnt/sdb/code/03_github/android-testdpc$ bazel build testdpc
INFO: Analyzed target //:testdpc (30 packages loaded, 1164 targets configured).
INFO: Found 1 target...
ERROR: /home/obana/.cache/bazel/_bazel_obana/f87dd64c2bc0bae87f9a70b466547b28/external/androidsdk/BUILD.bazel:13:25: Extracting interface @androidsdk//:dx_jar_import failed: missing input file 'external/androidsdk/build-tools/34.0.0/lib/dx.jar', owner: '@androidsdk//:build-tools/34.0.0/lib/dx.jar'
Target //:testdpc failed to build
Use --verbose_failures to see the command lines of failed build steps.
ERROR: /home/obana/.cache/bazel/_bazel_obana/f87dd64c2bc0bae87f9a70b466547b28/external/androidsdk/BUILD.bazel:13:25 Extracting interface @androidsdk//:dx_jar_import failed: 1 input file(s) do not exist
INFO: Elapsed time: 430.708s, Critical Path: 1.62s
INFO: 29 processes: 27 internal, 2 linux-sandbox.
FAILED: Build did NOT complete successfully
这个错误,很多同学应该熟悉,解决方案很简单,打开Android Studio,下载32版本的build tool,然后切换到32版本。
修改项目根目录下的WORKSPACE文件,
android_sdk_repository(
name = "androidsdk",
api_level = 34,
build_tools_version = "32.0.0",
)
也有一种改法,就是仿照windows上处理。
cp ~/Android/Sdk/build-tools/34.0.0/d8 ~/Android/Sdk/build-tools/34.0.0/dx
cp ~/Android/Sdk/build-tools/34.0.0/lib/d8.jar ~/Android/Sdk/build-tools/34.0.0/lib/dx.jar
我也在stackoverflow上找到如下方法,不知道改哪,放弃;上述方法更通用些
# Enable d8 merger
build --define=android_dexmerger_tool=d8_dexmerger
# Flags for the d8 dexer
build --define=android_incremental_dexing_tool=d8_dexbuilder
build --define=android_standalone_dexing_tool=d8_compat_dx
build --nouse_workers_with_dexbuilder
编译成功后,可以用下命令运行APP,可以连物理机,也可以avd运行
bazel mobile-install //android:android
教程结束,enjoy!
附:官方文档地址Build Tutorial - Android - Bazel 0.20.0