0

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 ?!

Jérémy
  • 1,790
  • 1
  • 24
  • 40

1 Answers1

0

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.

Michael
  • 57,169
  • 9
  • 80
  • 125
  • I removed those lines, but the error remain the same. (code in question updated) – Jérémy Feb 02 '19 at 16:20
  • Are you positive that the `.so` file has been bundled with your APK (for some ABI matching the device you're running this on)? – Michael Feb 02 '19 at 17:00
  • The `.so` file is in `arm64-v8a` folder. how to know if my phone match to it ? You have to know that in the same `.so` there is an other `.cpp` with functions that works well. – Jérémy Feb 02 '19 at 18:35
  • Well, I'm unable to reproduce the issue. The function is found just fine when I try it. Though Android Studio didn't like the `#include ` so I changed it to `#include "com_me_Native.h"`. – Michael Feb 02 '19 at 19:53
  • I also tried this, but nothing change: same error. It's still mysterious to me. – Jérémy Feb 03 '19 at 10:34
  • I changed `detection_based_tracker` to `Me_Native` in `CMakeLists.txt` , `Android.mk` and in java `System.loadLibrary("Me_Native");`. Now I have no errors but nothing is printed in the console.. – Jérémy Feb 03 '19 at 14:44