How to add prebuilt *.so libraries in android studio? - android

I am trying to integrate the Hyperledger indy SDK. However, when running my code I get the error
E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.UnsatisfiedLinkError: dlopen failed: library "libgnustl_shared.so" not found
at java.lang.Runtime.loadLibrary0(Runtime.java:1016)
at java.lang.System.loadLibrary(System.java:1657)
I am trying to follow the documentation provided in the project repo. I tried using the sample project on this blog .
I was able to build the *.so libraries under a linux virtual machine, the copied the built files in my android studio project on windows.
I added the files inside my project's jniLibs forlder for each architecture.
Added the code to load the library inside my mainActivity
static{
System.loadLibrary("indy");
}
Tried creating a CMake file
cmake_minimum_required(VERSION 3.4.1)
add_library(indy SHARED IMPORTED)
include_directories(src/main/jniLibs/${ANDROID_ABI}/include)
My gradle file includes:
android{
defaultconfig{
...
ndk{
moduleName "indy"
abiFilters 'armeabi-v7a'
}
}
...
sourceSets {
main {
jniLibs.srcDir 'src/main/jniLibs'
}
}
externalNativeBuild {
cmake {
path file('../CMakeLists.txt')
}
}
}
Still, keep on getting the same error when I launch the app.
I am aware that the bash script that builds the libraries on linux uses the android-ndk-r16b-linux-x86_64 tools so I tried downgrading my ndk in android studio to use the same version but had no luck.
The output of the build script is
include/
indy_anoncreds.h
indy_core.h
...
lib/
libindy.a
libindy.so
libindy_shared.so
How can I use this libraries in my android studio project?

The issue is mainly related to the nature of libraries. Libraries are dynamic in Android and needs to be linked at runtime.
libindy.so depends on stl, openssl, libsodium and libzmq.
You will find libgnustl_shared.so in NDK.
All the other needed prebuilt libraries are also available here.
What you need to do is make sure these libraries are present in the jniLibs folder and load these in order libraries before libindy.
System.loadLibrary("libgnustl_shared");
.
.
System.loadLibrary("indy");
Alternate approach:
There is a subproject in Indy where we are using libindy as dependency and we try to create a one fat dynamic library which has all the dependencies.
Link
If you follow the steps like vcx you dont have to have all the defendant l libraries in jniLibs as they will be already part of final .so file
The command which make one fat dynamic library with all the symbols and dependencies is this (from the link pasted above)
${LIBVCX}/target/${CROSS_COMPILE}/release/libvcx.a \
${TOOLCHAIN_DIR}/sysroot/usr/${NDK_LIB_DIR}/libz.so \
${TOOLCHAIN_DIR}/sysroot/usr/${NDK_LIB_DIR}/libm.a \
${TOOLCHAIN_DIR}/sysroot/usr/${NDK_LIB_DIR}/liblog.so \
${LIBINDY_DIR}/libindy.a \
${TOOLCHAIN_DIR}/${CROSS_COMPILE_DIR}/${NDK_LIB_DIR}/libgnustl_shared.so \
${OPENSSL_DIR}/lib/libssl.a \
${OPENSSL_DIR}/lib/libcrypto.a \
${SODIUM_LIB_DIR}/libsodium.a \
${LIBZMQ_LIB_DIR}/libzmq.a \
${TOOLCHAIN_DIR}/${CROSS_COMPILE_DIR}/${NDK_LIB_DIR}/libgnustl_shared.so -Wl,--no-whole-archive -z muldefs

Related

Cannot load prebuilt OpenSSL library in Android project

