在 Android 中从 C++ 调用 java 方法 [英] Calling a java method from c++ in Android

查看:37
本文介绍了在 Android 中从 C++ 调用 java 方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从 C++ 获取一个简单的 Java 方法调用,而 Java 调用本机方法.这是 Java 代码:

I'm trying to get a simple Java method call from C++ while Java calls native method. Here's the Java code:

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方法.

I'm trying to call messageMe method from native code in the process of getJniString* method call from Java to native.

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
", 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.

推荐答案

如果是对象方法,需要将对象传递给CallObjectMethod:

If it's an object method, you need to pass the object to CallObjectMethod:

jobject result = env->CallObjectMethod(obj, messageMe, jstr);

你所做的相当于jstr.messageMe().

因为你是一个void方法,你应该调用:

Since your is a void method, you should call:

env->CallVoidMethod(obj, messageMe, jstr);

如果你想返回一个结果,你需要改变你的JNI签名(()V表示一个void返回类型的方法)以及返回类型在您的 Java 代码中.

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.

这篇关于在 Android 中从 C++ 调用 java 方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