Hi I'm making a android app in Android Studio (ver 2.3 - NDK SUPPORT)
I have a problem with importing library (dlib)
I downloaded source code from https://github.com/tzutalin/dlib-android and built it
I copied .so file(libandroid_dlib.so) to app/src/main/JniLibs/armeabi-v7a folder
I changed a CMakeLists.txt settings
I loaded library from MainActivity.java file
My code has no errors but when I import dlib like above, my app doesn't work anything !
Is there a missed step? THANK YOU SO MUCH IN ADVANCE
MainActivity.java
static {
System.loadLibrary("opencv_java3");
System.loadLibrary("android_dlib");
System.loadLibrary("imported-lib");
System.loadLibrary("native-lib");
}
CMakeLists.txt
set(pathOPENCV /Users/gicheonkang/OpenCV-android-sdk)
set(pathPROJECT /Users/gicheonkang/AndroidStudioProjects/HelloWorld)
set(pathLIBOPENCV_JAVA ${pathPROJECT}/app/src/main/JniLibs/${ANDROID_ABI}/libopencv_java3.so)
set(pathDLIB /Users/gicheonkang/dlib)
set(pathLIBDLIB ${pathPROJECT}/app/src/main/JniLibs/${ANDROID_ABI}/libandroid_dlib.so)
cmake_minimum_required(VERSION 3.4.1)
# CMAKE settings
set(CMAKE_VERBOSE_MAKEFILE on)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11")
# include *.cpp and *.h file
file(GLOB Library_SOURCES_FACEANALYSER src/main/cpp/FaceAnalyser/*.cpp)
file(GLOB Library_HEADERS_FACEANALYSER src/main/cpp/FaceAnalyser/*.h)
include_directories(${pathOPENCV}/sdk/native/jni/include)
include_directories(${pathDLIB}/dlib)
# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds it for you.
# Gradle automatically packages shared libraries with your APK.
add_library( # Sets the name of the library.
native-lib
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
# Associated headers in the same location as their source
# file are automatically included.
src/main/cpp/native-lib.cpp )
add_library( lib_opencv SHARED IMPORTED )
add_library( dlib SHARED IMPORTED )
add_library( imported-lib SHARED ${Library_SOURCES_FACEANALYSER} ${Library_HEADERS_FACEANALYSER})
set_target_properties(lib_opencv PROPERTIES IMPORTED_LOCATION ${pathLIBOPENCV_JAVA})
set_target_properties( dlib PROPERTIES IMPORTED_LOCATION ${pathLIBDLIB})
set_target_properties( imported-lib PROPERTIES LINKER_LANGUAGE CXX )
find_library( log-lib log )
find_library( android-lib android)
# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in the
# build script, prebuilt third-party libraries, or system libraries.
target_link_libraries( # Specifies the target library.
imported-lib
# Links the target library to the log library
# included in the NDK.
lib_opencv
#here is the problem
#dlib
)
target_link_libraries( # Specifies the target library.
native-lib
# Links the target library to the log library
# included in the NDK.
${log-lib}
${android-lib}
#imported-lib
lib_opencv
#here is the problem!!!!!!
#dlib
)
Related
I eventually cross-compiled a capstone library for android, but now I am having trouble linking it with my main .so library(native-lib).
I followed the steps shown in the official web site.
My CMakeList.txt is as below.
# Sets the minimum version of CMake required to build your native library.
# This ensures that a certain set of CMake features is available to
# your build.
cmake_minimum_required(VERSION 3.4.1)
# Specifies a library name, specifies whether the library is STATIC or
# SHARED, and provides relative paths to the source code. You can
# define multiple libraries by adding multiple add.library() commands,
# and CMake builds them for you. When you build your app, Gradle
# automatically packages shared libraries with your APK.
add_library( # Specifies the name of the library.
native-lib
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
src/main/cpp/native-lib.cpp )
find_library( # Defines the name of the path variable that stores the
# location of the NDK library.
log-lib
# Specifies the name of the NDK library that
# CMake needs to locate.
log )
# Links your native library against one or more other native libraries.
#target_link_libraries( # Specifies the target library.
# native-lib
#
# # Links the log library to the target library.
# ${log-lib} )
add_library( capstone
SHARED
IMPORTED )
set_target_properties( # Specifies the target library.
capstone
# Specifies the parameter you want to define.
PROPERTIES IMPORTED_LOCATION
# Provides the path to the library you want to import.
C:/Users/82102/AndroidStudioProjects/Android-Disassembler/capstone/${ANDROID_ABI}/libcapstone.so.5 )
target_link_libraries( native-lib capstone ${log-lib} ) #app-glue
include_directories( capstone/include/ )
The APK built has native-lib.so, but does not have 'libcapstone.soorlibcapstone.so.5`. So when I launch the APK, the error below happens.
2019-08-22 14:21:01.340 6122-6122/com.kyhsgeekcode.disassembler E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.kyhsgeekcode.disassembler, PID: 6122
java.lang.UnsatisfiedLinkError: dlopen failed: library "libcapstone.so.5" not found
at java.lang.Runtime.loadLibrary0(Runtime.java:1016)
at java.lang.System.loadLibrary(System.java:1669)
at com.kyhsgeekcode.disassembler.MainActivity.<clinit>(MainActivity.java:169)
at java.lang.Class.newInstance(Native Method)
I tried adding a jniLibs directory in the folder and adding the libcapstone.so.5, but the APK built still does not contain libcapstone.so.(5) and the same error happens.
How should I edit my CMakeList.txt to correctly link a custom prebuilt library to a main shared library?(native-lib.so)
I changed my CMakeLists.txt to link the capstone as a static library.
# Sets the minimum version of CMake required to build your native library.
# This ensures that a certain set of CMake features is available to
# your build.
cmake_minimum_required(VERSION 3.4.1)
# Specifies a library name, specifies whether the library is STATIC or
# SHARED, and provides relative paths to the source code. You can
# define multiple libraries by adding multiple add.library() commands,
# and CMake builds them for you. When you build your app, Gradle
# automatically packages shared libraries with your APK.
add_library( # Specifies the name of the library.
native-lib
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
src/main/cpp/native-lib.cpp )
find_library( # Defines the name of the path variable that stores the
# location of the NDK library.
log-lib
# Specifies the name of the NDK library that
# CMake needs to locate.
log )
# Links your native library against one or more other native libraries.
#target_link_libraries( # Specifies the target library.
# native-lib
#
# # Links the log library to the target library.
# ${log-lib} )
#add_library( capstone
# SHARED
# IMPORTED )
#set_target_properties( # Specifies the target library.
# capstone
#
# # Specifies the parameter you want to define.
# PROPERTIES IMPORTED_LOCATION#
#
# # Provides the path to the library you want to import.
# C:/Users/82102/AndroidStudioProjects/Android-Disassembler/capstone/${ANDROID_ABI}/libcapstone. )
target_link_libraries( native-lib C:/Users/82102/AndroidStudioProjects/Android-Disassembler/capstone/${ANDROID_ABI}/libcapstone.a ${log-lib} ) #app-glue
include_directories( capstone/include/ )
Hopefully, it worked!
I am getting null UnsatisfiedLinkError while building my app.
i am using openssl and some third party libraries to capture finger and verify them.
here is my cmake file
# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html
# Sets the minimum version of CMake required to build the native library.
cmake_minimum_required(VERSION 3.4.1)
# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.
add_library( # Sets the name of the library.
native-lib
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
src/main/cpp/native-lib.cpp )
add_library(openssl SHARED IMPORTED)
set_target_properties (openssl PROPERTIES IMPORTED_LOCATION ${PROJECT_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/lib/libcrypto.so )
set_target_properties (openssl PROPERTIES IMPORTED_LOCATION ${PROJECT_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/lib/libssl.so )
set_target_properties (openssl PROPERTIES IMPORTED_LOCATION ${PROJECT_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/lib/libidkit.so )
set_target_properties (openssl PROPERTIES IMPORTED_LOCATION ${PROJECT_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/lib/libjnidispatch.so )
set_target_properties (openssl PROPERTIES IMPORTED_LOCATION ${PROJECT_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/lib/libMSO100.so )
set_target_properties (openssl PROPERTIES IMPORTED_LOCATION ${PROJECT_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/lib/libNativeMorphoSmartSDK_6.13.2.0-4.1.so )
# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.
include_directories(openssl-armeabi-v7a/include/)
find_library( # Sets the name of the path variable.
log-lib
# Specifies the name of the NDK library that
# you want CMake to locate.
log )
# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.
target_link_libraries( # Specifies the target library.
native-lib
${CMAKE_CURRENT_SOURCE_DIR}/openssl-armeabi-v7a/lib/libcrypto.a
${CMAKE_CURRENT_SOURCE_DIR}/openssl-armeabi-v7a/lib/libssl.a
# Links the target library to the log library
# included in the NDK.
${log-lib} )
is this correct way of loading .so file in cmake or is there any method to load .so files . help with this issue
Each .so file that you list is a separate library. You must give each a separate name, e.g.
add_library(foo SHARED IMPORTED)
set_target_properties (foo PROPERTIES IMPORTED_LOCATION ${PROJECT_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/lib/libcrypto.so )
add_library(prebuilt_ssl SHARED IMPORTED)
set_target_properties (prebuilt_ssl PROPERTIES IMPORTED_LOCATION ${PROJECT_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/lib/libssl.so )
add_library(idkit SHARED IMPORTED)
set_target_properties (idkit PROPERTIES IMPORTED_LOCATION ${PROJECT_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/lib/libidkit.so )
and so on. As you can see above, the logical name is not necessarily related to the file name.
If your native-lib depends on some of them, you can use these names:
target_link_libraries( # Specifies the target library.
native-lib foo prebuilt_ssl
# Links the target library to the log library
# included in the NDK.
log )
Note that with NDK, you don't need find_library to add dependency on the android log library.
There is some self-contradiction in the CMake script that you posted. You should use OpenSSL libraries either as shared objects (as libssl.so), or static libraries (as libssl.a), but not both. It's OK to mix libjnidispatch.so together with libssl.a, but not libcrypto.a and libssl.so.
Question Update:
Linker error
I installed Intel OpenCL SDK with Android Support on dev pc.
Difficulty building Android Studio Solution with C++ support using CMakelists.txt
OpenCL & OpenCV in Android Studio using CMakeLists.txt
CMakeLists.txt:
# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html
#Added 2 path definitions to support 20160825 additions
set(pathToProject C:/Users/eogha/Desktop/HelloOpenCV)
set(pathToOpenCv C:/OpenCV-3.1.0-android-sdk)
set(pathToOpenCL "C:/Program Files (x86)/Intel/OpenCL SDK/5.3")
# Sets the minimum version of CMake required to build the native library.
cmake_minimum_required(VERSION 3.4.1)
set(CMAKE_VERBOSE_MAKEFILE on)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11")
#Addition suggested by Bruno Alexandre Krinski 20160825
include_directories(${pathToOpenCv}/sdk/native/jni/include )
include_directories(${pathToOpenCL}/include)
# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.
add_library( native-lib SHARED src/main/cpp/native-lib.cpp )
#Addition suggested by Bruno Alexandre Krinski 20160825
add_library( lib_opencv SHARED IMPORTED )
#Addition suggested by Bruno Alexandre Krinski 20160825
set_target_properties( lib_opencv PROPERTIES IMPORTED_LOCATION
${pathToProject}/app/src/main/jniLibs/${ANDROID_ABI}/libopencv_java3.so )
add_library( lib_opencl SHARED IMPORTED )
set_target_properties( lib_opencl PROPERTIES IMPORTED_LOCATION "C:/Program
Files (x86)/Intel/OpenCL SDK/5.3/lib/android64/libOpenCL.so" )
# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.
find_library( # Sets the name of the path variable.
log-lib
# Specifies the name of the NDK library that
# you want CMake to locate.
log )
# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.
target_link_libraries( native-lib $\{log-lib} lib_opencv lib_opencl )
Android Studio Error: linker command failed with exit code 1 (use -v to see invocation)
Finally !! The OpenCL Shared Object library seems to be imported into the android project structure. But now I have this linker error. How do I resolve this in Windows 10 ??
I am trying to implement the lib ogg and vorbis on an Android project using CMake. The code is based on this repository. I was trying to build the source following the google's recommendation and a example from OpenCV. The compilation works but the .so file is not attached to the APK. Any idea to solve it?
cmake_minimum_required(VERSION 3.4.1)
set(CMAKE_VERBOSE_MAKEFILE on)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11")
set(root_DIR ${CMAKE_SOURCE_DIR})
include_directories(
${root_DIR}/include
${root_DIR}/include/ogg
${root_DIR}/include/stream
${root_DIR}/include/vorbis
${root_DIR}/libvorbis
${root_DIR}/libogg
)
file(GLOB_RECURSE libogg
"${root_DIR}/libogg/*.c"
"${root_DIR}/libogg/*.cpp"
)
file(GLOB_RECURSE libvorbis
"${root_DIR}/libvorbis/*.c"
"${root_DIR}/libvorbis/*.cpp"
)
file(GLOB_RECURSE org_xiph_vorbis_decode
"${root_DIR}/libvorbis/*.c"
"${root_DIR}/libvorbis/*.cpp"
)
file(GLOB_RECURSE org_xiph_vorbis_encoder
"${root_DIR}/libvorbis/*.c"
"${root_DIR}/libvorbis/*.cpp"
)
# add our library
add_library(libogg STATIC ${libogg})
add_library(libvorbis STATIC ${libvorbis})
add_library(org_xiph_vorbis_decoder STATIC ${org_xiph_vorbis_decode})
add_library(org_xiph_vorbis_encoder STATIC ${org_xiph_vorbis_encoder})
# configure import libs
set(lib_DIR ${root_DIR}/src/main/jniLibs)
# Add the shared library
add_library(imported-lib SHARED IMPORTED)
# Directory for compiled binary
set(target_DIR ${lib_DIR}${ANDROID_ABI}/imported-lib.so)
# Link libraries on build directory
set_target_properties(imported-lib PROPERTIES IMPORTED_LOCATION ${target_DIR})
find_library(log-lib log)
find_library(z-lib z)
target_link_libraries(
# Specifies the target library.
libvorbis
libogg
imported-lib
org_xiph_vorbis_encoder_VorbisEncoder
org_xiph_vorbis_decoder_VorbisDecoder
# Links the target library to the log library
# included in the NDK.
${z-lib}
${log-lib}
)
The source code is here: https://github.com/ppamorim/jni_vorbis
Thanks, I really appreciate any help.
So, I have android project with c++ support. There are some undefined references in my project when trying to build c++ code and, after answering my first question, what is the problem with it here link, I can't find a way to do it.
If you have Cmake that looks like this:
cmake_minimum_required(VERSION 3.4.1)
# OpenCV stuff
include_directories(D:\\opencv-3.2.0-android-sdk\\OpenCV-android-sdk\\sdk\\native\\jni\\include)
add_library( lib_opencv SHARED IMPORTED )
set_target_properties(lib_opencv PROPERTIES IMPORTED_LOCATION ${CMAKE_CURRENT_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/libopencv_java3.so)
add_compile_options(-std=c++11)
# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.
add_library( # Sets the name of the library.
native-lib
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
src/main/cpp/native-lib.cpp)
# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.
find_library( # Sets the name of the path variable.
log-lib
# Specifies the name of the NDK library that
# you want CMake to locate.
log )
# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in the
# build script, prebuilt third-party libraries, or system libraries.
target_link_libraries( # Specifies the target library.
native-lib
-ljnigraphics
lib_opencv
# Links the target library to the log library
# included in the NDK.
${log-lib} )
just change the part to be like this:
target_link_libraries( # Specifies the target library.
native-lib
-ljnigraphics
-landroid # Add this.
lib_opencv
# Links the target library to the log library
# included in the NDK.
${log-lib} )