I'm trying to link the OpenSSL .so prebuilt files so I can use it in my native C files. In my project, I've created native_hello.c file and it works fine, the C code runs, but when I'm trying to
System.loadLibrary("crypto");
System.loadLibrary("ssl");
in my java code to load the OpenSSL .so files, the app crashes with error
java.lang.UnsatisfiedLinkError: dlopen failed: library "libcrypto.so" not found
I went ahead and decompiled my source apk file, and sure enough there were no libcrypto.so or libssl.so files in lib folder.
I'm using CMake for linking native files:
CMakeLists.txt
cmake_minimum_required(VERSION 3.6.0)
set(distribution_DIR ${CMAKE_CURRENT_SOURCE_DIR}/src/main)
add_library(lib_crypto SHARED IMPORTED)
set_target_properties(lib_crypto PROPERTIES IMPORTED_LOCATION ${distribution_DIR}/jniLibs/${ANDROID_ABI}/lib/libcrypto.so)
# add the open ssl lib
add_library(lib_ssl SHARED IMPORTED)
set_target_properties(lib_ssl PROPERTIES IMPORTED_LOCATION ${distribution_DIR}/jniLibs/${ANDROID_ABI}/lib/libssl.so)
add_library(native-test SHARED
${distribution_DIR}/jni/native_hello.c)
target_include_directories(native-test PRIVATE ${distribution_DIR}/jniLibs/${ANDROID_ABI}/include)
target_link_libraries(native-test
lib_crypto
lib_ssl)
In related questions one of the solutions is to use sourceSets in build.gradle but with the following code snippet the .so files are still missing:
sourceSets {
main {
// let gradle pack the shared library into apk
jniLibs.srcDirs = ['src/main/jniLibs']
}
}
I think that the problem is pretty easy, but I could not figure out how to solve it. Any help would be appreciated
Turned out that structure of my jniLibs was something like:
arm64-v8a
include
...
lib
libcrypto.so
libssl.so
x86
include
...
lib
...
opposed to:
arm64-v8a
include
...
libcrypto.so
libssl.so
x86
include
...
...
Which solves initial problem.

Building c++ projects which have cmake build files for Android using NDK

I have to build 2 separate C++ projects which have Cmake build files setup for different platforms. I want to build them both for Android using NDK so that I can use them as prebuilt libs in Android Studio.
How do I build them for Android using NDK to generate a .a/.so for Arm architectures? Can I do it using cmake itself? Please provide detailed steps
Finally when I have 2 libraries, how do I integrate to Android Studio?
I kind of learnt how to create Android.mks for prebuilt libraries from this link
Using Pre-built Shared Library in Android Studio
But my lib2 depends on lib1 for both compilation and running. Jni code will depend on the combined library of both lib2 and lib1
I am new to NDK. So please provide detailed answers
Most likely, the CMake scripts that work for other platforms will require some changes for Android. Also, we often need special treatment for external dependencies, e.g. if we want CMake to find the correct version of boost.
But the main skeleton of CMakeLists.txt should be a good start. You can run CMake 'manually' for your libraries:
cmake \
-DCMAKE_TOOLCHAIN_FILE=${NDK_ROOT}/build/cmake/android.toolchain.cmake \
-DANDROID_NDK=${NDK_ROOT} \
-DANDROID_ABI=armeabi-v7a \
-DANDROID_PLATFORM=android-19 \
-DANDROID_STL=c++_shared \
-DCMAKE_LIBRARY_OUTPUT_DIRECTORY=${LIB1_DIRECTORY}/libs/armeabi-v7a \
${LIB1_DIRECTORY}
In the main CMakeLists.txt that builds your JNI wrapper, you can add
add_library(lib1 SHARED IMPORTED)
set_target_properties(lib1 PROPERTIES IMPORTED_LOCATION ${LIB1_DIRECTORY}/libs/armeabi-v7a/lib1.so )
target_link_libraries(jni_wrapper lib1 … log)
Android Studio will not build lib1.so for you, but it will pick it from the correct location and pack it into the APK.
Same trick with IMPORTED will provide lib1 for build of lib2 if the CMake script does not already handle this dependency.

How to integrate OpenCV into Qt Creator Android project

