OpenCV undefined reference to `cv::fastFree(void*)' - android

I try to build project with OpenCV, i downloaded OpenCV SDK 3.1 from official website, however during building i get error
/Users/Mario/Downloads/OpenCV-android-sdk/sdk/native/jni/include/opencv2/core/mat.inl.hpp
Error:(571) undefined reference to 'cv::fastFree(void*)'
Error:(663) undefined reference to 'cv::Mat::create(int, int const*, int)'
Error:(682) undefined reference to 'cv::Mat::deallocate()'
here is my gradle.build, i use gradle-experimental:0.4.0
apply plugin: 'com.android.model.application'
model {
android {
compileSdkVersion = 23
buildToolsVersion = "23.0.2"
defaultConfig.with {
applicationId = "pl.mariusz.opencv"
minSdkVersion.apiLevel = 17
targetSdkVersion.apiLevel = 23
versionCode = 1
versionName = "1.0"
}
task ndkBuild(type: Exec) {
commandLine '/Users/Mario/Library/Android/android-ndk-r10e/ndk-build', '-C', file('src/main').absolutePath
}
tasks.withType(JavaCompile) {
compileTask -> compileTask.dependsOn ndkBuild
}
}
android.ndk {
moduleName = "source_file"
cppFlags.add("-std=c++11")
cppFlags.add("-fexceptions")
cppFlags.add("-I${file("/Users/Mario/Downloads/OpenCV-android-sdk/sdk/native/jni/include")}".toString())
cppFlags.add("-I${file("/Users/Mario/Downloads/OpenCV-android-sdk/sdk/native/jni/include/opencv")}".toString())
stl = "gnustl_static"//"gnustl_static"//"gnustl_shared"//"stlport_static"
}
android.sources {
main {
jni{
source{
srcDirs = []
}
}
jniLibs {
source {
srcDirs = ['src/main/Libs']
}
}
}
}
android.buildTypes {
release {
minifyEnabled = false
proguardFiles.add(file('proguard-rules.txt'))
}
}
}
Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
OPENCVROOT:= /Users/Mario/Downloads/OpenCV-android-sdk
OPENCV_LIB_TYPE=STATIC
include ${OPENCVROOT}/sdk/native/jni/OpenCV.mk
LOCAL_SRC_FILES := source_file.cpp
LOCAL_LDLIBS += -llog -ldl
LOCAL_MODULE := source_file
include $(BUILD_SHARED_LIBRARY)
and Application.mk
APP_STL := gnustl_static
APP_CPPFLAGS := -frtti -fexceptions
APP_ABI := armeabi-v7a
APP_PLATFORM := android-17
i tried mix it with shared,static STL with still same error, how can i fix it?
[EDIT]
[SOLUTION]
its more like compromise not real solution, but i changed gradle-experimental:0.4.0 back to classpath 'com.android.tools.build:gradle:1.5.0' and added file gradle.properties to project root directory with
android.useDeprecatedNdk=true

You have to add opencv libraries to your linker. To do that, you have to do 2 things: 1. add the library files; 2. add the library directory.
Try
LOCAL_LDLIBS += -llog -ldl -LlibPath -lopencv_calib3d -lopencv_core
and so an, add all the opencv libraries (or at least the ones you'll need) in this way. -l will automatically assume the "lib" in the filename and the suffix.
The -L will tell the linker where to find libraries, so instead of libPath please add the absolute path of your opencv library folder. See how to mention path of libraries in Android.mk file or Application.mk file?
Try it and tell me the new error message.

Related

GStreamer Android SDK error for tutorial-5

We have taken clone of a project and made all settings as per instruction still not able to build app due to following error:
Error:(7, 10) fatal error: 'gst/gst.h' file not found
above error is in tutorial.c file:
#include <gst/gst.h>
#include <gst/video/video.h>
#include <gst/video/videooverlay.h>
#include <pthread.h>
GST_DEBUG_CATEGORY_STATIC (debug_category);
#define GST_CAT_DEFAULT debug_category
Please find below gradle and Android.mk file which I have added to my project:
Gradle
android {
compileSdkVersion 25
buildToolsVersion "25.0.0"
defaultConfig {
applicationId "com.example.ndktest"
minSdkVersion 19
targetSdkVersion 22
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
externalNativeBuild {
cmake {
cppFlags "-frtti -fexceptions"
arguments '-DANDROID_TOOLCHAIN=clang',
'-DANDROID_PLATFORM=android-19',
'-DANDROID_STL=gnustl_static',
'-DANDROID_ARM_NEON=TRUE',
'-DANDROID_CPP_FEATURES=exceptions rtti'
}
}
ndk {
moduleName "tutorial-5"
}
}
sourceSets.main {
jni.srcDirs = []
jniLibs.srcDir new File(buildDir, 'lib')
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
externalNativeBuild {
cmake {
path "CMakeLists.txt"
}
}
}
Android.mk
LOCAL_PATH := $(call my-dir)
GSTREAMER_ROOT_ANDROID := D:\ndk-gst1.9.1
SHELL := PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin /bin/bash
include $(CLEAR_VARS)
LOCAL_MODULE := tutorial-5
LOCAL_SRC_FILES := tutorial-5.c
LOCAL_SHARED_LIBRARIES := gstreamer_android
LOCAL_LDLIBS := -llog -landroid
include $(BUILD_SHARED_LIBRARY)
ifndef GSTREAMER_ROOT
ifndef GSTREAMER_ROOT_ANDROID
$(error GSTREAMER_ROOT_ANDROID is not defined!)
endif
GSTREAMER_ROOT := $(GSTREAMER_ROOT_ANDROID)
endif
GSTREAMER_NDK_BUILD_PATH := $(GSTREAMER_ROOT)/share/gst-android/ndk-build
include $(GSTREAMER_NDK_BUILD_PATH)/plugins.mk
GSTREAMER_PLUGINS := $(GSTREAMER_PLUGINS_CORE) $(GSTREAMER_PLUGINS_PLAYBACK) $(GSTREAMER_PLUGINS_CODECS) $(GSTREAMER_PLUGINS_NET) $(GSTREAMER_PLUGINS_SYS) $(GSTREAMER_PLUGINS_CODECS_RESTRICTED)
G_IO_MODULES := gnutls
GSTREAMER_EXTRA_DEPS := gstreamer-video-1.0
include $(GSTREAMER_NDK_BUILD_PATH)/gstreamer-1.0.mk
Please suggest what changes need to be done in above code to make it run.
I already answered a similar question... The problem with the official tutorials from the Gstreamer site is that they are old and made for Eclipse. Maybe your error is because you didn't link your Android Studio project to the C++ files... to do it follow the second step from this answer: Gstreamer examples in Android Studio
Since the linking with C++ files ins't the only error that will occurs I also suggest that you follow all other steps. There's a link for gitlab project with "turorial 5" working in the link above.

UnsatisfiedLinkError while trying to load opencv library

I am getting this error:
java.lang.UnsatisfiedLinkError: dlopen failed: file offset for the library "libopencv_java3.so" >= file size: 0 >= 0
when I am trying to link openCV native code with my java code. I tried a number of things but nothing resolved this error. Please suggest me any solutions.
Android.mk
OPENCV_INSTALL_MODULES:=on
OPENCV_LIB_TYPE:=STATIC
ifdef OPENCV_ANDROID_SDK
ifneq ("","$(wildcard $(OPENCV_ANDROID_SDK)/OpenCV.mk)")
include ${OPENCV_ANDROID_SDK}/OpenCV.mk
else
include ${OPENCV_ANDROID_SDK}/sdk/native/jni/OpenCV.mk
endif
else
include C:/opencv-3.2.0-android-sdk/OpenCV-android-sdk/sdk/native/jni/OpenCV.mk
endif
LOCAL_MODULE := Sample
LOCAL_LDLIBS += -llog
LOCAL_CFLAGS := $(OPTIMISATIONS)
LOCAL_SRC_FILES := \
Sample/Sample.cpp
build.gradle
defaultConfig
{
applicationId "com.example.app"
ndk
{
moduleName "Sample"
abiFilters "armeabi-v7a", "x86", "armeabi", "mips"
}
}
externalNativeBuild {
ndkBuild {
path '../../../../../opencv-3.2.0-android-sdk/OpenCV-android-sdk/sdk/native/jni/OpenCV.mk'
path 'src/main/jni/Android.mk'
}
}
The error is occurring at System.loadLibrary("Sample");
I think that LOCAL_SRC_FILE name you given as Sample/Sample.cpp is not correct,check SRC_FILE name with full name like com_example_app_Sample.cpp for your cpp file.
And check ndkBuild path "../../../../../" if this path structure mismatch also the Opencv.mk will not load.
This error means the system cannot find the compile library (Sample.so). You need to make sure the library is at the right place, and that the system knows where to find it - I would suggest making sure jniLibs.srcDirs is set in the build.gradle file. For example, if the library is in 'libs' folder, you put this in the gradle file:
sourceSets {
main {
...
jniLibs.srcDirs 'libs'

Android NDK Exception failed: dlopen failed: cannot locate symbol "_ZN7Tangram11setPositionEdd" referenced by "libtangram.so"

I am trying to run a demo application of a Library in which android ndk is integrated. I have imported this code in Android studio and also downloaded ndk and linked it with project. Code compiles and build succesfully. It gives crash with exception "exception ljava lang unsatisfiedlinkerror thrown while initializing" "failed: dlopen failed: cannot locate symbol "_ZN7Tangram11setPositionEdd" referenced by "libtangram.so"..."
Application.mk:
APP_STL := c++_shared
APP_CPPFLAGS := -frtti -fexceptions
APP_ABI := armeabi armeabi-v7a x86 mips
APP_PLATFORM := android-19
Android.mk:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := tangram
LOCAL_SRC_FILES := jniExports.cpp jniGenerated.cpp platform_android.cpp
LOCAL_LDLIBS := -llog
LOCAL_ALLOW_UNDEFINED_SYMBOLS := true
include $(BUILD_SHARED_LIBRARY)
Module Gradle File:
buildscript {
dependencies {
classpath 'com.android.tools.build:gradle:1.2.3'
classpath 'com.github.dcendents:android-maven-plugin:1.2'
}
}
apply plugin: 'com.android.library'
apply plugin: 'com.github.dcendents.android-maven'
group = GROUP
version = VERSION_NAME
android {
compileSdkVersion 22
buildToolsVersion "21.1.2"
defaultConfig {
minSdkVersion 15
targetSdkVersion 22
}
sourceSets.main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
jni.srcDirs = []
assets.srcDirs = ['core/resources']
}
task ndkBuild(type: Exec, description: 'Compile JNI source via NDK') {
commandLine "C:/Users/Administrator/AppData/Local/Android/android-ndk-r10e/ndk-build.cmd",
'NDK_PROJECT_PATH=build/intermediates/ndk',
'NDK_LIBS_OUT=jniLibs',
'APP_BUILD_SCRIPT=jni/Android.mk',
'NDK_APPLICATION_MK=jni/Application.mk'
}
tasks.withType(JavaCompile) {
compileTask -> compileTask.dependsOn ndkBuild
}
}
// Add gdb server to apk
afterEvaluate {
Sync packageTask = project.getTasks().findByName("packageReleaseJniLibs")
if (packageTask) { packageTask.include(['**/gdbserver', '**/gdb.setup']) }
packageTask = project.getTasks().findByName("packageDebugJniLibs")
if (packageTask) { packageTask.include(['**/gdbserver', '**/gdb.setup']) }
}
dependencies {
compile 'com.squareup.okhttp:okhttp:2.5.0'
compile 'xmlpull:xmlpull:1.1.3.1'
}
apply from: file('gradle-mvn-push.gradle')
In Java class load libraries:
static {
System.loadLibrary("c++_shared");
System.loadLibrary("tangram");
}
I google this issue and in these posts Link1 Link2 Link3 Link4 it is mentioned that it may be the issue of compiling at api 21 or newer and running it on older devices. But i have mentioend APP_PLATFORM := android-19 in my Application.mk and getting this exception.
Any Help?
It looks like you tried to compile the tangram project from your own custom ndk-build as opposed to their recommended build process (https://github.com/tangrams/tangram-es#android). Use their build process with make to create the libtangram.so file, and then copy that .so file into your native libraries directory for your app.
The reason it can't find those symbols is because you aren't including the appropriate source files that define those functions when building with the ndk. By ingnoring undefined symbols, it will compile but won't be able to resolve them at runtime.
The symbol you are missing is created from Tangram::setPosition(double, double) which is defined at https://github.com/tangrams/tangram-es/blob/master/core/src/tangram.cpp#L318 ; however, your Android.mk file doesn't include that source file.

Project porting from Eclipse to Studio

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']
}
}
}

How to use recompiled OpenCV build for Android

So I want to modify one of the files in the OpenCV build for Android and I followed the instructions on their site to download the build and I was able to make it successfully. I modified one of the files (calibinit.cpp) and made it again and copied the files in the
opencv/platforms/build_android_arm/lib/
folder into the jniLibs folder of my AndroidStudio Project.
However, the changes I made are definitely not working. I already had a previous OpenCV library linked to my project, so I'm pretty sure it's still using the old code. How do I use the new build with my AndroidStudio Project?
Can you provide more details about how u use jni in Android Studio Project?
Did u try to clean and rebuild?
Do you have your own custom Android.mk? cause Android Studio will generate its own Android.mk, which can lead to unexpected result as u thought.
delete jniLibs folder and every so file, you don't need them.
define NDK path in local.properties file:
sdk.dir=D:\Android\SDK
ndk.dir=D:\Android\NDK
create a folder(i named it jni) and put Android.mk and Application.mk in it (and an empty dummy.c file for preventing future buggy errors)
as Roy said, Android Studio generates it's own Android.mk, we have our own Android.mk file and Android Studio must use it.jni.srcDirs = []prevents that generation!
Extract openCV sources somewhere, i put them here: D:\Android\Libs\OpenCV
here is my files, modify and use them
build.gradle:
apply plugin: 'com.android.application'
android {
compileSdkVersion 22
buildToolsVersion "22.0.1"
defaultConfig {
applicationId "your.package"
minSdkVersion 11
targetSdkVersion 22
versionCode 1
versionName "1.0"
}
sourceSets {
main {
jni.srcDirs = [] /*disables automatic ndk-build call */
}
}
task ndkBuild(type: Exec) {
commandLine file('D:\\Android\\NDK\\ndk-build.cmd').absoluteFile,
'NDK_PROJECT_PATH='+file('src\\main\\jni').absolutePath,
'APP_BUILD_SCRIPT='+file('src\\main\\jni\\Android.mk').absolutePath,
'NDK_APPLICATION_MK='+file('src\\main\\jni\\Application.mk').absolutePath,
'NDK_LIBS_OUT='+file('src\\main\\jniLibs').absolutePath
}
tasks.withType(JavaCompile) {
compileTask -> compileTask.dependsOn ndkBuild
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.1.1'
compile project(':openCVLibrary300rc1')
}
Application.mk:
APP_STL := gnustl_static
APP_CPPFLAGS := -frtti -fexceptions
APP_ABI := armeabi-v7a x86
APP_PLATFORM := android-8
Android.mk:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
#opencv
OPENCVROOT:= D:/Android/Libs/OpenCV
OPENCV_CAMERA_MODULES:=on
OPENCV_INSTALL_MODULES:=on
OPENCV_LIB_TYPE:=SHARED
include ${OPENCVROOT}/sdk/native/jni/OpenCV.mk
//I have a local source file, you can remove it
LOCAL_SRC_FILES := DetectionBasedTracker_jni.cpp
LOCAL_C_INCLUDES += $(LOCAL_PATH)
LOCAL_LDLIBS += -llog
LOCAL_MODULE := detection_based_tracker
include $(BUILD_SHARED_LIBRARY)

Categories

Resources