I am trying to extend this project, but it has been built using ANT and these days we are using Android Studio and Gradle in practice. I want to move it on Android Studio for further implementations. So here's my project structure...
app/build.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "26.0.0"
defaultConfig {
applicationId "tum.andrive"
minSdkVersion 14
targetSdkVersion 25
versionCode 1
versionName "1.0"
ndk {
abiFilters "armeabi", "armeabi-v7a"
}
sourceSets {
main {
jniLibs.srcDirs = ['src/main/cpp']
}
}
externalNativeBuild {
cmake {
cppFlags "-frtti -fexceptions"
}
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
externalNativeBuild {
cmake {
path "CMakeLists.txt"
}
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:appcompat-v7:25.3.1'
compile project(':opencv')
}
CMakeLists.txt
cmake_minimum_required(VERSION 3.4.1)
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 )
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 )
SET(OpenCV_DIR $ENV{OPENCVSDK}/native/jni)
message(STATUS "opencv found: ${OpenCV_LIBS}")
include_directories( ${OpenCV_DIR}/include/)
target_link_libraries( # Specifies the target library.
native-lib
# Links the target library to the log library
# included in the NDK.
${log-lib} )
Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
#OpenCv
OPENCV_CAMERA_MODULES:=on
OPENCV_INSTALL_MODULES:=on
include $(OPENCVSDK)/native/jni/OpenCV.mk
LOCAL_MODULE := AndriveNative
LOCAL_SRC_FILES := native-lib.cpp
LOCAL_LDLIBS += -llog -ldl
include $(BUILD_SHARED_LIBRARY)
But getting error while building project, have a look on this...
Message View
Gradle Console
D:\Android\workspace\Local\Andrive\app\.externalNativeBuild\cmake\debug\armeabi
--target
native-lib
jvmArgs :
Starting process 'command 'Z:\Android\sdk\cmake\3.6.4111459\bin\cmake.exe''. Working directory: D:\Android\workspace\Local\Andrive\app Command: Z:\Android\sdk\cmake\3.6.4111459\bin\cmake.exe --build D:\Android\workspace\Local\Andrive\app\.externalNativeBuild\cmake\debug\armeabi --target native-lib
Successfully started process 'command 'Z:\Android\sdk\cmake\3.6.4111459\bin\cmake.exe''
[1/1] Linking CXX shared library ..\..\..\..\build\intermediat
es\cmake\debug\obj\armeabi\libnative-lib.so
FAILED: cmd.exe /C "cd . && Z:\Android\sdk\ndk-bundle\toolchains\llvm\prebuilt\windows-x86_64\bin\clang++.exe --target=armv5te-none-linux-androideabi --gcc-toolchain=Z:/Android/sdk/ndk-bundle/toolchains/arm-linux-androideabi-4.9/prebuilt/windows-x86_64 --sysroot=Z:/Android/sdk/ndk-bundle/sysroot -fPIC -isystem Z:/Android/sdk/ndk-bundle/sysroot/usr/include/arm-linux-androideabi -D__ANDROID_API__=14 -g -DANDROID -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -march=armv5te -mtune=xscale -msoft-float -fno-integrated-as -mthumb -Wa,--noexecstack -Wformat -Werror=format-security -frtti -fexceptions -O0 -fno-limit-debug-info -Wl,--exclude-libs,libgcc.a --sysroot Z:/Android/sdk/ndk-bundle/platforms/android-14/arch-arm -Wl,--build-id -Wl,--warn-shared-textrel -Wl,--fatal-warnings -Wl,--no-undefined -Wl,-z,noexecstack -Qunused-arguments -Wl,-z,relro -Wl,-z,now -shared -Wl,-soname,libnative-lib.so -o ..\..\..\..\build\intermediates\cmake\debug\obj\armeabi\libnative-lib.so CMakeFiles/native-lib.dir/src/main/cpp/native-lib.cpp.o -llog -lm "Z:/Android/sdk/ndk-bundle/sources/cxx-stl/gnu-libstdc++/4.9/libs/armeabi/libgnustl_static.a" "-latomic" && cd ."
D:\Android\workspace\Local\Andrive\app\src\main\cpp/native-lib.cpp:27: error: undefined reference to 'DetectionBasedTracker::Parameters::Parameters()'
D:\Android\workspace\Local\Andrive\app\src\main\cpp/native-lib.cpp:30: error: undefined reference to 'DetectionBasedTracker::DetectionBasedTracker(std::string const&, DetectionBasedTracker::Parameters const&)'
CMakeFiles/native-lib.dir/src/main/cpp/native-lib.cpp.o(.ARM.extab.text.Java_tum_andrive_DetectionBasedTracker_nativeCreateObject+0xe4): error: undefined reference to 'typeinfo for cv::Exception'
D:\Android\workspace\Local\Andrive\app\src\main\cpp/native-lib.cpp:54: error: undefined reference to 'DetectionBasedTracker::getParameters()'
D:\Android\workspace\Local\Andrive\app\src\main\cpp/native-lib.cpp:56: error: undefined reference to 'DetectionBasedTracker::setParameters(DetectionBasedTracker::Parameters const&)'
CMakeFiles/native-lib.dir/src/main/cpp/native-lib.cpp.o(.ARM.extab.text.Java_tum_andrive_DetectionBasedTracker_nativeSetDetectionSize+0xa4): error: undefined reference to 'typeinfo for cv::Exception'
CMakeFiles/native-lib.dir/src/main/cpp/native-lib.cpp.o(.ARM.extab.text.Java_tum_andrive_DetectionBasedTracker_nativeDestroyObject+0xa4): error: undefined reference to 'typeinfo for cv::Exception'
CMakeFiles/native-lib.dir/src/main/cpp/native-lib.cpp.o(.ARM.extab.text.Java_tum_andrive_DetectionBasedTracker_nativeStart+0xa4): error: undefined reference to 'typeinfo for cv::Exception'
D:/Android/OpenCV-2.4.10-android-sdk/sdk/native/jni/include\opencv2/core/mat.hpp:188: error: undefined reference to 'cv::_OutputArray::_OutputArray(cv::Mat&)'
D:/Android/OpenCV-2.4.10-android-sdk/sdk/native/jni/include\opencv2/core/mat.hpp:188: error: undefined reference to 'cv::Mat::copyTo(cv::_OutputArray const&) const'
D:/Android/OpenCV-2.4.10-android-sdk/sdk/native/jni/include\opencv2/core/mat.hpp:298: error: undefined reference to 'cv::Mat::copySize(cv::Mat const&)'
D:/Android/OpenCV-2.4.10-android-sdk/sdk/native/jni/include\opencv2/core/mat.hpp:27
8: error: undefined reference to 'cv::fastFree(void*)'
D:/Android/OpenCV-2.4.10-android-sdk/sdk/native/jni/include\opencv2/core/mat.hpp:367: error: undefined reference to 'cv::Mat::deallocate()'
clang++.exe: error: linker command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: subcommand failed.
:app:externalNativeBuildDebug FAILED
:app:externalNativeBuildDebug (Thread[Daemon worker Thread 2,5,main]) completed. Took 8.011 secs.
:app:buildInfoGeneratorDebug (Thread[Daemon worker Thread 2,5,main]) started.
:app:buildInfoGeneratorDebug
Putting task artifact state for task ':app:buildInfoGeneratorDebug' into context took 0.0 secs.
Executing task ':app:buildInfoGeneratorDebug' (up-to-date check took 0.0 secs) due to:
Task has not declared any outputs.
:app:buildInfoGeneratorDebug (Thread[Daemon worker Thread 2,5,main]) completed. Took 0.018 secs.
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:externalNativeBuildDebug'.
> Build command failed.
Error while executing process Z:\Android\sdk\cmake\3.6.4111459\bin\cmake.exe with arguments {--build D:\Android\workspace\Local\Andrive\app\.externalNativeBuild\cmake\debug\armeabi --target native-lib}
[1/1] Linking CXX shared library ..\..\..\..\build\intermediates\cmake\debug\obj\armeabi\libnative-lib.so
FAILED: cmd.exe /C "cd . && Z:\Android\sdk\ndk-bundle\toolchains\llvm\prebuilt\windows-x86_64\bin\clang++.exe --target=armv5te-none-linux-androideabi --gcc-toolchain=Z:/Android/sdk/ndk-bundle/toolchains/arm-linux-androideabi-4.9/prebuilt/windows-x86_64 --sysroot=Z:/Android/sdk/ndk-bundle/sysroot -fPIC -isystem Z:/Android/sdk/ndk-bundle/sysroot/usr/include/arm-linux-androideabi -D__ANDROID_API__=14 -g -DANDROID -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -march=armv5te -mtune=xscale -msoft-float -fno-integrated-as -mthumb -Wa,--noexecstack -Wformat -Werror=format-security -frtti -fexceptions -O0 -fno-limit-debug-info -Wl,--exclude-libs,libgcc.a --sysroot Z:/Android/sdk/ndk-bundle/platforms/android-14/arch-arm -Wl,--build-id -Wl,--warn-shared-textrel -Wl,--fatal-warnings -Wl,--no-undefined -Wl,-z,noexecstack -Qunused-arguments -Wl,-z,relro -Wl,-z,now -shared -Wl,-soname,libnative-lib.so -o ..\..\..\..\build\intermediates\cmake\debug\obj\armeabi\libnative-lib.so CMakeFiles/native-lib.dir/src/main/cpp/native-lib.cpp.o -llog -lm "Z:/Android/sdk/ndk-bundle/sources/cxx-stl/gnu-libstdc++/4.9/libs/armeabi/libgnustl_static.a" "-latomic" && cd ."
D:\Android\workspace\Local\Andrive\app\src\main\cpp/native-lib.cpp:27: error: undefined reference to 'DetectionBasedTracker::Parameters::Parameters()'
D:\Android\workspace\Local\Andrive\app\src\main\cpp/native-lib.cpp:30: error: undefined reference to 'DetectionBasedTracker::DetectionBasedTracker(std::string const&, DetectionBasedTracker::Parameters const&)'
CMakeFiles/native-lib.dir/src/main/cpp/native-lib.cpp.o(.ARM.extab.text.Java_tum_andrive_DetectionBasedTracker_nativeCreateObject+0xe4): error: undefined reference to 'typeinfo for cv::Exception'
D:\Android\workspace\Local\Andrive\app\src\main\cpp/native-lib.cpp:54: error: undefined reference to 'DetectionBasedTracker::getParameters()'
D:\Android\workspace\Local\Andrive\app\src\main\cpp/native-lib.cpp:56: error: undefined reference to 'DetectionBasedTracker::setParameters(DetectionBasedTracker::Parameters const&)'
CMakeFiles/native-lib.dir/src/main/cpp/native-lib.cpp.o(.ARM.extab.text.Java_tum_andrive_DetectionBasedTracker_nativeSetDetectionSize+0xa4): error: undefined reference to 'typeinfo for cv::Exception'
CMakeFiles/native-lib.dir/src/main/cpp/native-lib.cpp.o(.ARM.extab.text.Java_tum_andrive_DetectionBasedTracker_nativeDestroyObject+0xa4): error: undefined reference to 'typeinfo for cv::Exception'
CMakeFiles/native-lib.dir/src/main/cpp/native-lib.cpp.o(.ARM.extab.text.Java_tum_andrive_DetectionBasedTracker_nativeStart+0xa4): error: undefined reference to 'typeinfo for cv::Exception'
D:/Android/OpenCV-2.4.10-android-sdk/sdk/native/jni/include\opencv2/core/mat.hpp:188: error: undefined reference to 'cv::_OutputArray::_OutputArray(cv::Mat&)'
D:/Android/OpenCV-2.4.10-android-sdk/sdk/native/jni/include\opencv2/core/mat.hpp:188: error: undefined reference to 'cv::Mat::copyTo(cv::_OutputArray const&) const'
D:/Android/OpenCV-2.4.10-android-sdk/sdk/native/jni/include\opencv2/core/mat.hpp:298: error: undefined reference to 'cv::Mat::copySize(cv::Mat const&)'
D:/Android/OpenCV-2.4.10-android-sdk/sdk/native/jni/include\opencv2/core/mat.hpp:278: error: undefined reference to 'cv::fastFree(void*)'
D:/Android/OpenCV-2.4.10-android-sdk/sdk/native/jni/include\opencv2/core/mat.hpp:367: error: undefined reference to 'cv::Mat::deallocate()'
clang++.exe: error: linker command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: subcommand failed.
Anybody please help me to setup this project!
After a lot of searching, I've found this project. So many thanks to Adrien Lescourt. This demo project describes how we can use OpenCV with Android Studio & Gradle. Problem was with jniLibs directory and CMakeLists.txt file. Here's the correct version...
app/build.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "26.0.0"
defaultConfig {
applicationId "tum.andrive"
minSdkVersion 14
targetSdkVersion 19
versionCode 1
versionName "1.0"
ndk {
abiFilters "armeabi-v7a"
}
sourceSets {
main {
jniLibs.srcDirs = ['src/main/jniLibs']
}
}
externalNativeBuild {
cmake {
cppFlags "-frtti -fexceptions"
abiFilters 'x86', 'x86_64', 'armeabi', 'armeabi-v7a', 'arm64-v8a', 'mips', 'mips64'
}
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
externalNativeBuild {
cmake {
path "CMakeLists.txt"
}
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:appcompat-v7:25.3.1'
compile project(':opencv')
}
CMakeLists.txt
cmake_minimum_required(VERSION 3.4.1)
# OpenCV stuff
include_directories(D:\\Android\\OpenCV-2.4.10-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_java.so)
add_library( native-lib
SHARED
src/main/cpp/native-lib.cpp )
find_library( log-lib
log )
target_link_libraries( native-lib
lib_opencv
${log-lib} )
And at last I've added jniLibs folder to app and copied all .so files in this folder stored in OpenCV-2.4.10-android-sdk/sdk/native/libs.
I hope this solution will help people to setup OpenCV with gradle. Please comment if any other information required.
Thanks
Related
I am trying to implement Alexa Voice Service SDK in Android.
I have pre-build libraries, which i am using in my Android Project.
I have .so files under src/main/jniLibs/armeabi-v7a/*.so
I can successfully compile and build apk for x86 platform , but my target is run it on armeabi-v7a device.
but while generating apk i am facing the incompatible target error.
the full error is shown below ,
FAILURE: Build failed with an exception.
What went wrong:
Execution failed for task ':app:externalNativeBuildDebug'.
> Build command failed.
Error while executing process /home/ajinkya/Androidsdk/cmake/3.10.2.4988404/bin/cmake with arguments {--build /root/AndroidStudioProjects/alexaProtoApp/app/.externalNativeBuild/cmake/debug/armeabi-v7a --target native-lib}
[1/2] Building CXX object CMakeFiles/native-lib.dir/native-lib.cpp.o
[2/2] Linking CXX shared library /root/AndroidStudioProjects/alexaProtoApp/app/build/intermediates/cmake/debug/obj/armeabi-v7a/libnative-lib.so
FAILED: /root/AndroidStudioProjects/alexaProtoApp/app/build/intermediates/cmake/debug/obj/armeabi-v7a/libnative-lib.so
: && /home/ajinkya/Androidsdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/bin/clang++ --target=armv7-none-linux-androideabi24 --gcc-toolchain=/home/ajinkya/Androidsdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64 --sysroot=/home/ajinkya/Androidsdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/sysroot -fPIC -g -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -fno-addrsig -march=armv7-a -mthumb -Wa,--noexecstack -Wformat -Werror=format-security -O0 -fno-limit-debug-info -Wl,--exclude-libs,libgcc.a -Wl,--exclude-libs,libatomic.a -static-libstdc++ -Wl,--build-id -Wl,--warn-shared-textrel -Wl,--fatal-warnings -Wl,--exclude-libs,libunwind.a -Wl,--no-undefined -Qunused-arguments -Wl,-z,noexecstack -shared -Wl,-soname,libnative-lib.so -o /root/AndroidStudioProjects/alexaProtoApp/app/build/intermediates/cmake/debug/obj/armeabi-v7a/libnative-lib.so CMakeFiles/native-lib.dir/native-lib.cpp.o /root/AndroidStudioProjects/alexaProtoApp/app/src/main/cpp/../../main/jniLibs/armeabi-v7a/libSampleApp.so /home/ajinkya/Androidsdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib/arm-linux-androideabi/24/liblog.so /home/ajinkya/Androidsdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib/arm-linux-androideabi/24/liblog.so -latomic -lm && :
/home/ajinkya/Androidsdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/lib/gcc/arm-linux-androideabi/4.9.x/../../../../arm-linux-androideabi/bin/ld: error: /root/AndroidStudioProjects/alexaProtoApp/app/src/main/cpp/../../main/jniLibs/armeabi-v7a/libSampleApp.so: incompatible target
/root/AndroidStudioProjects/alexaProtoApp/app/src/main/cpp/native-lib.cpp:25: error: undefined reference to 'my_initialize(_JavaVM*, _jobject*)'
/root/AndroidStudioProjects/alexaProtoApp/app/src/main/cpp/native-lib.cpp:31: error: undefined reference to 'jni_wakeAlexa()'
/root/AndroidStudioProjects/alexaProtoApp/app/src/main/cpp/native-lib.cpp:37: error: undefined reference to 'jni_wakeWithHoldAlexa()'
/root/AndroidStudioProjects/alexaProtoApp/app/src/main/cpp/native-lib.cpp:43: error: undefined reference to 'jni_resetAlexa()'
/root/AndroidStudioProjects/alexaProtoApp/app/src/main/cpp/native-lib.cpp:49: error: undefined reference to 'jni_reauthorizeAlexa()'
/root/AndroidStudioProjects/alexaProtoApp/app/src/main/cpp/native-lib.cpp:55: error: undefined reference to 'jni_quitAlexa()'
/root/AndroidStudioProjects/alexaProtoApp/app/src/main/cpp/native-lib.cpp:61: error: undefined reference to 'jni_infoAlexa()'
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: subcommand failed.
My build.gradle like,
apply plugin: 'com.android.application'
android {
compileSdkVersion 29
defaultConfig {
applicationId "my.project.vdi"
minSdkVersion 24
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
multiDexEnabled true
externalNativeBuild {
cmake {
cppFlags ""
// abiFilters "armeabi-v7a"
// arguments "-DANDROID_ARM_MODE=arm" , "-DANDROID_TOOLCHAIN=clang"
}
}
ndk {
// Specifies the ABI configurations of your native
// libraries Gradle should build and package with your APK. , x86 , armeabi-v7a
abiFilters "armeabi-v7a"
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
externalNativeBuild {
cmake {
path "src/main/cpp/CMakeLists.txt"
version "3.10.2"
}
}
sourceSets {
main {
jni.srcDirs = []
// let gradle pack the shared library into apk
jniLibs.srcDirs = ['src/main/jniLibs/']
}
}
// packagingOptions {
// exclude 'lib/arm64-v8a/libnative-lib.so'
// exclude 'lib/x86_64/libnative-lib.so'
// exclude 'lib/x86/libnative-lib.so'
// }
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
implementation 'com.squareup.picasso:picasso:2.5.2'
}
ALso the CMakeLists.txt
cmake_minimum_required(VERSION 3.4.1)
add_library( # Sets the name of the library.
native-lib
SHARED
native-lib.cpp)
add_library(SampleApp SHARED IMPORTED)
set_target_properties(SampleApp PROPERTIES IMPORTED_LOCATION ${PROJECT_SOURCE_DIR}/../../main/jniLibs/${ANDROID_ABI}/libSampleApp.so)
find_library(
log-lib
log)
find_library(ANDROID_LOG_LIB log)
target_link_libraries(
native-lib
SampleApp
${ANDROID_LOG_LIB}
${log-lib} )
Please guide me resolving this issue.
.../armeabi-v7a/libSampleApp.so: incompatible target
That library isn't actually built for armeabi-v7a. The following should tell you what it is:
$ file /root/AndroidStudioProjects/alexaProtoApp/app/src/main/cpp/../../main/jniLibs/armeabi-v7a/libSampleApp.so
Here's example output from libc++_shared.so in the NDK:
$ file android-ndk-r21-canary/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib/arm-linux-androideabi/libc++_shared.so
android-ndk-r21-canary/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib/arm-linux-androideabi/libc++_shared.so: ELF 32-bit LSB shared object, ARM, EABI5 version 1 (SYSV), dynamically linked, BuildID[sha1]=4dc00a833014c1ddd2e1ec64c5dd72237c4179ee, with debug_info, not stripped
The interesting part is:
ELF 32-bit LSB shared object, ARM, EABI5 version 1 (SYSV)
I got the solution, it is very unusual for me , the .so files which were generating while compiling, were not getting replaced. the old version of the files were not getting updated by the new ones. I deleted the folder and then compiled and the new files generated were the proper ones.
I should say that I'm first time at android studio with ndk.
environment is Android Studio 3.1.4 and OpenCV 3.4 at windows 10 os.
I use open source about opencv that 'Facetracker' link is below
https://github.com/kylemcdonald/FaceTracker
i changed some codes and want to build at android studio
so i drew up CMakeList.txt, Android.mk, Application.mk, build.gradle.
Synchronization with gradle work well but build, generate signed APK occur error below.
Build command failed.
Error while executing process C:\Users\Duru\AppData\Local\Android\Sdk\cmake\3.6.4111459\bin\cmake.exe with arguments {--build D:\ProgramFiles\Android\Projects\OpencvTest\app\.externalNativeBuild\cmake\release\x86_64 --target PAW}
[1/1] **Linking CXX shared library** ..\..\..\..\build\intermediates\cmake\release\obj\x86_64\libPAW.so
FAILED: cmd.exe /C "cd . && C:\Users\Duru\AppData\Local\Android\Sdk\ndk-bundle\toolchains\x86_64-4.9\prebuilt\windows-x86_64\bin\x86_64-linux-android-g++.exe --sysroot=C:/Users/Duru/AppData/Local/Android/Sdk/ndk-bundle/sysroot -fPIC -isystem C:/Users/Duru/AppData/Local/Android/Sdk/ndk-bundle/sysroot/usr/include/x86_64-linux-android -D__ANDROID_API__=21 -g -DANDROID -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -Wa,--noexecstack -Wformat -Werror=format-security -frtti -fexceptions -std=gnu++11 -O2 -DNDEBUG -Wl,--exclude-libs,libgcc.a -Wl,--exclude-libs,libatomic.a --sysroot C:/Users/Duru/AppData/Local/Android/Sdk/ndk-bundle/platforms/android-21/arch-x86_64 -Wl,--build-id -Wl,--warn-shared-textrel -Wl,--fatal-warnings -Wl,--no-undefined -Wl,-z,noexecstack -Wl,-z,relro -Wl,-z,now -shared -Wl,-soname,libPAW.so -o ..\..\..\..\build\intermediates\cmake\release\obj\x86_64\libPAW.so CMakeFiles/PAW.dir/src/main/jni/src/lib/PAW.cc.o -latomic -lm "C:/Users/Duru/AppData/Local/Android/Sdk/ndk-bundle/sources/cxx-stl/gnu-libstdc++/4.9/libs/x86_64/libgnustl_static.a" && cd ."
D:/ProgramFiles/Android/Projects/OpenCV-android-sdk/sdk/native/jni/include/opencv2/core/cvstd.hpp:648: error: undefined reference to 'cv::String::deallocate()'
**D:/ProgramFiles/Android/Projects/OpenCV-android-sdk/sdk/native/jni/include/opencv2/core/cvstd.hpp:648: error: undefined reference to 'cv::String::deallocate()'
D:/ProgramFiles/Android/Projects/OpenCV-android-sdk/sdk/native/jni/include/opencv2/core/mat.inl.hpp:2686: error: undefined reference to 'cv::MatConstIterator::seek(long, bool)'
D:\ProgramFiles\Android\Projects\OpencvTest\app\src\main\jni\src\lib/PAW.cc:146: error: undefined reference to 'cv::Mat::zeros(int, int, int)'
D:/ProgramFiles/Android/Projects/OpenCV-android-sdk/sdk/native/jni/include/opencv2/core/mat.inl.hpp:2591: error: undefined reference to 'cv::MatConstIterator::seek(int const*, bool)'
D:/ProgramFiles/Android/Projects/OpenCV-android-sdk/sdk/native/jni/include/opencv2/core/mat.inl.hpp:2591: error: undefined reference to 'cv::MatConstIterator::seek(int const*, bool)'
D:/ProgramFiles/Android/Projects/OpenCV-android-sdk/sdk/native/jni/include/opencv2/core/mat.inl.hpp:2686: error: undefined reference to 'cv::MatConstIterator::seek(long, bool)'
D:/ProgramFiles/Android/Projects/OpenCV-android-sdk/sdk/native/jni/include/opencv2/core/mat.inl.hpp:717: error: undefined reference to 'cv::Mat::copySize(cv::Mat const&)'
D:/ProgramFiles/Android/Projects/OpenCV-android-sdk/sdk/native/jni/include/opencv2/core/mat.inl.hpp:717: error: undefined reference to 'cv::Mat::copySize(cv::Mat const&)'
D:\ProgramFiles\Android\Projects\OpencvTest\app\src\main\jni\src\lib/PAW.cc:167: error: undefined reference to 'cv::remap(cv::_InputArray const&, cv::_OutputArray const&, cv::_InputArray const&, cv::_InputArray const&, int, int, cv::Scalar_<double> const&)'
collect2.exe: error: ld returned 1 exit status**
ninja: build stopped: subcommand failed.
my android.mk
LOCAL_PATH := $(call my-dir)
MY_PATH := $(LOCAL_PATH)
ifeq ($(TARGET_ARCH_ABI),armeabi-v7a)
include $(CLEAR_VARS)
CVROOT := D:/ProgramFiles/Android/Projects/OpenCV-android-sdk/sdk/native/jni
LOCAL_MODULE := FaceTracker
LOCAL_SRC_FILES := libFaceTracker.a #$(wildcard /src/lib/*.cc)
#LOCAL_C_INCLUDES := $(LOCAL_PATH)/include
LOCAL_LDLIBS = -lz -lm
LOCAL_CFLAGS = -Wall -pedantic -g -O3 -ffast-math -funroll-loops -march=armv7-a -mfloat-abi=softfp -mfpu=neon
include $(PREBUILT_STATIC_LIBRARY)
include $(CLEAR_VARS)
OPENCV_INSTALL_MODULES:=on
include $(CVROOT)/OpenCV.mk
LOCAL_MODULE := opencv_java3
LOCAL_SRC_FILES := native-lib.cpp
LOCAL_C_FLAGS+= -DNDEBUG -Wall -pedantic -g -O3 -ffast-math -funroll-loops -march=armv7-a -mfloat-abi=softfp -mfpu=neon
#LOCAL_C_INCLUDES+= $(NDK_APP_PROJECT_PATH)/stasm4.1.0/stasm
#LOCAL_SRC_FILES += $(NDK_APP_PROJECT_PATH)/libstasm.a
#LOCAL_SRC_FILES += $(wildcard $(NDK_APP_PROJECT_PATH)/stasm/*.cpp)
LOCAL_LDLIBS += -lm -llog -landroid
#LOCAL_SHARED_LIBRARIES += libandroid
LOCAL_STATIC_LIBRARIES += android_native_app_glue
LOCAL_STATIC_LIBRARIES += FaceTracker
LOCAL_ARM_NEON := true
LOCAL_ARM_MODE := arm
include $(BUILD_SHARED_LIBRARY)
$(call import-module,android/native_app_glue)
endif
my build.gradle
import org.apache.tools.ant.taskdefs.condition.Os
def getNdkBuildPath() {
Properties properties = new Properties()
properties.load(project.rootProject.file('local.properties').newDataInputStream())
def command = properties.getProperty('ndk.dir')
if(Os.isFamily(Os.FAMILY_WINDOWS)) {
command +="\\ndk-build.cmd"
} else {
command +="/ndk-build"
}
return command
}
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.example.duru.opencvtest"
minSdkVersion 15
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
externalNativeBuild {
cmake {
cppFlags "-frtti -fexceptions"
arguments '-DANDROID_TOOLCHAIN=gcc',
'-DANDROID_STL=gnustl_static'
}
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
externalNativeBuild {
cmake {
path "CMakeLists.txt"
}
}
}
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:28.0.0-rc02'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation project(':openCVLibrary342')
}
my application.mk
APP_PLATFORM := android-28
APP_ABI := armeabi-v7a
APP_STL := gnustl_static
APP_CPPFLAGS := -frtti -fexceptions -std=c++11
APP_OPTIM := release
and CMakeList.txt
# 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)
set(pathOPENCV D:/ProgramFiles/Android/Projects/OpenCV-android-sdk)
set(pathPROJECT D:/ProgramFiles/Android/Projects/OpencvTest)
set(pathLIBOPENCV_JAVA ${pathPROJECT}/app/src/main/JniLibs/${ANDROID_ABI}/libopencv_java3.so)
set(CMAKE_VERBOSE_MAKEFILE on)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11")
include_directories(${pathOPENCV}/sdk/native/jni/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( # 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/jni/native-lib.cpp)
add_library( lib_opencv SHARED IMPORTED )
add_library( Tracker SHARED src/main/jni/src/lib/Tracker.cpp )
add_library( PDM SHARED src/main/jni/src/lib/PDM.cc )
add_library( PAW SHARED src/main/jni/src/lib/PAW.cc )
add_library( Patch SHARED src/main/jni/src/lib/Patch.cc )
add_library( IO SHARED src/main/jni/src/lib/IO.cc )
add_library( FDet SHARED src/main/jni/src/lib/FDet.cc )
add_library( FCheck SHARED src/main/jni/src/lib/FCheck.cc )
add_library( CLM SHARED src/main/jni/src/lib/CLM.cc )
set_target_properties(lib_opencv PROPERTIES IMPORTED_LOCATION ${pathLIBOPENCV_JAVA})
# 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( # Specifies the target library.
native-lib
Tracker
PDM
PAW
Patch
IO
FDet
FCheck
CLM
lib_opencv
# Links the target library to the log library
# included in the NDK.
${log-lib} )
opensource are composed of 7 header, 6 .cc files and a .cpp file.
I am trying to set a simple project in Android Studio with OpenCL. So I created a new project with NDK support and an empty activity. I have copied the libOpenCL.so from my mobile phone to the folder app\src\main\jniLibs\arm8 and I have downloaded the OpenCL headers from the Khronos webpage and copied them to app\src\main\jni\CL. Furthermore, I have done two modifications to the build.gradle file, I have added a parameter to the cppFlags to include the OpenCL folder and I have set an abi filter to build only for arm8. I copy my build.gradle file here:
apply plugin: 'com.android.application'
android {
compileSdkVersion 26
buildToolsVersion "26.0.1"
defaultConfig {
applicationId "es.um.mompes.testopencl"
minSdkVersion 22
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
externalNativeBuild {
cmake {
cppFlags "-std=c++11 -fexceptions -I${projectDir}/src/main/jni"
}
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
externalNativeBuild {
cmake {
path "CMakeLists.txt"
}
}
productFlavors {
arm8 {
ndk {
abiFilter "arm64-v8a"
}
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support.constraint:constraint-layout:1.0.2'
testCompile 'junit:junit:4.12'
}
I have also added a three lines to the generated native-lib.cpp file to test if OpenCL is working. This is my native-lib.cpp:
#include <jni.h>
#include <string>
#include <CL/cl.h>
extern "C"
JNIEXPORT jstring JNICALL
Java_es_um_mompes_testopencl_MainActivity_stringFromJNI(
JNIEnv *env,
jobject /* this */) {
std::string hello = "Hello from C++";
cl_int err;
// Find the number of OpenCL platforms
cl_uint num_platforms;
err = clGetPlatformIDs(0, NULL, &num_platforms);
return env->NewStringUTF(hello.c_str());
}
However, I must be missing something because I am getting an undefined reference error regarding the OpenCL function when I build the project. This is the complete error:
Execution failed for task ':app:externalNativeBuildArm8Debug'.
Build command failed.
Error while executing process C:\Users\Juan\AppData\Local\Android\Sdk\cmake\3.6.4111459\bin\cmake.exe with arguments {--build C:\Users\Juan\AndroidStudioProjects\TestOpenCL\app.externalNativeBuild\cmake\arm8Debug\arm64-v8a --target native-lib}
[1/1] Linking CXX shared library ........\build\intermediates\cmake\arm8\debug\obj\arm64-v8a\libnative-lib.so
FAILED: cmd.exe /C "cd . && C:\Users\Juan\AppData\Local\Android\sdk\ndk-bundle\toolchains\llvm\prebuilt\windows-x86_64\bin\clang++.exe --target=aarch64-none-linux-android --gcc-toolchain=C:/Users/Juan/AppData/Local/Android/sdk/ndk-bundle/toolchains/aarch64-linux-android-4.9/prebuilt/windows-x86_64 --sysroot=C:/Users/Juan/AppData/Local/Android/sdk/ndk-bundle/sysroot -fPIC -isystem C:/Users/Juan/AppData/Local/Android/sdk/ndk-bundle/sysroot/usr/include/aarch64-linux-android -D__ANDROID_API__=22 -g -DANDROID -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -Wa,--noexecstack -Wformat -Werror=format-security -std=c++11 -fexceptions -IC:\Users\Juan\AndroidStudioProjects\TestOpenCL\app/src/main/jni -O0 -fno-limit-debug-info -Wl,--exclude-libs,libgcc.a --sysroot C:/Users/Juan/AppData/Local/Android/sdk/ndk-bundle/platforms/android-22/arch-arm64 -Wl,--build-id -Wl,--warn-shared-textrel -Wl,--fatal-warnings -Wl,--no-undefined -Wl,-z,noexecstack -Qunused-arguments -Wl,-z,relro -Wl,-z,now -shared -Wl,-soname,libnative-lib.so -o ........\build\intermediates\cmake\arm8\debug\obj\arm64-v8a\libnative-lib.so CMakeFiles/native-lib.dir/src/main/cpp/native-lib.cpp.o -llog -lm "C:/Users/Juan/AppData/Local/Android/sdk/ndk-bundle/sources/cxx-stl/gnu-libstdc++/4.9/libs/arm64-v8a/libgnustl_static.a" && cd ."
CMakeFiles/native-lib.dir/src/main/cpp/native-lib.cpp.o: In function Java_es_um_mompes_testopencl_MainActivity_stringFromJNI':
C:\Users\Juan\AndroidStudioProjects\TestOpenCL\app\src\main\cpp/native-lib.cpp:14: undefined reference toclGetPlatformIDs'
clang++.exe: error: linker command failed with exit code 1 (use -v to see invocation)
Do you know what am I missing? I've searching for a few hours over the last two days about how to setup OpenCL in Android Studio but everything I find seems to be outdated and doesn't seem to work for my problem.
Look for Android.mk under jni folder. Then add this:
LOCAL_PATH := $(call my-dir) // should already be there
.
.
LOCAL_LDLIBS := $(LOCAL_PATH)/libOpenCL.so
This example takes the libOpenCL.so from the jni root but you can change this for your needs.
Reference: Getting Started with OpenCL on Android
I would recommend you replace the automatic Android Studio NDK building with your own so you have total control.
Then you can use #RonTLV answer to add the library to the Android.mk and have a proper build.
Android Studio by default ignores Android.mk and generates its own with all the .c/.cpp files in the jni folder.
sourceSets {
debug.jniLibs.srcDir 'src/main/obj/local' //debug builds have symbols
release.jniLibs.srcDir 'src/main/libs' //release builds have cleaned libraries
main.jni.srcDirs = [] //disable automatic ndk-build call with auto-generated Android.mk
}
// call regular ndk-build script manually
task ndkBuild(type: Exec) {
if (System.properties['os.name'].toLowerCase(Locale.ROOT).contains('windows')) {
commandLine 'ndk-build.cmd', '-j8', '-C', file('src/main').absolutePath
} else {
commandLine 'ndk-build', '-j8', '-C', file('src/main').absolutePath
}
}
//Make the NDK task depend on Java build Task
tasks.withType(JavaCompile) {
compileTask -> compileTask.dependsOn ndkBuild
}
I have simple Gradle project and trying to add native library building it using CMake
Root CMakeLists.txt
project(mine_lib)
cmake_minimum_required(VERSION 2.8 FATAL_ERROR)
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake")
set(EXECUTABLE_OUTPUT_PATH "${PROJECT_SOURCE_DIR}/bin")
configure_file("${PROJECT_SOURCE_DIR}/src/buildinfo/buildinfo.hpp.in" "${PROJECT_BINARY_DIR}/buildinfo.hpp")
set(CMAKE_VERBOSE_MAKEFILE on)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${warnings}")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${warnings}")
add_subdirectory(${PROJECT_SOURCE_DIR}/src)
include_directories(${PROJECT_SOURCE_DIR}/src/data)
include_directories("${PROJECT_BINARY_DIR}")
add_library(mine_lib SHARED "${PROJECT_SOURCE_DIR}/src/jni/jniwrapper.c")
build.gradle
android {
compileSdkVersion 25
buildToolsVersion "25.0.3"
defaultConfig {
applicationId "..."
minSdkVersion 16
targetSdkVersion 25
versionCode 1
versionName "1.0"
defaultConfig {
externalNativeBuild {
cmake {
arguments "-DANDROID_STL=system"
cppFlags "-fexceptions"
}
}
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
externalNativeBuild {
cmake {
path "src/main/mine_lib/CMakeLists.txt"
}
}
sourceSets {
main {
jniLibs.srcDirs 'src/main/mine_lib/src/'
}
}
}
In my native library I use part of STL library, for example cstdint. And for now I'm getting compilation error which looks like that:
Error:FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:externalNativeBuildDebug'.
> Build command failed.
Error while executing process /AndroidSDK/android-sdk-macosx/cmake/3.6.4111459/bin/cmake with arguments {--build /Users/user/IdeaProjects/Sandbox/MyApp/app/.externalNativeBuild/cmake/debug/mips64 --target mine_lib}
[1/2] Building C object CMakeFiles/mine_lib.dir/src/jni/jniwrapper.c.o
FAILED: /AndroidSDK/android-sdk-macosx/ndk-bundle/toolchains/llvm/prebuilt/darwin-x86_64/bin/clang --target=mips64el-none-linux-android --gcc-toolchain=/AndroidSDK/android-sdk-macosx/ndk-bundle/toolchains/mips64el-linux-android-4.9/prebuilt/darwin-x86_64 --sysroot=/AndroidSDK/android-sdk-macosx/ndk-bundle/sysroot -Dheif_EXPORTS -I/Users/user/IdeaProjects/Sandbox/HEIFGallery/app/src/main/mine_lib/src/reader -I. -isystem /AndroidSDK/android-sdk-macosx/ndk-bundle/sysroot/usr/include/mips64el-linux-android -D__ANDROID_API__=21 -g -DANDROID -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -fintegrated-as -Wa,--noexecstack -Wformat -Werror=format-security -O0 -fno-limit-debug-info -fPIC -MD -MT CMakeFiles/mine_lib.dir/src/jni/jniwrapper.c.o -MF CMakeFiles/mine_lib.dir/src/jni/jniwrapper.c.o.d -o CMakeFiles/mine_lib.dir/src/jni/jniwrapper.c.o -c /Users/user/IdeaProjects/Sandbox/MyApp/app/src/main/mine_lib/src/jni/jniwrapper.c
In file included from /Users/user/IdeaProjects/Sandbox/MyApp/app/src/main/mine_lib/src/jni/jniwrapper.c:2:
/Users/user/IdeaProjects/Sandbox/MyApp/app/src/main/mine_lib/src/data/dataparser.hpp:16:10: fatal error: 'cstdint' file not found
#include <cstdint>
^~~~~~~~~
1 error generated.
ninja: build stopped: subcommand failed.
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
BUILD FAILED in 0s
What is the problem really is and how can I solve it?
Based on my understanding the problem could be solved by using a different C++ Library.
Currently you are specifying the following within the build.gradle file:
arguments "-DANDROID_STL=system"
Which, as stated within the following documentation, is:
system: The minimal system C++ runtime library and the default runtime
You'll probably need to change that to another C++ Library but I am not sure which one it would be, could be one of the gnu options. Better yet, it could be solved by removing the statement entirely.
I'm having trouble using native OpenCv 3.0.0 with Android Studio 2.2, with new ndk support i.e usin CMAKE build script. Below is the error which i am getting.Am i missing any thing in my gradle or cmake file? Please let me know.
Error:FAILURE: Build failed with an exception.
What went wrong:
Execution failed for task ':app:externalNativeBuildDebug'.
Build command failed.
Error while executing 'C:\Users\User\AppData\Local\Android\sdk\cmake\3.6.3155560\bin\cmake.exe' with arguments {--build E:\OpenCvAndroid\OPecvTry2CSupport\app.externalNativeBuild\cmake\debug\mips64 --target native-lib}
[1/1] Linking CXX shared library ..\obj\mips64\libnative-lib.so
FAILED: cmd.exe /C "cd . && C:\Users\User\AppData\Local\Android\sdk\ndk-bundle\toolchains\llvm\prebuilt\windows-x86_64\bin\clang++.exe -target mips64el-none-linux-android -gcc-toolchain C:/Users/User/AppData/Local/Android/sdk/ndk-bundle/toolchains/mips64el-linux-android-4.9/prebuilt/windows-x86_64 --sysroot=C:/Users/User/AppData/Local/Android/sdk/ndk-bundle/platforms/android-21/arch-mips64 -fPIC -g -DANDROID -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -Wa,--noexecstack -Wformat -Werror=format-security -fno-exceptions -fno-rtti -O0 -fno-limit-debug-info -Wl,--build-id -Wl,--warn-shared-textrel -Wl,--fatal-warnings -Wl,--no-undefined -Wl,-z,noexecstack -Qunused-arguments -Wl,-z,relro -Wl,-z,now -shared -Wl,-soname,libnative-lib.so -o ..\obj\mips64\libnative-lib.so CMakeFiles/native-lib.dir/src/main/cpp/native-lib.cpp.o -llog -lm "C:/Users/User/AppData/Local/Android/sdk/ndk-bundle/sources/cxx-stl/gnu-libstdc++/4.9/libs/mips64/libgnustl_static.a" && cd ."
CMakeFiles/native-lib.dir/src/main/cpp/native-lib.cpp.o: In function ~Mat':
E:/OpenCVSdk/sdk/native/jni/include\opencv2/core/mat.hpp:278: undefined reference tocv::fastFree(void*)'
CMakeFiles/native-lib.dir/src/main/cpp/native-lib.cpp.o: In function cv::Mat::release()':
E:/OpenCVSdk/sdk/native/jni/include\opencv2/core/mat.hpp:367: undefined reference tocv::Mat::deallocate()'
clang++.exe: error: linker command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: subcommand failed.
And my Cmake.txt
cmake_minimum_required(VERSION 3.4.1)
add_library( native-lib
SHARED
src/main/cpp/native-lib.cpp )
include_directories(E\:\\OpenCVSdk\\sdk\\native\\jni\\include )
find_library( log-lib log )
target_link_libraries(native-lib ${log-lib} )
And my Gradle Build file
{ apply plugin: 'com.android.application'
android {
compileSdkVersion 24
buildToolsVersion "24.0.2"
defaultConfig {
applicationId "viki.opecvtry2csupport"
minSdkVersion 16
targetSdkVersion 24
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
externalNativeBuild {
cmake {
cppFlags ""
}
}
}
I fixed my problem by add "lib_opencv" lib to target_link_libraries
target_link_libraries(
native-lib lib_opencv #Just add the lib_opencv
${log-lib} )
don't forget to Link Gradle to your native library
my full CMakeList.txt
cmake_minimum_required(VERSION 3.4.1)
add_library( native-lib
SHARED
src/main/cpp/native-lib.cpp )
find_library( log-lib
log )
target_link_libraries(native-lib lib_opencv
${log-lib} )
set(CMAKE_VERBOSE_MAKEFILE on)
add_library(lib_opencv SHARED IMPORTED)
set_target_properties(lib_opencv PROPERTIES IMPORTED_LOCATION /PROJECT_PATH/openCVLibrary320/src/main/jniLibs/${ANDROID_ABI}/libopencv_java3.so)
include_directories(/OpenCV-android-sdk/sdk/native/jni/include)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11")
Setting OpenCV_DIR has worked for me with OpenCV 3.1 and AS 2.2 and the latest Gradle plugin. This is the relevant bit of my CMakeLists.txt.
cmake_minimum_required(VERSION 3.6)
SET(OpenCV_DIR $ENV{HOME}/AndroidDevelopment/opencv-3.1.0/sdk/native/jni)
find_package(OpenCV REQUIRED)
message(STATUS "opencv found: ${OpenCV_LIBS}")
include_directories(${CMAKE_CURRENT_SOURCE_DIR}
${OpenCV_DIR}/include/)
[...]
target_link_libraries(myTarget log ${OpenCV_LIBS} m z android )