Reputation: 1890
I want to use C++ method inside my Java code. So I decided to use JNI. But the link seams to not work properly, du to my error at the execution No implementation found for void com.me.Native.helloWorld() (tried Java_com_me_Native_helloWorld and Java_com_me_Native_helloWorld__)
Native.java (called elsewhere as Native.helloWorld()
):
package com.me;
public class Native{
static {
System.loadLibrary("detection_based_tracker");
}
public static native void helloWorld();
}
Android.mk :
...
LOCAL_SRC_FILES += com_me_Native.cpp
LOCAL_C_INCLUDES += $(LOCAL_PATH)
LOCAL_MODULE := detection_based_tracker
include $(BUILD_SHARED_LIBRARY)
com_me_Native.h (generated with javah command):
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_me_Native */
#ifndef _Included_com_me_Native
#define _Included_com_me_Native
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: com_me_Native
* Method: helloWorld
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_com_me_Native_helloWorld
(JNIEnv *, jclass);
#ifdef __cplusplus
}
#endif
#endif
com_me_Native.cpp :
#include <com_me_Native.h>
#include <iostream>
#include <android/log.h>
#define LOG_TAG "HelloWorld"
#define LOGD(...) ((void)__android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__))
#ifdef __cplusplus
extern "C" {
#endif
using namespace std;
/*
* Class: com_me_Native
* Method: helloWorld
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_com_me_Native_helloWorld
(JNIEnv *, jclass)
{
LOGD("Hello from c++");
}
#ifdef __cplusplus
}
#endif
As you see a use JNIEXPORT
and JNICALL
on my method. I also use extern "C"
for C++ use. My .h
was generated by javah
. I checked the Android.mk
and I didn't forgot to add my .cpp
file to LOCAL_SRC_FILES
. I statically loaded my library in the Native.java
to use my static function.
Now I don't know where the error may come from... Any idea ?!
Upvotes: 0
Views: 485
Reputation: 58467
The include guard should only be in the .h
file, not in the .cpp
file.
So in your .cpp
file, remove these lines:
#ifndef _Included_com_me_Native
#define _Included_com_me_Native
As well as the final #endif
.
What happens with your curent code is that _Included_com_me_Native
gets defined when you include your header file, so then the #ifndef _Included_com_me_Native
in the .cpp
file will be false, and none of that code gets compiled.
Upvotes: 0