title: Bazel Android c++_shared/c++_static issues
body: <p>We have a project that uses a library that is built on top of Google's <a href="https://github.com/google/mediapipe" rel="nofollow noreferrer">Mediapipe</a>, which is built using the <code>Bazel</code> build system.</p>
  <p>The project itself is an Android Native Library, built using Gradle with CMake</p>
  <pre><code>        externalNativeBuild {
              cmake {
                  cppFlags &quot;-std=c++17 -fopenmp -static-openmp -fexceptions -frtti -Wl,-s -Wno-unused-command-line-argument&quot;
                  arguments &quot;-DANDROID_STL=c++_shared&quot;, &quot;-DOpenCV_DIR=${opencvDir}&quot;, &quot;-DANDROID_ARM_NEON=TRUE&quot;
              }
          }
  </code></pre>
  <p>So we end up with 2 (or more later, also dependent on OpenCV for example) shared object libraries - the actual <code>SDK</code> &amp; the <code>Mediapipe</code> project.</p>
  <p>We're seeing issues that are similar to <a href="https://github.com/facebook/react-native/issues/29377" rel="nofollow noreferrer">this</a>, which lead me to look into the runtime part of our project.</p>
  <pre><code>E/libc++abi: terminating with uncaught exception of type std::bad_cast: std::bad_cast
  </code></pre>
  <p>I saw <a href="https://github.com/facebook/react-native/issues/29377#issuecomment-855347478" rel="nofollow noreferrer">this</a> comment on that issue thread, and adding</p>
  <pre><code>System.loadLibrary(&quot;c++_shared&quot;);
  </code></pre>
  <p>Solved the crash.</p>
  <p>However, this is not a practical solution as the project we're building would provide a native SDK in the form of multiple <code>.so</code> files and I wouldn't want to force our clients to have to explicitly load the shared runtime library before using our library.</p>
  <p>The gradle library has <code>&quot;-DANDROID_STL=c++_shared&quot;</code> flag, so this is using the shared one, but I couldn't find any way to compile Mediapipe (with Bazel) using <code>c++_shared</code>. I couldn't find any reference to using shared runtime when compiling Bazel projects (except for <a href="https://github.com/bazelbuild/bazel/issues/7475" rel="nofollow noreferrer">this</a>, which isn't exactly relevant and the solution didn't help me)</p>
  <p>We might be able to work around this by setting <code>-DANDROID_STL=c++_static</code>, but this has other issues, mainly, it violates <a href="https://developer.android.com/ndk/guides/cpp-support#shared_runtimes" rel="nofollow noreferrer">Android's guidelines</a> for using multiple shared libraries, though it might be possible for for <a href="https://developer.android.com/ndk/guides/middleware-vendors#using_the_stl" rel="nofollow noreferrer">middleware vendors</a></p>
  <p>So the question is,</p>
  <ul>
  <li>Is it possible to build Mediapipe (or any other Bazel based) using <code>c++_shared</code> Android STL</li>
  <li>If not, are there any other options to solve the runtime conflicts</li>
  <li>Is it even a runtime conflict or something else?</li>
  </ul>