gocv icon indicating copy to clipboard operation
gocv copied to clipboard

Can't load Gomobile library that depends on GoCV on Android

Open johnb8 opened this issue 9 months ago • 0 comments

Description

Loading an Android library built with Gomobile that depends on Gocv fails with java.lang.UnsatisfiedLinkError: dlopen failed: TLS symbol "(null)" in dlopened "/data/app/~~w-ekDiUxMA_lJMAENzg1zQ==/com.john.ocv-qdxRrd5byUKkqZSi1SukYQ==/base.apk!/lib/x86_64/libgojni.so" referenced from "/data/app/~~w-ekDiUxMA_lJMAENzg1zQ==/com.john.ocv-qdxRrd5byUKkqZSi1SukYQ==/base.apk!/lib/x86_64/libgojni.so" using IE access model.

The only other similar issue I could find is from mesa https://gitlab.freedesktop.org/mesa/mesa/-/issues/5218. I've tried adding the --fno-emulated-tls flag that seems to have fixed it for them in the OpenCV build and in CGO_CFLAGS but it made no difference. I also tried the various --ftls-model options (https://www.intel.com/content/www/us/en/docs/cpp-compiler/developer-guide-reference/2021-8/ftls-model.html) but those made no difference either. I've tried both statically and dynamically linking to OpenCV too with the same result.

I'm not really sure what to try next. It seems to be something specific to the combination of Go, OpenCV, and Android because the same library built for iOS works no problem and I'm able build a Rust library that links to OpenCV that runs fine on Android too.

Steps to Reproduce

  1. Build the library with the instructions in the example repo
  2. Add the library to an Android project and run it
  3. It will crash with the dlopen error

Your Environment

I have an example repo here https://github.com/johnb8/gocv-android with a docker container that will build the gomobile library.

  • Operating System and version: Building on Debian 12
  • OpenCV version used: 4.8.0
  • How did you install OpenCV? With build/install-opencv.sh in the example repo
  • GoCV version used: 0.34
  • Go version: 1.21.0

johnb8 avatar Sep 10 '23 22:09 johnb8