3

I'm trying to make a NDK application, but I get this error:

java.lang.UnsatisfiedLinkError: Native method not found: com.example.hellondk.jni.HelloNDK.hello:()I

I don't understand because the name of the C++ function is the same as the Java packagename and class

HelloNDK.cpp

#include <jni.h>

JNIEXPORT jint JNICALL Java_com_example_hellondk_jni_HelloNDK_hello(JNIEnv* env, jobject o){
    return (jint) 2;
}

HelloNDK.java

package com.example.hellondk.jni;

public class HelloNDK {
    public native int hello();

    static {
        System.loadLibrary("HelloNDK");
    }
}

Android.mk

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE    := HelloNDK
LOCAL_SRC_FILES := HelloNDK.cpp

include $(BUILD_SHARED_LIBRARY)
Mifeet
  • 12,949
  • 5
  • 60
  • 108
alvinmeimoun
  • 1,472
  • 1
  • 19
  • 38

1 Answers1

22

You're exporting it as a C++ function, but the JNI linker doesn't understand C++ name mangling, so it won't be able to find it.

You can use extern "C" to have the function exported without C++ name mangling:

extern "C" JNIEXPORT jint JNICALL Java_com_example_hellondk_jni_HelloNDK_hello(JNIEnv* env, jobject o)
{
    return (jint) 2;
}
James M
  • 18,506
  • 3
  • 48
  • 56
  • You do not realize how helpful this was. Words CANNOT express my gratitude. I have been screwing around with this for the past 2 hours and it was something this stupid. There is a place reserved for you in heaven, good sir. – Anonymous Person Nov 04 '15 at 02:56
  • @AnonymousPerson Glad to be of service. :-) – James M Nov 04 '15 at 16:13