JNI - How to create a library for different classes


I'm new to JNI, i'm developing a native library for an Android project. I read some papers about JNI programming but i didn't understand if it is possible to create a library that can be loaded in different project classes with different packages. I read that to declare a new JNI method the syntax is:

  • the prefix "Java_"
  • an encoded fully qualified class name
  • an underscore ("_") separator
  • an encoded method name

Based on this definition it shouldn't be possible...

Suppose i had defined a class A in the package pkg1 with the native method foo contained in libfoo, and then i defined a class B in the package pkg2. Can i use libfoo and the foo method in B? How should i define the native method to achieve the result?


On Android, the recommended way to register natives is to use JNI's RegisterNatives(), because it's faster than the Java_* lookup mechanism.

And doing so, you should be able to register the same native method into several classes/packages if you like.