[英]Calling a java method from c++ in Android
I'm trying to get a simple Java method call from C++ while Java calls native method. 我正在尝试从C ++获取一个简单的Java方法调用,而Java调用本机方法。 Here's the Java code: 这是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();
}
I'm trying to call messageMe
method from native code in the process of getJniString*
method call from Java to native. 我试图在从Java到本机的getJniString*
方法调用过程中从本机代码调用messageMe
方法。
native.cpp: 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!");
}
After clean compilation app stops with next message: 干净的编译应用程序停止与下一条消息:
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)
Apparently it means that method name is wrong, but it looks OK to me. 显然它意味着方法名称错误,但它看起来不错。
If it's an object method, you need to pass the object to CallObjectMethod
: 如果它是一个对象方法,则需要将该对象传递给CallObjectMethod
:
jobject result = env->CallObjectMethod(obj, messageMe, jstr);
What you were doing was the equivalent of jstr.messageMe()
. 你在做什么相当于jstr.messageMe()
。
Since your is a void method, you should call: 由于您是一个无效方法,您应该致电:
env->CallVoidMethod(obj, messageMe, jstr);
If you want to return a result, you need to change your JNI signature (the ()V
means a method of void
return type) and also the return type in your Java code. 如果要返回结果,则需要更改JNI签名( ()V
表示void
返回类型的方法)以及Java代码中的返回类型。
Solution posted by Denys S. in the question post: 解决方案由Denys S.在问题帖子中发布:
I quite messed it up with c to c++ conversion (basically env
variable stuff), but I got it working with the following code for C++: 我把它搞砸了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);
}
And next code for java methods: 以及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();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.