以NDK自带hellojni为例 public class HelloJni extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); /* * Create a TextView and set its content. the text is retrieved by * calling a native function. */ TextView tv = new TextView(this); tv.setText(stringFromJNI("jialiry")); setContentView(tv); } /* * A native method that is implemented by the 'hello-jni' native library, * which is packaged with this application. */ public native String stringFromJNI(String str); /* * This is another native method declaration that is *not* implemented by * 'hello-jni'. This is simply to show that you can declare as many native * methods in your Java code as you want, their implementation is searched * in the currently loaded native libraries only the first time you call * them. * * Trying to call this function will result in a * java.lang.UnsatisfiedLinkError exception ! */ public native String unimplementedStringFromJNI(); /* * 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.loadLibrary("hello-jni"); } }jstring Java_com_example_hellojni_HelloJni_stringFromJNI( JNIEnv* env, jobject thiz, jstring jstr) { char buf[128]; const char* string=(char*)(*env)->GetStringUTFChars(env,jstr,NULL); strcpy(buf, string); strcat(buf, "同志,hello from jni"); (*env)->ReleaseStringUTFChars(env,jstr,(jbyte*)string); return (*env)->NewStringUTF(env, buf); } 结果: jialiry同志,hello from jni
Java_Prompt_getLine(JNIEnv *env, jobject obj, jstring prompt)
{
char buf[128];
const jbyte *str;
str = (*env)->GetStringUTFChars(env, prompt, NULL);
if (str == NULL) {
return NULL; /* OutOfMemoryError already thrown */
}
printf("%s", str);
(*env)->ReleaseStringUTFChars(env, prompt, str);
/* We assume here that the user does not type more than
* 127 characters */
scanf("%127s", buf);
return (*env)->NewStringUTF(env, buf);
}class Prompt {
// native method that prints a prompt and reads a line
private native String getLine(String prompt);
public static void main(String args[]) {
Prompt p = new Prompt();
String input = p.getLine("Type a line: ");
System.out.println("User typed: " + input);
}
static {
System.loadLibrary("Prompt");
}
}
{"JniHello","(Ljava/lang/String)Ljava/lang/String;",(void*)Hello_Native}, 这样改试一试
public class HelloJni extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); /*
* Create a TextView and set its content. the text is retrieved by
* calling a native function.
*/
TextView tv = new TextView(this);
tv.setText(stringFromJNI("jialiry"));
setContentView(tv);
} /*
* A native method that is implemented by the 'hello-jni' native library,
* which is packaged with this application.
*/
public native String stringFromJNI(String str); /*
* This is another native method declaration that is *not* implemented by
* 'hello-jni'. This is simply to show that you can declare as many native
* methods in your Java code as you want, their implementation is searched
* in the currently loaded native libraries only the first time you call
* them.
*
* Trying to call this function will result in a
* java.lang.UnsatisfiedLinkError exception !
*/
public native String unimplementedStringFromJNI(); /*
* 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.loadLibrary("hello-jni");
}
}jstring
Java_com_example_hellojni_HelloJni_stringFromJNI( JNIEnv* env,
jobject thiz, jstring jstr)
{
char buf[128];
const char* string=(char*)(*env)->GetStringUTFChars(env,jstr,NULL);
strcpy(buf, string);
strcat(buf, "同志,hello from jni");
(*env)->ReleaseStringUTFChars(env,jstr,(jbyte*)string);
return (*env)->NewStringUTF(env, buf);
}
结果:
jialiry同志,hello from jni
String path, String[] keys, String[] values)
throws IOException, IllegalArgumentException, SecurityException, IllegalStateException;{
"_setDataSource",
"(Ljava/lang/String;[Ljava/lang/String;[Ljava/lang/String;)V",
(void *)android_media_MediaPlayer_setDataSourceAndHeaders
}