I am trying to integrate OpenCv with android studio and in the process NDK issues are being faced by me . The NDK always remains unselected in the Project Structure.The header files always remain unavailable because the path get unmentioned . I am trying to resolve this issue since days but not able to find the solution of the above if someone would help. The NDK does not connect , though having been tried many ways to integrate NDK. I had used all possible ways of integrating the NDK and have taken possible measure to replicate my needs but nothing works. I have seen how others tried to resolve the issue but none of those worked for me.
Here below is my code CMakeLists.txt file
cmake_minimum_required(VERSION 3.4.1)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11")
include_directories(${OpenCV_DIR}/jni/include)
add_library( lib_opencv SHARED IMPORTED )
set_target_properties(lib_opencv PROPERTIES IMPORTED_LOCATION ${OpenCV_DIR}/libs/${ANDROID_ABI}/libopencv_java4.so)
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).
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 )
target_link_libraries( # Specifies the target library.
native-lib
# Links the target library to the log library
# included in the NDK.
${log-lib}
lib_opencv)
native-lib.cpp
#include <jni.h>
Here above the jni.h file is linted to be not found and hence the JNIEXPORT and JNICALL also remains hidden.
build.gradle(app)
apply plugin: 'com.android.application'
android {
compileSdkVersion 29
defaultConfig {
applicationId "com.karuneshpalekar.copencv"
minSdkVersion 21
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
externalNativeBuild {
cmake {
cppFlags "-frtti -fexceptions"
abiFilters 'x86', 'x86_64', 'armeabi-v7a', 'arm64-v8a'
arguments "-DOpenCV_DIR=" + opencvsdk + "/sdk/native"
}
}
ndk {
moduleName "test"
cFlags "-std=c++11 -fexceptions"
stl "gnustl_shared"
}
}
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"
}
}
packagingOptions {
pickFirst 'lib/armeabi-v7a/libopencv_java4.so'
pickFirst 'lib/arm64-v8a/libopencv_java4.so'
pickFirst 'lib/x86/libopencv_java4.so'
pickFirst 'lib/x86_64/libopencv_java4.so'
}
}
dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.0'
implementation project(path: ':java')
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}
build.gradle(java)
apply plugin: 'com.android.library'
println "OpenCV: " + project.buildscript.sourceFile
android {
compileSdkVersion 29
//buildToolsVersion "x.y.z" // not needed since com.android.tools.build:gradle:3.0.0
defaultConfig {
minSdkVersion 21
targetSdkVersion 29
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_6
targetCompatibility JavaVersion.VERSION_1_6
}
sourceSets {
main {
jniLibs.srcDirs = ['src/main/jniLibs']
java.srcDirs = ['src/main/java']
aidl.srcDirs = ['src']
res.srcDirs = ['/build/master_pack-android/opencv/modules/java/android_sdk/android_gradle_lib/res']
manifest.srcFile 'java/AndroidManifest.xml'
}
}
}
dependencies {
}
Thank-You for helping I have Ndk-rc21 and Android Studio AGP Version 4.0.1 and Gradle Versopion 6.1.1
The Issue I had was not with the NDK tool , it was with the directory where i had placed it.
There was space between file name which resulted in the error eg"User space Name".eg:C:/User/Elon Musk/SDK/ndk
I rectified this mistake of mine after a warning display by Android Studio lint .
I changed the Android SDK directory placement and rerun the code and the issue got resolved.
As per the answer above by Lakindu , there is no need for downgrading the Ndk library just make sure that the NDK is in the right place .
And also dont forget to add the NDK tool in the Project Structure which you can find by clicking Ctrl + Shift +Alt + S simultaneously.