I am trying to use a native opencv method in AndroidStudio, but I have a problem when I try to include the opencv package in native.lib.cpp.
I already followed different tutorials trying to solve this problem. The most useful was this one. But my problem is that I got an error on the code
#include <opencv2/core.hpp>
"Cannot find 'opencv2' "
See picture: https://i.stack.imgur.com/6SHxF.png
Following the previous tutorial, that is also described in this thread, I get these files.
app build.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "25.0.3"
defaultConfig {
applicationId "esp1718.android.deblur"
minSdkVersion 16
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
externalNativeBuild {
cmake {
cppFlags "-std=c++11 -frtti -fexceptions"
abiFilters 'x86', 'x86_64', 'armeabi-v7a', 'arm64-v8a'
}
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
sourceSets {
main {
jniLibs.srcDirs = ['C:/Users/erikb_000/AndroidStudioProjects/DeBlurGit/app/src/main/jniLibs']
}
}
externalNativeBuild {
cmake {
path "CMakeLists.txt"
}
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
compile 'com.android.support:design:25.3.1'
testCompile 'junit:junit:4.12'
compile project(':openCVLibrary342')
}
CMakeLists.txt
set(pathToProject C:/Users/erikb_000/AndroidStudioProjects/DeBlurGit)
set(pathToOpenCV C:/Users/erikb_000/Desktop/Erik/Università/EmbeddedSystemsProgramming/Project/OpenCV-android-sdk)
# 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(CMAKE_VERBOSE_MAKEFILE on)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11")
include_directories(${pathToOpenCV}/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/cpp/native-lib.cpp )
add_library(lib_opencv SHARED IMPORTED)
set_target_properties(lib_opencv PROPERTIES IMPORTED_LOCATION ${pathToProject}/app/src/main/jniLibs/${ANDROID_ABI}/libopencv_java342.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( # Specifies the target library.
native-lib
lib_opencv
# Links the target library to the log library
# included in the NDK.
${log-lib} )
native-lib.cpp
#include <jni.h>
#include <string>
#include <opencv2/core.hpp>
extern "C" {
JNIEXPORT jstring JNICALL
Java_esp1718_android_deblur_MainActivity_stringFromJNI(
JNIEnv *env,
jobject /* this */) {
std::string hello = "Hello from C++";
return env->NewStringUTF(hello.c_str());
}
jstring
Java_esp1718_android_deblur_MainActivity_validate(JNIEnv *env, jlong addGray, jlong addrRgba) {
cv::Rect();
cv::Mat();
std::string hello2 = "Hello from validate";
return env->NewStringUTF(hello2.c_str());
}
}
Note that, unlike the tutorials I found, in the gradle file, in the line
abiFilters 'x86', 'x86_64', 'armeabi', 'armeabi-v7a', 'arm64-v8a', 'mips', 'mips64'
I had to delete 'armeabi' 'mips' 'mips64'
because AndroidStudio gave me some errors saying that they are no longer supported.
I also found a "solution" to this importation problem in another thread, that said to delete these lines from the gradle file
externalNativeBuild {
cmake {
path "CMakeLists.txt"
}
}
It actually solved the #include <opencv2/core.hpp>
problem, but generated another one on my mainActivity when I tried to call a native method. So it was not much exaustive. In particular see this picture: https://i.stack.imgur.com/P45um.png
Is there anything wrong in my files? Among the youtube tutorial comments I noticed that other people faced the same problem, and no one found a solution yet.