Search code examples
androidc++bazel

Bazel Android c++_shared/c++_static issues


We have a project that uses a library that is built on top of Google's Mediapipe, which is built using the Bazel build system.

The project itself is an Android Native Library, built using Gradle with CMake

        externalNativeBuild {
            cmake {
                cppFlags "-std=c++17 -fopenmp -static-openmp -fexceptions -frtti -Wl,-s -Wno-unused-command-line-argument"
                arguments "-DANDROID_STL=c++_shared", "-DOpenCV_DIR=${opencvDir}", "-DANDROID_ARM_NEON=TRUE"
            }
        }

So we end up with 2 (or more later, also dependent on OpenCV for example) shared object libraries - the actual SDK & the Mediapipe project.

We're seeing issues that are similar to this, which lead me to look into the runtime part of our project.

E/libc++abi: terminating with uncaught exception of type std::bad_cast: std::bad_cast

I saw this comment on that issue thread, and adding

System.loadLibrary("c++_shared");

Solved the crash.

However, this is not a practical solution as the project we're building would provide a native SDK in the form of multiple .so files and I wouldn't want to force our clients to have to explicitly load the shared runtime library before using our library.

The gradle library has "-DANDROID_STL=c++_shared" flag, so this is using the shared one, but I couldn't find any way to compile Mediapipe (with Bazel) using c++_shared. I couldn't find any reference to using shared runtime when compiling Bazel projects (except for this, which isn't exactly relevant and the solution didn't help me)

We might be able to work around this by setting -DANDROID_STL=c++_static, but this has other issues, mainly, it violates Android's guidelines for using multiple shared libraries, though it might be possible for for middleware vendors

So the question is,

  • Is it possible to build Mediapipe (or any other Bazel based) using c++_shared Android STL
  • If not, are there any other options to solve the runtime conflicts
  • Is it even a runtime conflict or something else?

Solution

  • I managed to get it working as suggested by using c++_static on all of our shared objects (SDK, Mediapipe, OpenCV and others)