Using NDK with STL in Android Studio gradle projec

2019-04-28 11:37发布

I have a trouble with linking stlport into gradle project in Android Studio.

Eclipse Android project with using NDK migrates into Android Studio.

The project uses STL and I have android.mk file with contents

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE    := MyProject
LOCAL_SRC_FILES := jniapi.cpp renderer.cpp
LOCAL_LDLIBS    := -llog -landroid -lEGL -lGLESv1_CM -ljnigraphics

include $(BUILD_SHARED_LIBRARY)

It seems gradle to ignore .mk file, and I added the folowing code into build.gradle file:

ndk {
   moduleName "MyProject"
   stl "stlport_shared"
   ldLibs "log", "EGL", "android", "jnigraphics", "GLESv1_CM"
   //No equivalent for the "include $(BUILD_SHARED_LIBRARY)" here
}

After this gradle building became successful, but running the application on device causes an error:

27446-27446/com.example.test E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.UnsatisfiedLinkError: Cannot load library: soinfo_link_image(linker.cpp:1635): could not load library "libstlport_shared.so" needed by "libMyProject.so"; caused by load_library(linker.cpp:745): library "libstlport_shared.so" not found

2条回答
放荡不羁爱自由
2楼-- · 2019-04-28 12:14

I think the newer way to do this is to use APP_STL in your Application.mk, something like this:

APP_STL := c++_shared
APP_ABI := armeabi-v7a
NDK_TOOLCHAIN_VERSION := clang

See the official documentation here: https://developer.android.com/ndk/guides/application_mk.html

查看更多
ら.Afraid
3楼-- · 2019-04-28 12:25

You need to load the stlport shared library manually in your Java code if you are using the shared variant. If you do not need the shared variant, specify stlport_static instead:

ndk {
    moduleName "MyProject"
    stl "stlport_static"
    ldLibs "log", "EGL", "android", "jnigraphics", "GLESv1_CM"
    //No equivalent for the "include $(BUILD_SHARED_LIBRARY)" here
}
查看更多
登录 后发表回答