Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using NDK with STL in Android Studio gradle project

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
like image 792
xjossy Avatar asked Dec 25 '14 10:12

xjossy


People also ask

How do I add NDK to Gradle?

To include ndk-build projects in your Gradle build, you need to use Android Studio 2.2 and higher with Android plugin for Gradle 2.2. 0 and higher. To learn more about Android Studio's support for external native builds, read Add C and C++ Code to Your Project.

Is NDK necessary for Android Studio?

Prerequisite. To use Android NDK support in your application, you need to download the following three tools: LLDB: It is used by Android Studio to debug the native code present in your project. NDK: Native Development Kit(NDK) is used to code in C and C++ i.e. native languages for Android.

Which is better NDK or sdk?

It is important to mention that some Android Apps use NDK to achieve a specific functionality. That makes NDK and SDK somehow complementary in some cases. However, Android still recommends to only used NDK if you really need to.

Where do you put NDK?

Select the NDK (Side by side) checkbox and the checkboxes below it that correspond to the NDK versions you want to install. Android Studio installs all versions of the NDK in the android-sdk /ndk/ directory.


1 Answers

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
}
like image 161
nbtdev Avatar answered Sep 28 '22 01:09

nbtdev