Android NDK Build failing for simple native function - android

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"

Related

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.

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

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

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

embed python in android NDK

I have a android program that has main logic written in NDK. But now I want to evaluate some python codes as part of main logic. So I tried to use this embed python in c http://docs.python.org/2/extending/embedding.html
I created a simple android project, with native.c like this (all I did was just adding python header)
#include <Python.h>
#include <jni.h>
#include <string.h>
#include <android/log.h>
#include <pthread.h>
#define DEBUG_TAG "NDK_AndroidNDK1SampleActivity"
void Java_com_example_com_test_mytest_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);
}
Here is my Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_LDLIBS := -llog `python2.7-config --ldflags`
LOCAL_MODULE := ndk1
LOCAL_SRC_FILES := native.c
LOCAL_CFLAGS := `python2.7-config --cflags`
#LOCAL_SHARED_LIBRARIES := -lpthread
include $(BUILD_SHARED_LIBRARY)
I got following error when compiling (using Mac)
Compile thumb : ndk1 <= native.c
arm-linux-androideabi-gcc: error: i386: No such file or directory
arm-linux-androideabi-gcc: error: x86_64: No such file or directory
arm-linux-androideabi-gcc: error: unrecognized option '-arch'
arm-linux-androideabi-gcc: error: unrecognized option '-arch'
make: *** [obj/local/armeabi/objs/ndk1/native.o] Error 1
Anyone could help please? :D
Python2.7-config was built for a 64 bit i386 system not for Android. It's passing bad arguments to the compiler in the LOCAL_CFLAGS field. Either run one that you built to cross compile on Android or specify the values yourself. Check google for 'Python-For-Android' (Py4A) on how to embed python in an NDK app.

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