I use Qt Creator to compile an Android application. I needed to integrate OpenCV into it, and it took me half a day to configure it properly, so I want to document the steps I took here, in case somebody else ever has to do it.
Edit: For OpenCV 4.x see the answers below. My answer was tested on OpenCV 2.4 only.
Original answer:
First, I downloaded OpenCV-2.4.10-android-sdk, and put into my project directory. It contains static libraries, and link order matters for static libraries for GCC. So you need to order them just so. This is how my .pro file looked in the end ($$_PRO_FILE_PWD_ refers to the project directory):
INCLUDEPATH += "$$_PRO_FILE_PWD_/OpenCV-2.4.10-android-sdk/sdk/native/jni/include"
android {
LIBS += \
-L"$$_PRO_FILE_PWD_/OpenCV-2.4.10-android-sdk/sdk/native/3rdparty/libs/armeabi-v7a"\
-L"$$_PRO_FILE_PWD_/OpenCV-2.4.10-android-sdk/sdk/native/libs/armeabi-v7a"\
-llibtiff\
-llibjpeg\
-llibjasper\
-llibpng\
-lIlmImf\
-ltbb\
-lopencv_core\
-lopencv_androidcamera\
-lopencv_flann\
-lopencv_imgproc\
-lopencv_highgui\
-lopencv_features2d\
-lopencv_calib3d\
-lopencv_ml\
-lopencv_objdetect\
-lopencv_video\
-lopencv_contrib\
-lopencv_photo\
-lopencv_java\
-lopencv_legacy\
-lopencv_ocl\
-lopencv_stitching\
-lopencv_superres\
-lopencv_ts\
-lopencv_videostab
ANDROID_PACKAGE_SOURCE_DIR=$$_PRO_FILE_PWD_/android
}
After that the project will compile but it will fail to run with the error
E/AndroidRuntime(11873): java.lang.UnsatisfiedLinkError: Cannot load library: link_image[1891]: 176 could not load needed library 'libopencv_java.so' for 'libMyProject.so' (load_library[1093]: Library 'libopencv_java.so' not found)
To overcome this, you need to add libopencv_java.so to your APK, and then manually load it from QtActivity.java. That's what the ANDROID_PACKAGE_SOURCE_DIR=$$_PRO_FILE_PWD_/android line at the end was for. Now you need to place libopencv_java.so here:
project_root/android/libs/armeabi-v7a/libopencv_java.so
project_root/android/src/org/qtproject/qt5/android/bindings/QtActivity.java
You can get QtActivity.java from the Android target build directory, in my case the full path was c:\Workspace\build-MyProject-Android_for_armeabi_v7a_GCC_4_9_Qt_5_4_0-Debug\android-build\src\org\qtproject\qt5\android\bindings\QtActivity.java, and just copy it.
Then you find those lines in it:
// now load the application library so it's accessible from this class loader
if (libName != null)
System.loadLibrary(libName);
And load libopencv_java.so before them, so they become:
// This is needed for OpenCV!!!
System.loadLibrary("opencv_java");
// now load the application library so it's accessible from this class loader
if (libName != null)
System.loadLibrary(libName);
Note that you pass opencv_java to System.loadLibrary(), even though the file is libopencv_java.so.
Edit: I forgot to mention, but I already had installed OpenCV Manager on my phone when trying to run one of the samples that come with OpenCV-2.4.10-android-sdk, so I don't know if it's needed or not. In any event, keep it in mind, if it fail even after my steps, you might need to download OpenCV Manager (it's available on the Google Store).
Edit 2: I'm using adt-bundle-windows-x86-20140702, android-ndk-r10d, OpenCV-2.4.10-android-sdk, Qt Creator 3.3.0, and my build target is "Android for armeabi-v7a (GCC 4.9, Qt 5.4.0)".
Edit 3: From Daniel Saner's comment:
In OpenCV 3.x, opencv_java has been renamed to opencv_java3. Also, while I didn't look into the specific changes that might have effected this, the workaround regarding that library in the final step seems to no longer be necessary. The app compiles and runs without the ANDROID_PACKAGE_SOURCE_DIR line
Edit 4: #myk's comment:
Worked for me with OpenCV 3.2. To workaround the build issues with carotene finish the LIBS+ section with: -lopencv_videostab\ -ltegra_hal\ – myk 2 hours ago
For OpenCV 4, sashoalm's approach did not work for me until I adapted it:
Download the Android-Pack and unzip it somewhere. We'll create a qmake-variable OPENCV_ANDROID which points to that directory later.
Add the following snippet to your *.pro-file:
android {
contains(ANDROID_TARGET_ARCH,arm64-v8a) {
isEmpty(OPENCV_ANDROID) {
error("Let OPENCV_ANDROID point to the opencv-android-sdk, recommended: v4.0")
}
INCLUDEPATH += "$$OPENCV_ANDROID/sdk/native/jni/include"
LIBS += \
-L"$$OPENCV_ANDROID/sdk/native/libs/arm64-v8a" \
-L"$$OPENCV_ANDROID/sdk/native/3rdparty/libs/arm64-v8a" \
-llibtiff \
-llibjpeg-turbo \
-llibjasper \
-llibpng \
-lIlmImf \
-ltbb \
-lopencv_java4 \
ANDROID_EXTRA_LIBS = $$OPENCV_ANDROID/sdk/native/libs/arm64-v8a/libopencv_java4.so
} else {
error("Unsupported architecture: $$ANDROID_TARGET_ARCH")
}
}
This will work for the arm64-v8a only. If you happen to build for another architecture (apparently 32-Bit is still the default for Qt#Android), you must change the .../libs/arm64-v8a part of the paths (occurs 3 times) and the same to match your actual target-architecture (the contains(...)-part in the second line of the snippet).
Tell qmake where to find the SDK. Add the following to qmake-call: "OPENCV_ANDROID=/path/to/OpenCV-android-sdk".
e.g., this looks like qmake example.pro "OPENCV_ANDROID=/home/user/OpenCV-android-sdk" from command line.
when you use QtCreator, add "OPENCV_ANDROID=..." to the "Additional arguments"-field. You can find it after enabling the Project-Mode in the Build-section of the android-kit. Expand the qmake-field under Build Steps
Starting from Android android-ndk-r18b, with Qt Creator 4.9.x kits, I could not use the openCV-4.1.1 pre-compiled shared libraries (.so) with Qt Android ABI armeabi-v7a target and ABI arm64-v8a, as Opencv standard is based on GCC, While the NDK-r18b removed gcc and uses clang compiler. ( I am getting
Fatal signal 11 (SIGSEGV), code 1
On initialize calling android_getCpuFeatures() when the application starts)
Thus, openCV shared libs must be compiled from sources for clang in order to be used with Qt Android kits.
This reference Compiling OpenCV on Android from C++ (Without OpenCVManager) was of real help. I would leave a reference here as well for a simple procedure I used under windows 10, to get opencv compiled with NDK 18 (clang) for Qt Android:
Downloaded openCV source code
Downloaded openCV contrib source for selected openCV version
Used cmake for windows
in the unzipped opencv source folder, created a new build folder.
MinGW from Qt installation can generally be used for building, So I used Qt 5.11.x (MinGW 5.3.0 32 bit) command line tool from Qt menu.
from command line, in new build folder, I could generate cmake configuration :
C:\opencv-4.1.1\build> "C:\program files\cmake\bin\cmake" .. -G"MinGW Makefiles"
-DBUILD_SHARED_LIBS=ON
-DANDROID_STL=c++_shared
-DANDROID_ABI="armeabi-v7a with NEON"
-DANDROID_NATIVE_API_LEVEL=23
-DANDROID_TOOLCHAIN=clang
-DCMAKE_TOOLCHAIN_FILE=D:\Qt\android-ndk-r18b\build\cmake\android.toolchain.cmake
-DANDROID_NDK=D:\Qt\android-ndk-r18b
-DANDROID_SDK=C:\Users\moham\AppData\Local\Android\sdk
-DCMAKE_BUILD_TYPE=Debug
-DBUILD_ANDROID_PROJECTS=OFF
-DWITH_OPENCL=ON -DWITH_TBB=ON -DENABLE_NEON=ON
-DBUILD_TESTS=OFF -DBUILD_PERF_TESTS=OFF
-DBUILD_FAT_JAVA_LIB=OFF
Then , C:\opencv-4.1.1\build>\mingw32-make -jx and C:\opencv-4.1.1\build>\mingw32-make install
the result libs can be picked from opencv-4.1.1\build\install folder
Link in Qt Android project:
android {
#opencv
OPENCVLIBS = $$PWD/../opencv-4.1.1\build\install/sdk/native
INCLUDEPATH = $$OPENCVLIBS/jni/include
contains(ANDROID_TARGET_ARCH,armeabi-v7a) {
# might need libs to be copied into app's library folder and loaded on start-up, in case android ships older libs??!
ANDROID_EXTRA_LIBS = \
$$OPENCVLIBS/libs/armeabi-v7a/libopencv_core.so \
$$OPENCVLIBS/libs/armeabi-v7a/libopencv_imgproc.so \
$$OPENCVLIBS/libs/armeabi-v7a/libtbb.so
LIBS += -L"$$OPENCVLIBS/libs/armeabi-v7a"\
-lopencv_core -lopencv_imgproc -ltbb
}
ANDROID_PACKAGE_SOURCE_DIR = $$PWD/android
}
Also, copy the libs to ANDROID_PACKAGE_SOURCE_DIR
Note: If detailed control over cmake configuration is needed, cmake windows gui can be used, while not a must and not tested. AmMIN's procedure is helpful for cmake tool, remember to add flag for shared Android STL.

How to build Android application in QT5 using OpenCV on Windows [duplicate]

I use Qt Creator to compile an Android application. I needed to integrate OpenCV into it, and it took me half a day to configure it properly, so I want to document the steps I took here, in case somebody else ever has to do it.
Edit: For OpenCV 4.x see the answers below. My answer was tested on OpenCV 2.4 only.
Original answer:
First, I downloaded OpenCV-2.4.10-android-sdk, and put into my project directory. It contains static libraries, and link order matters for static libraries for GCC. So you need to order them just so. This is how my .pro file looked in the end ($$_PRO_FILE_PWD_ refers to the project directory):
INCLUDEPATH += "$$_PRO_FILE_PWD_/OpenCV-2.4.10-android-sdk/sdk/native/jni/include"
android {
LIBS += \
-L"$$_PRO_FILE_PWD_/OpenCV-2.4.10-android-sdk/sdk/native/3rdparty/libs/armeabi-v7a"\
-L"$$_PRO_FILE_PWD_/OpenCV-2.4.10-android-sdk/sdk/native/libs/armeabi-v7a"\
-llibtiff\
-llibjpeg\
-llibjasper\
-llibpng\
-lIlmImf\
-ltbb\
-lopencv_core\
-lopencv_androidcamera\
-lopencv_flann\
-lopencv_imgproc\
-lopencv_highgui\
-lopencv_features2d\
-lopencv_calib3d\
-lopencv_ml\
-lopencv_objdetect\
-lopencv_video\
-lopencv_contrib\
-lopencv_photo\
-lopencv_java\
-lopencv_legacy\
-lopencv_ocl\
-lopencv_stitching\
-lopencv_superres\
-lopencv_ts\
-lopencv_videostab
ANDROID_PACKAGE_SOURCE_DIR=$$_PRO_FILE_PWD_/android
}
After that the project will compile but it will fail to run with the error
E/AndroidRuntime(11873): java.lang.UnsatisfiedLinkError: Cannot load library: link_image[1891]: 176 could not load needed library 'libopencv_java.so' for 'libMyProject.so' (load_library[1093]: Library 'libopencv_java.so' not found)
To overcome this, you need to add libopencv_java.so to your APK, and then manually load it from QtActivity.java. That's what the ANDROID_PACKAGE_SOURCE_DIR=$$_PRO_FILE_PWD_/android line at the end was for. Now you need to place libopencv_java.so here:
project_root/android/libs/armeabi-v7a/libopencv_java.so
project_root/android/src/org/qtproject/qt5/android/bindings/QtActivity.java
You can get QtActivity.java from the Android target build directory, in my case the full path was c:\Workspace\build-MyProject-Android_for_armeabi_v7a_GCC_4_9_Qt_5_4_0-Debug\android-build\src\org\qtproject\qt5\android\bindings\QtActivity.java, and just copy it.
Then you find those lines in it:
// now load the application library so it's accessible from this class loader
if (libName != null)
System.loadLibrary(libName);
And load libopencv_java.so before them, so they become:
// This is needed for OpenCV!!!
System.loadLibrary("opencv_java");
// now load the application library so it's accessible from this class loader
if (libName != null)
System.loadLibrary(libName);
Note that you pass opencv_java to System.loadLibrary(), even though the file is libopencv_java.so.
Edit: I forgot to mention, but I already had installed OpenCV Manager on my phone when trying to run one of the samples that come with OpenCV-2.4.10-android-sdk, so I don't know if it's needed or not. In any event, keep it in mind, if it fail even after my steps, you might need to download OpenCV Manager (it's available on the Google Store).
Edit 2: I'm using adt-bundle-windows-x86-20140702, android-ndk-r10d, OpenCV-2.4.10-android-sdk, Qt Creator 3.3.0, and my build target is "Android for armeabi-v7a (GCC 4.9, Qt 5.4.0)".
Edit 3: From Daniel Saner's comment:
In OpenCV 3.x, opencv_java has been renamed to opencv_java3. Also, while I didn't look into the specific changes that might have effected this, the workaround regarding that library in the final step seems to no longer be necessary. The app compiles and runs without the ANDROID_PACKAGE_SOURCE_DIR line
Edit 4: #myk's comment:
Worked for me with OpenCV 3.2. To workaround the build issues with carotene finish the LIBS+ section with: -lopencv_videostab\ -ltegra_hal\ – myk 2 hours ago
For OpenCV 4, sashoalm's approach did not work for me until I adapted it:
Download the Android-Pack and unzip it somewhere. We'll create a qmake-variable OPENCV_ANDROID which points to that directory later.
Add the following snippet to your *.pro-file:
android {
contains(ANDROID_TARGET_ARCH,arm64-v8a) {
isEmpty(OPENCV_ANDROID) {
error("Let OPENCV_ANDROID point to the opencv-android-sdk, recommended: v4.0")
}
INCLUDEPATH += "$$OPENCV_ANDROID/sdk/native/jni/include"
LIBS += \
-L"$$OPENCV_ANDROID/sdk/native/libs/arm64-v8a" \
-L"$$OPENCV_ANDROID/sdk/native/3rdparty/libs/arm64-v8a" \
-llibtiff \
-llibjpeg-turbo \
-llibjasper \
-llibpng \
-lIlmImf \
-ltbb \
-lopencv_java4 \
ANDROID_EXTRA_LIBS = $$OPENCV_ANDROID/sdk/native/libs/arm64-v8a/libopencv_java4.so
} else {
error("Unsupported architecture: $$ANDROID_TARGET_ARCH")
}
}
This will work for the arm64-v8a only. If you happen to build for another architecture (apparently 32-Bit is still the default for Qt#Android), you must change the .../libs/arm64-v8a part of the paths (occurs 3 times) and the same to match your actual target-architecture (the contains(...)-part in the second line of the snippet).
Tell qmake where to find the SDK. Add the following to qmake-call: "OPENCV_ANDROID=/path/to/OpenCV-android-sdk".
e.g., this looks like qmake example.pro "OPENCV_ANDROID=/home/user/OpenCV-android-sdk" from command line.
when you use QtCreator, add "OPENCV_ANDROID=..." to the "Additional arguments"-field. You can find it after enabling the Project-Mode in the Build-section of the android-kit. Expand the qmake-field under Build Steps
Starting from Android android-ndk-r18b, with Qt Creator 4.9.x kits, I could not use the openCV-4.1.1 pre-compiled shared libraries (.so) with Qt Android ABI armeabi-v7a target and ABI arm64-v8a, as Opencv standard is based on GCC, While the NDK-r18b removed gcc and uses clang compiler. ( I am getting
Fatal signal 11 (SIGSEGV), code 1
On initialize calling android_getCpuFeatures() when the application starts)
Thus, openCV shared libs must be compiled from sources for clang in order to be used with Qt Android kits.
This reference Compiling OpenCV on Android from C++ (Without OpenCVManager) was of real help. I would leave a reference here as well for a simple procedure I used under windows 10, to get opencv compiled with NDK 18 (clang) for Qt Android:
Downloaded openCV source code
Downloaded openCV contrib source for selected openCV version
Used cmake for windows
in the unzipped opencv source folder, created a new build folder.
MinGW from Qt installation can generally be used for building, So I used Qt 5.11.x (MinGW 5.3.0 32 bit) command line tool from Qt menu.
from command line, in new build folder, I could generate cmake configuration :
C:\opencv-4.1.1\build> "C:\program files\cmake\bin\cmake" .. -G"MinGW Makefiles"
-DBUILD_SHARED_LIBS=ON
-DANDROID_STL=c++_shared
-DANDROID_ABI="armeabi-v7a with NEON"
-DANDROID_NATIVE_API_LEVEL=23
-DANDROID_TOOLCHAIN=clang
-DCMAKE_TOOLCHAIN_FILE=D:\Qt\android-ndk-r18b\build\cmake\android.toolchain.cmake
-DANDROID_NDK=D:\Qt\android-ndk-r18b
-DANDROID_SDK=C:\Users\moham\AppData\Local\Android\sdk
-DCMAKE_BUILD_TYPE=Debug
-DBUILD_ANDROID_PROJECTS=OFF
-DWITH_OPENCL=ON -DWITH_TBB=ON -DENABLE_NEON=ON
-DBUILD_TESTS=OFF -DBUILD_PERF_TESTS=OFF
-DBUILD_FAT_JAVA_LIB=OFF
Then , C:\opencv-4.1.1\build>\mingw32-make -jx and C:\opencv-4.1.1\build>\mingw32-make install
the result libs can be picked from opencv-4.1.1\build\install folder
Link in Qt Android project:
android {
#opencv
OPENCVLIBS = $$PWD/../opencv-4.1.1\build\install/sdk/native
INCLUDEPATH = $$OPENCVLIBS/jni/include
contains(ANDROID_TARGET_ARCH,armeabi-v7a) {
# might need libs to be copied into app's library folder and loaded on start-up, in case android ships older libs??!
ANDROID_EXTRA_LIBS = \
$$OPENCVLIBS/libs/armeabi-v7a/libopencv_core.so \
$$OPENCVLIBS/libs/armeabi-v7a/libopencv_imgproc.so \
$$OPENCVLIBS/libs/armeabi-v7a/libtbb.so
LIBS += -L"$$OPENCVLIBS/libs/armeabi-v7a"\
-lopencv_core -lopencv_imgproc -ltbb
}
ANDROID_PACKAGE_SOURCE_DIR = $$PWD/android
}
Also, copy the libs to ANDROID_PACKAGE_SOURCE_DIR
Note: If detailed control over cmake configuration is needed, cmake windows gui can be used, while not a must and not tested. AmMIN's procedure is helpful for cmake tool, remember to add flag for shared Android STL.

Android native project referencing shared library from c++ library project

I have 2 projects:
1 - Android Native project
2 - C/C++ Project
I'm building my shared library files (.so) in a C/C++ project and want to use those .so files in Android Native project.
I don't want to copy and paste these library files from one project to another.
First of all is it possible to use those .so files from my native Android project by using some reference etc to C++ library project?
Would it be easier to find a way to automatically copy the .so files from the C++ library project to the Android native project?
Well you'll need to re-compile your libs for ARM first. You can use the ndk's "standalone toolchain" functionality for that. There's a doc explaining more about it in the ndk dir (docs/STANDALONE-TOOLCHAIN.html). I needed to use libexpat in a project so I whipped up a bash file to compile expat using the standalone toolchain like so:
NDK_PATH=/android-ndk-r7
NDK_GCC=${NDK_PATH}/toolchains/arm-linux-androideabi-4.4.3/prebuilt/linux-x86/bin/arm-linux-androideabi-gcc
export CC="${NDK_GCC} --sysroot=${NDK_PATH}/platforms/android-8/arch-arm"
export CFLAGS='-mthumb'
export LDFLAGS='-Wl,--fix-cortex-a8'
./configure --host=arm-eabi
make
Then you can copy the so wherever you want and reference it from Android.mk:
LOCAL_LDLIBS := \
-Lvendor/expat/sdk/lib/android \
-lexpat
Note that I built expat and linked it statically so I didn't have to worry about copying the resulting so

Categories

Resources