在Android中从c ++调用java方法
我试图从C ++中获得一个简单的Java方法调用,而Java调用本地方法。 这是Java代码:
public class MainActivity extends Activity { private static String LIB_NAME = "name"; static { System.loadLibrary(LIB_NAME); } /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); TextView tv = (TextView) findViewById(R.id.textview); tv.setText(this.getJniString()); } public void messageMe(String text) { System.out.println(text); } public native String getJniString(); }
我试图从getJniString*
方法从Java调用到本机的过程中从本机代码调用messageMe
方法。
native.cpp:
#include <string.h> #include <stdio.h> #include <jni.h> jstring Java_the_package_MainActivity_getJniString( JNIEnv* env, jobject obj, jint depth ){ // JavaVM *vm; // JNIEnv *env; // JavaVMInitArgs vm_args; // vm_args.version = JNI_VERSION_1_2; // vm_args.nOptions = 0; // vm_args.ignoreUnrecognized = 1; // // // Construct a VM // jint res = JNI_CreateJavaVM(&vm, (void **)&env, &vm_args); // Construct a String jstring jstr = env->NewStringUTF("This string comes from JNI"); // First get the class that contains the method you need to call jclass clazz = env->FindClass("the/package/MainActivity"); // Get the method that you want to call jmethodID messageMe = env->GetMethodID(clazz, "messageMe", "(Ljava/lang/String;)V"); // Call the method on the object jobject result = env->CallObjectMethod(jstr, messageMe); // Get a C-style string const char* str = env->GetStringUTFChars((jstring) result, NULL); printf("%s\n", str); // Clean up env->ReleaseStringUTFChars(jstr, str); // // Shutdown the VM. // vm->DestroyJavaVM(); return env->NewStringUTF("Hello from JNI!"); }
干净的编译应用程序停止与下一条消息后:
ERROR/AndroidRuntime(742): FATAL EXCEPTION: main java.lang.NoSuchMethodError: messageMe at *.android.t3d.MainActivity.getJniString(Native Method) at *.android.t3d.MainActivity.onCreate(MainActivity.java:22)
显然这意味着方法名称是错误的,但对我来说看起来还不错。
如果是对象方法,则需要将该对象传递给CallObjectMethod
:
jobject result = env->CallObjectMethod(obj, messageMe, jstr);
你在做什么是相当于jstr.messageMe()
。
既然你是一个无效的方法,你应该打电话给:
env->CallVoidMethod(obj, messageMe, jstr);
如果你想返回一个结果,你需要改变你的JNI签名( ()V
一个void
返回types的方法)以及Java代码中的返回types。
你可以学习如何从本地代码正确调用java方法: http : //journals.ecs.soton.ac.uk/java/tutorial/native1.1/implementing/method.html
你可以用这个很好的教程学习更多关于整体JNI编程的知识: http : //journals.ecs.soton.ac.uk/java/tutorial/native1.1/implementing/index.html
Denys S.在问题文章中发表的解决scheme:
我把它搞砸了c到c + +转换(基本上env
可变的东西),但我得到它与C + +的下面的代码工作:
#include <string.h> #include <stdio.h> #include <jni.h> jstring Java_the_package_MainActivity_getJniString( JNIEnv* env, jobject obj){ jstring jstr = (*env)->NewStringUTF(env, "This comes from jni."); jclass clazz = (*env)->FindClass(env, "com/inceptix/android/t3d/MainActivity"); jmethodID messageMe = (*env)->GetMethodID(env, clazz, "messageMe", "(Ljava/lang/String;)Ljava/lang/String;"); jobject result = (*env)->CallObjectMethod(env, obj, messageMe, jstr); const char* str = (*env)->GetStringUTFChars(env,(jstring) result, NULL); // should be released but what a heck, it's a tutorial :) printf("%s\n", str); return (*env)->NewStringUTF(env, str); }
接下来是java方法的代码:
public class MainActivity extends Activity { private static String LIB_NAME = "thelib"; static { System.loadLibrary(LIB_NAME); } /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); TextView tv = (TextView) findViewById(R.id.textview); tv.setText(this.getJniString()); } // please, let me live even though I used this dark programming technique public String messageMe(String text) { System.out.println(text); return text; } public native String getJniString(); }