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.
Related
sorry bad english
cloned and compiled this project in Android Studio:
https://github.com/Pangu-Immortal/KeepAlivePerfect/
During compile, got the following error (using ndk 24.0.8215888):
KeepAlivePerfect/library/src/main/cpp/binder_libs/armeabi-v7a/libbinder.so: invalid sh_info in symbol table
tested both in linux and windows. Same error.
plz help
Finally could compile with changing SHARED to STATIC in CMakeLists.txt. And some more config.
Here are the changes:
# Sets the minimum version of CMake required to build the native library.
cmake_minimum_required(VERSION 3.4.1)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include/)
add_compile_options(-fno-rtti -O3 -v)
message(###${CMAKE_CURRENT_SOURCE_DIR})
link_directories(binder_libs/${CMAKE_ANDROID_ARCH_ABI})
aux_source_directory(. SRC_LIST) # 搜索当前目录下的所有.cpp文件
add_library( # Sets the name of the library.
keep_alive
# Sets the library as a shared library.
STATIC # Changed from SHARED to STATIC
# 提供源文件的相对路径。
${SRC_LIST} main.cpp art.cpp)
# More Config
set_target_properties(keep_alive PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/binder_libs/${ANDROID_ABI}/libutils.so)
set_target_properties(keep_alive PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/binder_libs/${ANDROID_ABI}/libc.so)
set_target_properties(keep_alive PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/binder_libs/${ANDROID_ABI}/libcutils.so)
set_target_properties(keep_alive PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/binder_libs/${ANDROID_ABI}/libutils.so)
# The END
find_library(log-lib log)
target_link_libraries(
keep_alive
${log-lib}
)
More information in an issue in main project:
https://github.com/Pangu-Immortal/KeepAlivePerfect/issues/8#issuecomment-1107427875
I got some C code I have to integrate into my Android project. It depends on a library (.so), and I also have the .h files.
I copied the libs and include directories into the project's cpp package to make them easier to find.
To make it easier I tried starting with the HelloJNI project Android Studio offers and followed the instructions here.
This is the CMakeLists.txt:
#given from HelloJNI
cmake_minimum_required(VERSION 3.4.1)
add_library(hello-jni SHARED
hello-jni.c)
# Include libraries needed for hello-jni lib
target_link_libraries(hello-jni
android
log)
#my own additions now:
add_library( # Specifies the name of the library.
libgdndk
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
IMPORTED)
set_target_properties( # Specifies the target library.
libgdndk
# Specifies the parameter you want to define.
PROPERTIES IMPORTED_LOCATION
# Provides the path to the library you want to import.
${CMAKE_BINARY_DIR}/libs/${ANDROID_ABI}/libgdndk.so )
include_directories(${CMAKE_BINARY_DIR}/inc/)
target_link_libraries( hello-jni libgdndk app-glue ${libgdndk} )
This results in an error:
ninja: error: 'libs/armeabi-v7a/libgdndk.so', needed by 'C:/workspace/android/HelloJNI/app/build/intermediates/cmake/arm7/debug/obj/armeabi-v7a/libhello-jni.so', missing and no known rule to make it
You seem to be linking your imported library to the hello-jni target twice. Also, using ${} in the target_link_libraries() command for the libgdndk library is not necessary. You have already defined a CMake target libgdndk for the library, so you can just do this:
target_link_libraries( hello-jni libgdndk app-glue )
I finally got some help from the publisher of the lib, the (working) result looks quite similar to the code i tried, so maybe it was just the location of the library that was at fault.
add_library( gdndk SHARED IMPORTED )
include_directories( $ENV{userprofile}/AppData/Local/Android/sdk/extras/blackberry/dynamics_sdk/sdk/libs/handheld/gd/inc)
set_target_properties(gdndk PROPERTIES IMPORTED_LOCATION $ENV{userprofile}/AppData/Local/Android/sdk/extras/blackberry/dynamics_sdk/sdk/libs/handheld/gd/libs/${ANDROID_ABI}/libgdndk.so )
target_link_libraries( hello-jni gdndk )
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 ??
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
)