Java – How to import shared libraries in Cmake for Android

How to import shared libraries in Cmake for Android… here is a solution to the problem.

How to import shared libraries in Cmake for Android

I’ve been trying to import a project for Android using CMake, but when I try to import these libraries and compile and execute the program on Android using the terminal, I get the following error:

D/AndroidRuntime( 6162): Shutting down VM
E/AndroidRuntime( 6162): FATAL EXCEPTION: main
E/AndroidRuntime( 6162): Process: org.abc.project, PID: 6162
E/AndroidRuntime( 6162): **java.lang.UnsatisfiedLinkError: dlopen failed: library "libcsoundandroid.so" not found**
E/AndroidRuntime( 6162):    at java.lang.Runtime.loadLibrary(Runtime.java:371)
E/AndroidRuntime( 6162):    at java.lang.System.loadLibrary(System.java:988)
E/AndroidRuntime( 6162):    at org.qtproject.qt5.android.bindings.QtActivity.loadApp

My CMakeLists.txt is:

add_library(csoundandroid SHARED IMPORTED)
set_property(TARGET csoundandroid PROPERTY IMPORTED_LOCATION /home/ayush/csound-android-6.07.0/CsoundForAndroid/CsoundAndroid/src/main/jniLibs/armeabi/)

add_library(sndfile SHARED IMPORTED)
set_property(TARGET sndfile PROPERTY IMPORTED_LOCATION /home/ayush/csound-android-6.07.0/CsoundForAndroid/CsoundAndroid/src/main/jniLibs/armeabi/)

add_library(c++_shared SHARED IMPORTED)
set_property(TARGET c++_shared PROPERTY IMPORTED_LOCATION /home/ayush/csound-android-6.07.0/CsoundForAndroid/CsoundAndroid/src/main/jniLibs/armeabi/)
set(LIBS1 libcsoundandroid.so)
set(LIBS2 libsndfile.so)
set(LIBS3 libc++_shared.so)
link_directories(/home/ayush/csound-android-6.07. 0/CsoundForAndroid/CsoundAndroid/src/main/jniLibs/armeabi)

include_directories(/home/ayush/csound/include)
include_directories(/home/ayush/csound/android/CsoundAndroid/jni/)
target_link_libraries(abc ${LIBS1} ${LIBS2} ${LIBS3} )

Here abc is the generated executable. All the libraries I listed there are in the same place. Can you help me find out what the error is? Any kind of help would be appreciated.

Solution

Property IMPORTED_LOCATION should contain the full path to the library file. This is explicitly written in the documentation as that property.

To link with an imported library, use the target name, not the library file:

# Correctly set property for imported library
set_property(TARGET csoundandroid PROPERTY IMPORTED_LOCATION
    /home/ayush/csound-android-6.07.0/(...) /armeabi/libcsoundandroid.so
)

# And correctly link with it
set(LIBS1 csoundandroid)

target_link_libraries(abc ${LIBS1})

Related Problems and Solutions