Ubuntu本地构建MediaPipe,出现如下类似问题
ERROR: /home/user/mediapipe/mediapipe/java/com/google/mediapipe/framework/BUILD:130:16: Compiling Java headers mediapipe/java/com/google/mediapipe/framework/libmediapipe_exception_android-hjar.jar (1 source file) failed: (Exit 1): java failed: error executing command (from target //mediapipe/java/com/google/mediapipe/framework:mediapipe_exception_android) external/remotejdk11_linux/bin/java -XX:-CompactStrings '--add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED' '--add-exports=jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED' ... (remaining 40 arguments skipped)
Use --sandbox_debug to see verbose messages from the sandbox and retain the sandbox build root for debugging
java.lang.ArrayIndexOutOfBoundsException: Index -1 out of bounds for length 71
at com.google.turbine.bytecode.ConstantPoolReader.utf8(ConstantPoolReader.java:120)
at com.google.turbine.bytecode.ClassReader.readMethodParameters(ClassReader.java:229)
at com.google.turbine.bytecode.ClassReader.readMethods(ClassReader.java:438)
at com.google.turbine.bytecode.ClassReader.read(ClassReader.java:105)
at com.google.turbine.bytecode.ClassReader.read(ClassReader.java:55)
at com.google.turbine.binder.bytecode.BytecodeBoundClass$1.get(BytecodeBoundClass.java:91)
at com.google.turbine.binder.bytecode.BytecodeBoundClass$1.get(BytecodeBoundClass.java:88)
at com.google.common.base.Suppliers$NonSerializableMemoizingSupplier.get(Suppliers.java:183)
at com.google.turbine.binder.bytecode.BytecodeBoundClass$4.get(BytecodeBoundClass.java:151)
at com.google.turbine.binder.bytecode.BytecodeBoundClass$4.get(BytecodeBoundClass.java:147)
at com.google.common.base.Suppliers$NonSerializableMemoizingSupplier.get(Suppliers.java:183)
at com.google.turbine.binder.bytecode.BytecodeBoundClass.children(BytecodeBoundClass.java:166)
at com.google.turbine.binder.Resolve.resolve(Resolve.java:67)
at com.google.turbine.binder.Resolve.resolve(Resolve.java:72)
at com.google.turbine.binder.Resolve.resolve(Resolve.java:49)
at com.google.turbine.binder.TypeBinder$ClassMemberScope.lookup(TypeBinder.java:126)
at com.google.turbine.binder.lookup.CompoundScope.lookup(CompoundScope.java:36)
at com.google.turbine.binder.lookup.CompoundScope.lookup(CompoundScope.java:41)
at com.google.turbine.binder.TypeBinder.bindClassTy(TypeBinder.java:844)
at com.google.turbine.binder.TypeBinder.bindTy(TypeBinder.java:815)
at com.google.turbine.binder.TypeBinder.bindMethod(TypeBinder.java:642)
at com.google.turbine.binder.TypeBinder.bindMethods(TypeBinder.java:574)
at com.google.turbine.binder.TypeBinder.bind(TypeBinder.java:254)
at com.google.turbine.binder.TypeBinder.bind(TypeBinder.java:147)
at com.google.turbine.binder.Binder.bindTypes(Binder.java:307)
at com.google.turbine.binder.Binder.bind(Binder.java:156)
at com.google.turbine.binder.Binder.bind(Binder.java:97)
at com.google.turbine.main.Main.bind(Main.java:259)
at com.google.turbine.main.Main.compile(Main.java:157)
at com.google.turbine.main.Main.compile(Main.java:132)
at com.google.turbine.main.Main.main(Main.java:88)
Target //mediapipe/examples/android/src/java/com/google/mediapipe/apps/aar_example:mediapipe_face_detection.aar failed to build
Use --verbose_failures to see the command lines of failed build steps.
INFO: Elapsed time: 4.554s, Critical Path: 1.33s
INFO: 283 processes: 136 internal, 147 linux-sandbox.
这是由于bazel_java_tools问题,可更新bazel版本进行修复,我从6.1.1更新到6.4.0,这里也不能更新到过高的版本不然会出现新的错误,比如当更新到7.3.1时如下:
ERROR: /home/user/.cache/bazel/_bazel_dtc/1098f9536915fc724bdd553e1b953ac0/external/bazel_tools/tools/cpp/BUILD:58:19: in cc_toolchain_alias rule @@bazel_tools//tools/cpp:current_cc_toolchain:
Traceback (most recent call last):
File "/virtual_builtins_bzl/common/cc/cc_toolchain_alias.bzl", line 26, column 48, in _impl
File "/virtual_builtins_bzl/common/cc/cc_helper.bzl", line 225, column 17, in _find_cpp_toolchain
Error in fail: Unable to find a CC toolchain using toolchain resolution. Target: @@bazel_tools//tools/cpp:current_cc_toolchain, Platform: @@//mediapipe:android_arm64_platform, Exec platform: @@bazel_tools//tools:host_platform
ERROR: /home/user/.cache/bazel/_bazel_dtc/1098f9536915fc724bdd553e1b953ac0/external/bazel_tools/tools/cpp/BUILD:58:19: Analysis of target '@@bazel_tools//tools/cpp:current_cc_toolchain' failed
ERROR: Analysis of target '//mediapipe/examples/android/src/java/com/google/mediapipe/apps/handtrackinggpu:handtrackinggpu' failed; build aborted: Analysis failed
INFO: Elapsed time: 18.494s, Critical Path: 0.02s
INFO: 1 process: 1 internal.
ERROR: Build did NOT complete successfully
FAILED:
Fetching repository @@rules_java~~toolchains~remote_java_tools; starting
Fetching repository @@npm; starting
Fetching repository @@rules_java; starting
Fetching repository @@yarn; starting
Fetching repository @@com_google_protobuf; starting
Fetching repository @@rules_java~~toolchains~remote_java_tools_linux; starting
Fetching repository @@maven; starting
Fetching repository @@nodejs_linux_amd64; starting ... (10 fetches)
更新bazel 使用如下方法, bazel releases
sudo apt install bazel-6.4.0
sudo rm -rf /usr/bin/bazel
sudo ln -s /usr/bin/bazel-6.4.0 /usr/bin/bazel
查看是否更新成功
bazel --version
本地部署时还有很多其他问题,按照官方指导进行操作,主要注意的地方是sdk与ndk的安装
vim ./.bashrc
将下面两行加入bashrc中, NDK releases
export ANDROID_HOME=/home/user/Android/Sdk
export ANDROID_NDK_HOME=/home/user/ndk/android-ndk-r26d
执行下面命令完成设置
source ./.bashrc
然后执行下脚本
cd mediapipe //后面操作都在该路径下
sh setup_android_sdk_and_ndk.sh
bash setup_android_sdk_and_ndk.sh
执行之后,会自动将 android_ndk_repository() 和 android_sdk_repository() 规则添加到文件中, WORKSPACE如下所示
android_sdk_repository(name = "androidsdk", path = "/home/user/Android/Sdk")
android_ndk_repository(name = "androidndk", api_level=21, path = "/home/user/ndk/android-ndk-r26d")
bind(name = "android/crosstool", actual = "@androidndk//:toolchain")
这里我们需要根据自己ndk实际位置进行修改这里我们需要改成:
android_ndk_repository(name = "androidndk", api_level=21, path = "/home/user/Android/ndk/android-ndk-r27")
如果这里没有配置好会出现报toolchain/androidsdk/androidndk之类的错误,注意检查
RROR: /home/user/.cache/bazel/_bazel_dtc/1098f9536915fc724bdd553e1b953ac0/external/local_config_cc/BUILD:47:19: in cc_toolchain_suite rule @local_config_cc//:toolchain: cc_toolchain_suite '@local_config_cc//:toolchain' does not contain a toolchain for cpu 'arm64-v8a'
ERROR: /home/user/.cache/bazel/_bazel_dtc/1098f9536915fc724bdd553e1b953ac0/external/local_config_cc/BUILD:47:19: Analysis of target '@local_config_cc//:toolchain' failed
ERROR: Analysis of target '//mediapipe/examples/android/src/java/com/google/mediapipe/apps/aar_example:mediapipe_face_detection.aar' failed; build aborted:
INFO: Elapsed time: 2.896s
INFO: 0 processes.
FAILED: Build did NOT complete successfully (95 packages loaded, 1368 targets configured)
currently loading: @com_google_protobuf//java/lite ... (3 packages)
如果添加多个android_sdk_repository/android_ndk_repository会出现如下错误,需要删除其他保留正确的一个
ERROR: Traceback (most recent call last):
File "/home/dtc/mediapipe/WORKSPACE", line 713, column 23, in <toplevel>
android_sdk_repository(name = "androidsdk", path = "/home/dtc/Android/Sdk")
Error in android_sdk_repository: Cannot redefine repository after any load statement in the WORKSPACE file (for repository 'androidsdk')
ERROR: Error computing the main repository mapping: error loading package 'external': Package 'external' contains errors
当可以正常编译后,发现依然会有问题,比如我们编译的apk,或者aar使用是就会出现crash,报错如下:
Process: com.google.mediapipe.apps.handtrackinggpu, PID: 17831
java.lang.UnsatisfiedLinkError: dlopen failed: library "libc++_shared.so" not found: needed by /data/app/~~yRuuvhZDkZlJ4RGPbw5Y5w==/com.google.mediapipe.apps.handtrackinggpu-9gfqnE8pNxbZ7m2zDu-vOA==/lib/arm64/libopencv_java4.so in namespace classloader-namespace
at java.lang.Runtime.loadLibrary0(Runtime.java:1077)
at java.lang.Runtime.loadLibrary0(Runtime.java:998)
at java.lang.System.loadLibrary(System.java:1656)
at com.google.mediapipe.apps.basic.MainActivity.<clinit>(MainActivity.java:59)
at java.lang.Class.newInstance(Native Method)
at android.app.AppComponentFactory.instantiateActivity(AppComponentFactory.java:95)
at androidx.core.app.CoreComponentFactory.instantiateActivity(CoreComponentFactory.java:41)
at android.app.Instrumentation.newActivity(Instrumentation.java:1274)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3533)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3793)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2211)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loopOnce(Looper.java:201)
at android.os.Looper.loop(Looper.java:288)
at android.app.ActivityThread.main(ActivityThread.java:7880)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:574)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1009)
报错信息比较明确就是libopencv_java4.so引用的libc++_shared.so找不到,试了很多方法编译opencv_java4.so都没解决,然后发现使用官方提供的demo发现使用的是java3.so, 于是重新拉取老版本mediapipe,也都会出现各种不同错误(各种环境版本差异过大,难以解决),还是决定在最后的版本上解决此问题,最后尝试修改opencv的版本,目前使用的版本是opencv4.10.0, 然后就根据opencv发布的4.xx版本中尝试,最后使用4.0.1版本问题被解决。opencv releases版本详情。
WORKSAPCE中作如下修改:
http_archive(
name = "android_opencv",
build_file = "@//third_party:opencv_android.BUILD",
strip_prefix = "OpenCV-android-sdk",
type = "zip",
- url = "https://github.com/opencv/opencv/releases/download/4.10.0/opencv-4.10.0-android-sdk.zip",
+ url = "https://github.com/opencv/opencv/releases/download/4.0.1/opencv-4.0.1-android-sdk.zip",
)
在更改opencv版本后,可能遇到download失败的情况,在重新使用bazel编译前最好clean一下:
bazel clean --expunge
在实际部署过程中还可能遇到其他问题,最后最后附上编译配置供参考:
mediapipe version: 0.10.16
bazel version : 6.4.0
opencv version : 4.0.1
python version: 3.10.12
gcc/g++ version : 12.3.0
sdk : 35
ndk : android-ndk-r26d
os : ubuntu22.04
如果想使用mediapipe老的解决方案可以参考,毕竟老的方案已经停止更新,但是实际效果并没有比新方案差,而且同样的场景老方法的性能明显更高,因此可以跑在性能不是那么强的设备上
有关其他关于opencv、bazel等安装配置请参开官方教程:MediaPipe安装