-2

In my C++ side I have :

extern "C" {
      JNIEXPORT void JNICALL test(JNIEnv*, jobject){}
}

My Acivity has a declared method :

public native void test();

However when I call test I get the error native method not found. What's the problem ?

hahaha1256
  • 51
  • 10
  • 3
    Your activity's full class name should be part of that export. A hunt for `"[java] [jni] [android] native method not found"` will proffer a *wealth* of information. Possible duplicate of http://stackoverflow.com/questions/21100542/native-method-not-found. – WhozCraig Jan 30 '15 at 11:32

1 Answers1

3

JNI is not looking for a function test. The name it looks for is of the form Java_packagename_classname_functionname. For example, if your class is

package your.package;

public class Activity {
  static {
    System.loadLibrary("your_library");
  }

  public native void test();
}

Then your library has to contain a function

extern "C" JNIEXPORT void JNICALL Java_your_package_Activity_test(JNIEnv *, jobject) { }

...assuming that test is not overloaded. If test is overloaded, use the javah program to generate a header for you because you don't want to do that manually.

javah your.package.Activity

will generate a file your_package_Activity.h that contains the function signatures you need. Note that this requires the Activity class to be compiled, i.e., there has to be a file your/package/Activity.class in the class path.

Wintermute
  • 42,983
  • 5
  • 77
  • 80