I'm using NDK to use C++ native code in OpenCV through Android studio.
Every thing is alright except when I'm running this project to an android device I have the following error
Warning:Native C/C++ source code is found, but it seems that NDK option is not configured. Note that if you have an Android.mk, it is not used for compilation. The recommended workaround is to remove the default jni source code directory by adding:
android {
sourceSets {
main {
jni.srcDirs = []
}
}
}
and also
to build.gradle, manually compile the code with ndk-build, and then place the resulting shared object in src/main/jniLibs.
C:\Users\MALIK\Desktop\ImageJ\app\src\main\jni\com_example_malik_imagej_DetectionBasedTracker.cpp
Error:(2, 10) 'opencv2/opencv.hpp' file not found 1 error generated.
and when I do the workaround and make jni.srcDir empty , I got error that "cannot resolve corresponding JNI function "
so the native code won't run and the app crashes , what should I do in this case?
this is my gradle :
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.1"
defaultConfig {
applicationId "com.example.malik.haar"
minSdkVersion 16
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
task ndkBuild(type: Exec, description: 'Compile JNI source via NDK') {
commandLine "C:/Android/sdk/ndk-bundle/ndk-build.cmd",
'NDK_PROJECT_PATH=build/intermediates/ndk',
'NDK_LIBS_OUT=src/main/jniLibs',
'APP_BUILD_SCRIPT=src/main/jni/Android.mk',
'NDK_APPLICATION_MK=src/main/jni/Application.mk'
}
tasks.withType(JavaCompile) {
compileTask -> compileTask.dependsOn ndkBuild
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
sourceSets { main { jni.srcDirs = [] } }
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:appcompat-v7:23.1.1'
androidTestCompile files('libs/junit-4.12.jar')
compile project(':openCVLibrary320')
}
and this is my Android.mk file :
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
OPENCVROOT:=C:\OpenCV-android-sdk
OPENCV_INSTALL_MODULES:=on
OPENCV_LIB_TYPE:=SHARED
include $(OPENCVROOT)/sdk/native/jni/OpenCV.mk
LOCAL_SRC_FILES := com_example_malik_imagej_DetectionBasedTracker.cpp
LOCAL_C_INCLUDES += $(LOCAL_PATH)
LOCAL_LDLIBS += -llog -ldl
LOCAL_MODULE := detection_based_tracker
include $(BUILD_SHARED_LIBRARY)
and this is my Header file of native code :
thanks.
Related
I am using latest version of ndk (18.1). Whenever I try to compile my code it shows me Execution failed for task ':app:ndkBuild'
I tried 2 different versions of ndk (18.1) and (14.1), Both shows almost same error while the only difference is that;
ndk (18.1) shows Exit Value 2
ndk (14.1) shows Exit Value -1
jni Path:
Build.Gradle:
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.fyp.major.fyp1"
minSdkVersion 21
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
sourceSets.main {
jni.srcDirs = [] //disable automatic ndk-build call
}
task ndkBuild(type: Exec, description: 'Compile JNI source via NDK') {
commandLine "C:/Users/MaJoR/AppData/Local/Android/Sdk/ndk-bundle/ndk-build.cmd",
'NDK_PROJECT_PATH=build/intermediates/ndk',
'NDK_LIBS_OUT=src/main/jniLibs',
'APP_BUILD_SCRIPT=src/main/jni/Android.mk',
'NDK_APPLICATION_MK=src/main/jni/Application.mk'
}
tasks.withType(JavaCompile) {
compileTask -> compileTask.dependsOn ndkBuild
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:28.0.0-alpha1'
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(':openCVLibrary344')
}
ANDROID.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
#opencv
OPENCVROOT:= C:\OpenCV-3.4.0-android-sdk
OPENCV_CAMERA_MODULES:=on
OPENCV_INSTALL_MODULES:=on
OPENCV_LIB_TYPE:=SHARED
include ${OPENCVROOT}/sdk/native/jni/OpenCV.mk
LOCAL_SRC_FILES := com_fyp_major_fyp1_OpencvClass.cpp
LOCAL_LDLIBS += -llog
LOCAL_MODULE := MyLibs
include $(BUILD_SHARED_LIBRARY)
APPLICATION.mk
APP_STL := gnustl_static
APP_CPPFLAGS := -frtti -fexceptions
APP_ABI := armeabi-v7a
APP_PLATFORM := android-16
OUTPUT Error while using ndk 18.1
OUTPUT Error while using ndk 14.1
Any Solution for these Errors?
NDK r18 has dropped gnustl, so you must rebuild OpenCV for libc++.
As for NDK r14.1, there is too little info to explain the failure. Note that the screenshot mentions that ndk-stack.cmd and not ndk-build.cmd was run there. Could it be a typo when you switched to the older NDK?
I am trying to write a C++ file in Android Studio and I would like to use OpenCV in my project.
However, when I try to use the following includes, I get an error saying Cannot Find 'opencv2'.
#include <opencv2/core.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/features2d.hpp>
To set up OpenCV in Android, you have to take the following steps, found here, to use the Java part of OpenCV in Android Studio.
To the best of my knowledge to use the C++ part of OpenCV in Android Studio, you have to add Android.mk and Application.mk to the folder containing your native code.
My Android.mk file looks like the following.
LOCAL_PATH := $(call my-dir)
CVROOT := C:/Users/Dan/Documents/Repos/Android-Studio/Assets/opencv-3.4.1-android-sdk/OpenCV-android-sdk/sdk/native/jni
include $(CLEAR_VARS)
OPENCV_CAMERA_MODULES:=on
OPENCV_INSTALL_MODULES:=on
OPENCV_LIB_TYPE:=SHARED
include $(CVROOT)/OpenCV.mk
LOCAL_MODULE := app
LOCAL_SRC_FILES := native-lib.cpp
LOCAL_LDLIBS += -llog -ldl
include $(BUILD_SHARED_LIBRARY)
My Application.mk file looks like the following.
APP_STL := gnustl_static
APP_CPPFLAGS := -frtti -fexceptions
APP_ABI := armeabi-v7a
APP_PLATFORM := android-27
Am I doing something wrong to set my project up to use the C++ part of OpenCV on Android?
Additional Information for if it makes a difference
My project outline looks like this.
My gradles look like the following
app module
apply plugin: 'com.android.application'
android {
compileSdkVersion 27
defaultConfig {
applicationId "com.test.test.esra"
minSdkVersion 24
targetSdkVersion 27
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
externalNativeBuild {
cmake {
cppFlags "-frtti -fexceptions"
}
}
}
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:27.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
// Room - Managing local databases
implementation 'android.arch.persistence.room:runtime:1.0.0'
annotationProcessor "android.arch.persistence.room:compiler:1.0.0"
//Jetbrains Annotations
implementation 'org.jetbrains:annotations-java5:15.0'
implementation project(':openCVLibrary341')
}
openCVLibrary341 module
apply plugin: 'com.android.library'
android {
compileSdkVersion 27
defaultConfig {
minSdkVersion 24
targetSdkVersion 27
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
}
project build.gradle
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
For anyone else who arrives here with the same issue, the problem is fairly easy to fix.
You no longer need the .mk files so they can be deleted.
If you are starting a project from scratch, make sure you have Android NDK and CMake installed as well as enabling them for your project.
After following the initial steps posted in this answer to set up the Java part of OpenCV on Android. You will need to change your app module's build gradle to reflect the snippet below.
defaultConfig {
...
externalNativeBuild {
cmake {
cppFlags "-frtti -fexceptions"
abiFilters 'x86', 'x86_64', 'armeabi', 'armeabi-v7a', 'arm64-v8a', 'mips', 'mips64'
}
}
}
sourceSets {
main {
jniLibs.srcDirs = ['src/main/jniLibs']
}
}
Then you want to edit the file CMakeLists.txt. In this file you want to include the following code underneath the cmake_minimum_required(VERSION X.X.X) line.
Also, you will need to add lib_opencv to the target_link_libraries near the bottom of CMakeLists.txt. This will help prevent undefined reference errors.
include_directories(your-path-to/OpenCV-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_java3.so)
...
target_link_libraries( # Specifies the target library.
native-lib
# OpenCV lib
lib_opencv
# Links the target library to the log library
# included in the NDK.
${log-lib} )
Make sure to replace your-path-to with your actual path to OpenCV for Android.
Finally, clean your project and refresh your linked C++ projects. This will remove the error.
I got this information from this really nice GitHub page. Credits to leadrien
Clarification on You no longer need the .mk files. This is true for Android Studio 3. I have not tested this on older versions of Android Studio or Eclipse, so you might need .mk files for those IDEs.
I am trying to run this project .Every time i run the project i get the below error:
java.lang.UnsatisfiedLinkError: dlopen failed: /data/app/path.androidspeakerrec-1/lib/arm/libndkspeaker.so: has text relocations
The project does not load the *.so files. I have changed location of so file from libs to jniLibs, but still get the same issue.
Build .gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
defaultConfig {
applicationId "com.sharannya.androidspeakerrec"
minSdkVersion 15
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
externalNativeBuild {
cmake {
cppFlags ""
}
}
ndk {
abiFilters "armeabi-v7a","armeabi"
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
externalNativeBuild {
cmake {
path "CMakeLists.txt"
}
}
}
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:appcompat-v7:25.1.0'
testCompile 'junit:junit:4.12'
}
Call from java code
static {
try {
System.loadLibrary("ndkspeaker");
} catch (UnsatisfiedLinkError e) {
Log.e("Error in loading lib","Native code library failed to load" + e);
}
}
Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := ndkspeaker
LOCAL_LDLIBS := -llog
LOCAL_SRC_FILES := lib_mfcc/abs.c \
lib_mfcc/abs1.c \
lib_mfcc/fft.c \
lib_mfcc/fi_fft.c \
lib_mfcc/fi_mfcc.c \
lib_mfcc/fi_mfcc_initialize.c \
lib_mfcc/fi_mfcc_rtwutil.c \
lib_mfcc/fi_mfcc_terminate.c \
lib_mfcc/log.c \
lib_mfcc/mfcc_bare.c \
lib_mfcc/mtimes1.c \
lib_mfcc/power1.c \
lib_mfcc/rt_nonfinite.c \
lib_mfcc/rtGetInf.c \
lib_mfcc/rtGetNaN.c \
lib_mfcc/sqrt.c \
lib_mfcc/sum.c \
SpeakerRecognizer.c
APP_ABI := armeabi
include $(BUILD_SHARED_LIBRARY)
The .so file is located in project/app/src/main/jniLibs/armeabi
and project/app/src/main/jniLibs/armeabi-v7a
How do i fix this?
You may have to set the 'jniLibs.srcDirs' in the gradle file. You can take a look here - actually, the code in the question should help you: How to setup NDK libs path in Gradle?
After some time of digging into the code, i got the solution to the problem.
The project i was trying to run was an older project with Android mk ndk build .So i removed all the cpp folder, cmake file from the project.
Then right clicked on libraries and selected the option "Link c++ with gradle". On click of the option , it will show a window to choose type of build system.Select "ndk build" and link the Android.mk path to the project.
I have an android application which is been using OpenGL libraries and PVRTC Tool.
This application was running perfectly on Eclipse till API 19. Now I want to move this till API 23 so think to port the application to Android Studio as well for future updates sync. I am facing problems in this porting please look up the screenshots to see the folder hierarchy I have in Eclipse running code
[
[
Following was my Android.mk file:
LOCAL_PATH := $(call my-dir)
PVRSDKDIR := $(LOCAL_PATH)
include $(PVRSDKDIR)/Tools/OGLES2/Build/Android/Android.mk
include $(CLEAR_VARS)
LOCAL_MODULE := nativeegl
LOCAL_CFLAGS := -DBUILD_OGLES2 -Wall -g
LOCAL_SRC_FILES := CCAppMain.cpp
LOCAL_LDLIBS := -llog -landroid -lEGL -lGLESv2# -lGLESv1_CM
LOCAL_STATIC_LIBRARIES := \
ogles2tools \
android_native_app_glue
LOCAL_C_INCLUDES := \
$(PVRSDKDIR)/Builds/OGLES2/Include \
$(PVRSDKDIR)/Tools \
$(PVRSDKDIR)/Tools/OGLES2
include $(BUILD_SHARED_LIBRARY)
$(call import-module,android/native_app_glue)
And this is respective Application.mk file in Eclipse
# The ARMv7 is significanly faster due to the use of the hardware FPU
#APP_ABI := armeabi armeabi-v7a x86
#APP_OPTIM := release
APP_STL := stlport_static
I am using Android Studio Version 1.4 , Gradle Built 2.5
have set classpath as "classpath 'com.android.tools.build:gradle-experimental:0.2.0' "
My app module built.gradle looks like
apply plugin: 'com.android.model.application'
model {
android {
compileSdkVersion = 23
buildToolsVersion = "23.0.1"
defaultConfig.with {
applicationId = "xxx"
minSdkVersion.apiLevel = 14
targetSdkVersion.apiLevel = 23
versionCode = 1
versionName = "1.0"
}
}
compileOptions.with {
sourceCompatibility=JavaVersion.VERSION_1_7
targetCompatibility=JavaVersion.VERSION_1_7
}
android.ndk {
moduleName = "nativeegl"
//cppFlags += "-I${file("src/main/jni/native_app_glue")}".toString()
cppFlags += "-I${file("src/main/jni")}".toString()
cppFlags += "-I${file("src/main/jni/Tools")}".toString()
cppFlags += "-I${file("src/main/jni/Tools/OGLES2")}".toString()
cppFlags += "-I${file("src/main/jni/Tools/OGLES2/GLES2")}".toString()
ldLibs += ["android", "EGL", "GLESv2", "OpenSLES", "log"]
stl = "stlport_static"
}
android.buildTypes {
release {
minifyEnabled = false
proguardFiles += file('proguard-rules.txt')
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.0.1'
compile 'com.android.support:support-v4:23.0.1'
}
I am really not able to configure the application at moment, On syncing it always giving errors such as
Error:Execution failed for task ':app:compileArmeabi-v7aDebugNativeeglSharedLibraryNativeeglMainCpp'.
> Multiple build operations failed.
C++ compiler failed while compiling PVRTPrint3D.cpp.
C++ compiler failed while compiling PVRTBackground.cpp.
C++ compiler failed while compiling PVRTShader.cpp.
C++ compiler failed while compiling PVRTPrint3DAPI.cpp.
C++ compiler failed while compiling PVRTPFXParserAPI.cpp.
See the complete log at: file:///E:/Android_Studio_Project/XXX/app/build/tmp/compileArmeabi-v7aDebugNativeeglSharedLibraryNativeeglMainCpp/output.txt
Can anyone look into it and make correction to my build.gradle file. I am guessing that PVRTC tool is been ignored in it.
Any help will be appreciated.
You are not obliged to forsake your carefully crafted Android.mk for the unpolished and limited DSL of the experimental gradle plugin. See more at define LOCAL_SRC_FILES in ndk{} DSL.
The specific problem in your case seems to be not that the PVRTC files are ignored, but that they are not compiled according to the rules and flags as configured in Tools/OGLES2/Build/Android/Android.mk.
TL;NR: append the following lines to you build.gradle:
def ndkBuild = android.ndkDirectory
import org.apache.tools.ant.taskdefs.condition.Os
if (Os.isFamily(Os.FAMILY_WINDOWS)) {
ndkBuild += '.cmd'
}
task buildNative(type: Exec, description: 'Compile JNI source via NDK') {
commandLine '$ndkBuild', '-C', 'src/main'
}
task cleanNative(type: Exec, description: 'Clean JNI object files') {
commandLine '$ndkBuild', 'clean', '-C', 'src/main'
}
clean.dependsOn 'cleanNative'
tasks.all {
task ->
if (task.name.startsWith('compile') && task.name.contains('MainC')) {
task.enabled = false
}
if (task.name.startsWith('link')) {
task.enabled = false
}
if (task.name.endsWith("SharedLibrary") ) {
task.dependsOn buildNative
}
}
android {
sourceSets {
main {
jniLibs.srcDirs = ['src/main/libs']
}
}
}
After spending hours and hours searching for a solution to my problem and browsing the various forums (including this one) i finally decided to ask my question, hoping that it has not been fully answered elsewhere.
I'm trying to build a pretty complex architecture :
I have C sources that i compile in separates static libraries (.a)
I use them via JNI in a module Library
I want to use this library in an app project.
I first successfully did the following tests
- I already managed to make a module library without NDK and compile it with the app.
- I also managed to use the static libraries and JNI directly in the app but
I am failing with the following step :
- The combination of JNI inside the module and app calling the classes of the module doesn't work.
I think the problem is with the include of the aar, because I can't find the exploded-aar in the build directory of my app while the aar is in the library build/outputs directory. Besides, all the previous tests (including the use of
JNI were successful).
I'm not using the experimental model because it is experimental and there are known limitations with static libraries.
The structure of my project is :
- App
- src
- main
- java
- activity
- bar
- src
- main
- java
- class
- jni
- include
- *.h
- libs
- abis...
- libmod1.a
- libmod2.a
Android.mk
Application.mk
bar.c
bar.h
The app build.gradle looks like this :
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.0"
defaultConfig {
applicationId "com.test.foo"
minSdkVersion 10
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
debuggable false
jniDebuggable false
minifyEnabled false
}
unsigned {
debuggable false
jniDebuggable false
minifyEnabled false
}
debug {
debuggable true
jniDebuggable true
minifyEnabled false
}
}
productFlavors {
x86 {
ndk {
abiFilter "x86"
}
}
mips {
ndk {
abiFilter "mips"
}
}
armv7 {
ndk {
abiFilter "armeabi-v7a"
}
}
arm {
ndk {
abiFilter "armeabi"
}
}
fat
}
project.ext.versionCodes = ['armeabi':1, 'armeabi-v7a':2, 'arm64-v8a':3, 'mips':5, 'mips64':6, 'x86':8, 'x86_64':9]
android.applicationVariants.all { variant ->
variant.outputs.each { output ->
output.versionCodeOverride =
project.ext.versionCodes.get(output.getFilter(com.android.build.OutputFile.ABI), 0) * 1000000 + defaultConfig.versionCode
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar','*.aar'])
compile project( ":bar" )
}
The module build.gradle looks like this :
apply plugin: 'com.android.library'
android {
compileSdkVersion 23
buildToolsVersion "23.0.0"
defaultConfig {
minSdkVersion 10
targetSdkVersion 23
versionCode 1
versionName "1.0"
ndk {
moduleName "module"
}
}
buildTypes {
release {
debuggable false
jniDebuggable false
minifyEnabled false
}
unsigned {
debuggable false
jniDebuggable false
minifyEnabled false
}
debug {
debuggable true
jniDebuggable true
minifyEnabled false
}
}
productFlavors {
x86 {
ndk {
abiFilter "x86"
}
}
mips {
ndk {
abiFilter "mips"
}
}
armv7 {
ndk {
abiFilter "armeabi-v7a"
}
}
arm {
ndk {
abiFilter "armeabi"
}
}
fat
}
sourceSets.main {
jniLibs.srcDir 'src/main/libs'
jni.srcDirs = []
}
task ndkBuild(type: Exec) {
commandLine android.ndkDirectory.getAbsolutePath()+'/ndk-build', '-C', file('src/main').absolutePath
}
tasks.withType(JavaCompile) {
compileTask -> compileTask.dependsOn ndkBuild
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
}
My Android.mk inside the module jni directory is :
LOCAL_PATH:= $(call my-dir)
#### Mod1
include $(CLEAR_VARS)
LOCAL_MODULE := mod1
LOCAL_SRC_FILES := libs/$(TARGET_ARCH_ABI)/libmod1.a
include $(PREBUILT_STATIC_LIBRARY)
#### Mod2
include $(CLEAR_VARS)
LOCAL_MODULE := pxnav
LOCAL_SRC_FILES := libs/$(TARGET_ARCH_ABI)/libmod2.a
LOCAL_STATIC_LIBRARIES := pxfd
include $(PREBUILT_STATIC_LIBRARY)
##### Parser
include $(CLEAR_VARS)
LOCAL_MODULE := module
LOCAL_C_INCLUDES := $(LOCAL_PATH)/include
LOCAL_LDLIBS += -landroid -llog
LOCAL_SRC_FILES := bar.c
LOCAL_STATIC_LIBRARIES := mod1 mod2
include $(BUILD_SHARED_LIBRARY)
I found an easy workaround for similar task: I simply set jniLibs.srcDir for the app module. Now I don't depend on exploding the aar to get the .so files in place. BTW, when you decide to switch to experimental plugin (to use native debugging, C++ syntax highlighting and cross-references and other niceties), you are invited to check define LOCAL_SRC_FILES in ndk{} DSL.