Can't compile android project wiht JNI code (algorithm not found) - android

I'm trying to build simple android app with some JNI code.
I already try this suggestion, but isn't help
When I press build project in eclipse I get this error:
Description Resource Path Location Type
fatal error: algorithm: No such file or directory Tracker line 56, external location: /home/slani/code/OpenCV-2.4.6-android-sdk/sdk/native/jni/include/opencv2/core/core.hpp C/C++ Problem
make: *** [obj/local/armeabi/objs/detect_jni/detect_jni.o] Error 1 Tracker C/C++ Problem
Line 56 in core.hpp contains the relevant include.
This is my Android.mk file jni folder:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
include /home/slani/code/OpenCV-2.4.6-android-sdk/sdk/native/jni/OpenCV.mk
LOCAL_MODULE := detect_jni
LOCAL_SRC_FILES := detect_jni.cpp
include $(BUILD_SHARED_LIBRARY)
This is my Aplication.mk file in jni folder:
APP_STL := gnustl_static
APP_CPPFLAGS := -frtti -fexceptions
APP_ABI := all
APP_PLATFORM := android-8
This is my .cpp file:
#include <jni.h>
#include <opencv/cv.h>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/features2d/features2d.hpp>
using namespace cv;
extern "C"{
JNIEXPORT void JNICALL Java_com_slani_tracker_OpenCamera_findObject((JNIEnv *env, jlong addRgba, jlong addHsv);
JNIEXPORT void JNICALL Java_com_slani_tracker_OpenCamera_findObject((JNIEnv *env, jlong addRgba, jlong addHsv)
{
Mat& rgba = *(Mat*)addRgba;
Mat& hsv = *(Mat*)addHsv;
cvtColor(rgba, hsv,CV_RGBA2HSV);
}
}
Can someone please help me? What could be causing this problem?
Thanks

See my answer at your thread in answers.opencv.org
Should help

Related

NDK - include error

I need your help because it drives me crazy. What cause my error?
The error is
"jni/algorithm.cpp:4:33: fatal error: opencv2/core/core.hpp: No such file or directory #include <opencv2/core/core.hpp>
^ compilation terminated. make: *** [obj/local/arm64-v8a/objs/algorithm/algorithm.o] Error 1"
My algorithm.cpp is:
#include <jni.h>
#include <string.h>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/features2d/features2d.hpp>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc_c.h"
using namespace std;
using namespace cv;
extern "C"
{
JNIEXPORT jlong JNICALL Java_com_example_hematoma_MainActivity_fce(JNIEnv *env, jobject obj, jlong matimage)
{
Mat *jni_image = (Mat*) matimage;
return (jlong)jni_image;
}
}
My Android.mk is:
LOCAL_PATH := $(call my-dir)
include /home/nemesis/adt-bundle-linux-x86_64-20140702/OpenCV-2.4.10-android-sdk/sdk/native/jni/OpenCV.mk
include $(CLEAR_VARS)
LOCAL_MODULE := algorithm
LOCAL_SRC_FILES := algorithm.cpp
LOCAL_C_INCLUDE := /home/nemesis/adt-bundle-linux-x86_64-20140702/OpenCV-2.4.10-android-sdk/sdk/native/jni/include/opencv2/core/core.hpp
LOCAL_C_INCLUDE += /home/nemesis/adt-bundle-linux-x86_64-20140702/OpenCV-2.4.10-android-sdk/sdk/native/jni/include/
include $(BUILD_SHARED_LIBRARY)
The error occurs when ndk try to build .so
Thanks in advance.
LOCAL_C_INCLUDE := /home/nemesis/adt-bundle-linux-x86_64-20140702/OpenCV-2.4.10-android-sdk/sdk/native/jni/include/opencv2
should be
LOCAL_C_INCLUDES := /home/nemesis/adt-bundle-linux-x86_64-20140702/OpenCV-2.4.10-android-sdk/sdk/native/jni/include
ie, it is plural and should point to the location from which the following is a relative path:
#include <opencv2/core/core.hpp>
I solved my problem by add LOCAL_LDLIBS += -llog -ldl to "Android.mk"
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
include C:/.../OpenCV-3.1.0-android-sdk/OpenCV-android-sdk/sdk/native/jni/OpenCV.mk
LOCAL_MODULE := Testappcv
LOCAL_SRC_FILES := Testappcv.cpp
LOCAL_LDLIBS += -llog -ldl
include $(BUILD_SHARED_LIBRARY)
And added "Application.mk" file to jni folder
APP_STL := gnustl_static
APP_CPPFLAGS := -frtti -fexceptions
APP_ABI := armeabi-v7a
APP_PLATFORM := android-8
So, I tried to change in Project properties C/C++ Build -> Build command from ${NDKROOT}/ndk-build to ${NDKROOT}/ndk-build.cmd and it solved all my include libs errors. weird

Android NDK includes

Suppose my Android project uses jni with next structure:
jni__
|__src__
|__first.c
|__second.c
|
|__include__
|__first.h
There are two source files in src directory and one header file in src/include director.
first.h:
#include <string.h>
#ifndef FIRST_H
#define FIRST_H
extern char * text();
#endif
first.c:
#include "first.h"
char * text() {
...
return ...;
}
second.c:
#include "first.h"
#include <jni.h>
jbyteArray Java_com_example_android_MainActivity_getText( JNIEnv* env, jobject this) {
char * str = text();
...
return str;
}
How to compile this source and correctly specify the directory with headers in Android.mk file?
Now my Android.mk file is as follows:
LOCAL_PATH:= $(call my-dir)
LOCAL_SRC_PATH := $(LOCAL_PATH)/../src
LOCAL_C_INCLUDES := $(LOCAL_PATH)/../src/include
include $(CLEAR_VARS)
LOCAL_MODULE := mylib
LOCAL_SRC_FILES := $(LOCAL_SRC_PATH)/first.c \
$(LOCAL_SRC_PATH)/second.c
include $(BUILD_SHARED_LIBRARY)
But when I try to build this sources compilation terminated with error: "first.h: No such file or directory".
LOCAL_C_INCLUDES := $(LOCAL_PATH)/src/include should do it. As stated in ANDROID-MK file in NDK docs that you can find in your NDK directory, $(LOCAL_PATH) refers to the directory that your Android.mk file is located. Assuming that it is located in jni directory, $(LOCAL_PATH)/src/include points to your header directory where the missing file is located.
And I would use #include <first.h> rather than using #include "first.h" according to the general convention of using headers.

Type 'std::string' could not be resolved.

I'd started porting some Java code in native c++ in Android. I have an issue with using strings in c++:
Type 'std::string' could not be resolved
There is my sample code
#include <jni.h>
#include <lexu_me_test_native.h>
#include <string.h>
using namespace std;
JNIEXPORT jstring JNICALL Java_lexu_me_test_native_prepairToShowNative
(JNIEnv * env, jclass javaThis, jstring str)
{
jboolean blnIsCopy;
jstring jstrOutput;
char* strCOut;
std::string ss;
const char* strCIn = (env)->GetStringUTFChars(str , &blnIsCopy);
// convert jstring to a char array
// Do stuff with the char array and and store the result
// in another char array strCOut
(env)->ReleaseStringUTFChars(str , strCIn); // release jstring
jstrOutput = (env)->NewStringUTF(strCOut); // convert char array to jstring
return jstrOutput;
}
Android.mk file:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := native
LOCAL_SRC_FILES := native.cpp
include $(BUILD_SHARED_LIBRARY)
Application.mk file:
APP_STL := stlport_static
MinGW installed and added to path. I tried using android-ndk-r8e and android-ndk-r8-crystax-1 nothing helped. In Cygwin Terminal errors:
Compile++ thumb : native <= native.cpp
jni/native.cpp: In function '_jstring* Java_lexu_me_test_native_prepairToShowNative(JNIEnv*, jclass, jstring)':
jni/native.cpp:11:2: error: 'string' was not declared in this scope
jni/native.cpp:11:9: error: expected ';' before 'ss'
I'm using Win 7 64bit. Can anyone say how it could be solved?
Thanks.
EDIT.
In C/C++ General - Path and Symbols already set: C:\Android\android-ndk-r8e\platforms\android-14\arch-arm\usr\include
If the other answers didn't work, then try these steps:
If you have using namespace std;, use string instead of std::string.
If #include <string> doesn't work and you're using Linux, try #include <unistd.h>. If you're using another OS, use #include <cstdlib>.
Check your Android.mk and Application.mk files. Ensure you select an STL library and include int the Application.mk file:
Application.mk: APP_STL := gnustl_static
(gnustl_static) can be replaced with the STL version you want, and STLPort is still an option
See http://www.kandroid.org/ndk/docs/CPLUSPLUS-SUPPORT.html

Android NDK Build failing for simple native function

I am trying to build a very simple native function using ndk-build and getting the following error.
DriverAdapter.cpp:6:69: error: expected ',' or '...' before 'this'
The following are my .mk and .cpp files
DriverAdapter.cpp
#include <jni.h>
#include <string.h>
#include <android/log.h>
#define DEBUG_TAG "NDK_AndroidNDK1SampleActivity"
void Java_com_ihearhtpi_MainActivity_helloLog(JNIEnv * env, jobject this, jstring logThis)
{
jboolean isCopy;
const char * szLogThis = (*env)->GetStringUTFChars(env, logThis, &isCopy);
__android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "NDK:LC: [%s]", szLogThis);
(*env)->ReleaseStringUTFChars(env, logThis, szLogThis);
}
Android.mk
LOCAL_PATH := $(call my-dir)
include $(call all-subdir-makefiles)
include $(CLEAR_VARS)
LOCAL_LDLIBS := -llog
LOCAL_MODULE :=driver
LOCAL_SRC_FILES :=DriverAdapter.cpp
include $(BUILD_SHARED_LIBRARY)
Not sure why this is happening.
The problem is that this is c++ keyword. And you can't use a keyword as variable name.
You can't name your argument "this"

android libpcap -> no such file or directory

I'm trying to use libpcap with android to test it I wrote a simple c program that looks like this
#include <string.h>
#include <pcap.h>
#include <jni.h>
jstring Java_com_example_penguin_AvailableHosts_stringFromJNI(JNIEnv* jobject thiz) {
char *dev, errbuf[PCAP_ERRBUF_SIZE];
dev = pcap_lookupdev(errbuf);
if (dev != NULL)
return (*env)->NewStringUTF(env, dev);
else
return (*env)->NewStringUTF(env, "ERROR");
}
The Android.mk file looks like
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := test
LOCAL_SRC_FILES := test.c
include $(BUILD_SHARED_LIBRARY)
In the root of my project I call
ndk-build
which gives me the following error
jni/test.c:2:18: fatal error: pcap.h: No such file or directory
compilation terminated.
Is there anything missing in the Android.mk file?

Categories

Resources