Yes, you can use the same JNI signature in many applications. Your class may not belong to the default package of the application, as defined in AndroidManifest.xml. So what?
Example:
Start with HelloJni sample from NDK (in Eclipse, use Import -> Android -> existing Android Code, and point to the ${android-ndk-root}/samples/hello-jni).
Build it and run on device or emulator.
Open a new Android Application project, call it TestCrossJni.
The package name for our app will be: test.cross.jni - no relation to com.example.hellojni
!
Choose "Create Activity" -> create Blank Activity.
Add new Java class to this project (src/com/example/hellojni/HelloJni.java
):
package com.example.hellojni;
public class HelloJni
{
public static String gets() {
return stringFromJNI();
}
/* A native method that is implemented by the
* 'hello-jni' native library, which is packaged
* with this application.
*/
private native String stringFromJNI();
/* this is used to load the 'hello-jni' library on application
* startup. The library has already been unpacked into
* /data/data/com.example.hellojni/lib/libhello-jni.so at
* installation time by the package manager.
*/
static {
System.load("/data/data/com.example.hellojni/lib/libhello-jni.so");
}
}
Edit res/layout/activity_main.xml
: replace
line 12 android:text="@string/hello_world" />
with android:id="@+id/hello_world" />
In src/test/cross/jni/MainActivity.java
, add the following after
line 12 setContentView(R.layout.activity_main);
((android.widget.TextView)findViewById(R.id.hello_world)).setText(com.example.hellojni.HelloJni.gets());
Profit!