Linking to the C++ runtime with a Flutter app on Android

  android, android-ndk, c++, flutter, gradle

I’m trying to make use of a prebuilt dynamic library in a Flutter (v2.2.1) Android application.

I’ve placed the compiled libs into the android/app/src/main/jniLibs folder under target specific sub-folders, and they’re picked up correctly by the build system and I can load the library with DynamicLibrary.open().

The library is written with Rust, but it makes use of a C++ library and I’m getting the error dlopen failed: cannot locate symbol "__cxa_pure_virtual" referenced by...

So my question is, how exactly do I tell the build system to link against the C++ runtime?

The Android ‘C++ library support’ page explains that the default runtime for ndk-build is none, and that I should add APP_STL := c++_shared to my Application.mk file.

Up until this point I didn’t have an Application.mk file, but I could see that adding one might make sense, so I added the APP_STL definition to android/app/jni/Application.mk, and after some reading I decided that I should also add an Android.mk file (I don’t know if it’s actually required), with the following contents:

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)
LOCAL_MODULE := foo-prebuilt
LOCAL_SRC_FILES := $(TARGET_ARCH_ABI)/libfoo.so
include $(PREBUILT_SHARED_LIBRARY)

From what I understand these then need to be picked up by Gradle, so in android/app/build.gradle I’ve added:

...

android {
    ...

    externalNativeBuild {
        ndkBuild {
            path 'jni/Android.mk'
            path 'jni/Application.mk'
        }
    }
}

Unfortunately this doesn’t have any effect (I get the same error, and inspecting the APK shows that libc++_shared.so hasn’t been included). I’m at the point where I’m thinking there’s surely a simpler way to tell the build system to link against libc++_shared.so?

If I’m going down the right path, any ideas for why isn’t my APP_STL definition being applied?

Source: Windows Questions C++

LEAVE A COMMENT