简体   繁体   English

Android NDK:从C ++调用Java函数

[英]Android NDK: Calling Java functions from C++

I am very new to JNI and I am trying to figure out how certain things work before I port my C++ iOS code to it. 我是JNI的新手,我想在将C ++ iOS代码移植到它之前弄清楚某些事情是如何工作的。 I was successful in getting one of the NDK samples working in Android studio and I can see how Java is able to call C++ functions. 我成功地在Android工作室中获得了一个NDK示例,我可以看到Java如何调用C ++函数。

I've been searching around and taking chunks of code, but I haven't been able to get it to work in my specific implementation. 我一直在搜索并获取大量代码,但我无法让它在我的具体实现中工作。

Just to test how things worked I set up a simple text log function in java, and I am trying to call it from my native code but I've run into issues. 只是为了测试一切是如何工作的,我在java中设置了一个简单的文本日志功能,我试图从我的本机代码中调用它,但我遇到了问题。

Here is my Java function: 这是我的Java函数:

public static void log(String s){
        Log.d("Native", s);
}

And C++: 和C ++:

void Log(std::string s){

    JNIEnv *env;
    g_JavaVM->GetEnv((void**)&env, JNI_VERSION_1_6);

    jstring jstr1 = env->NewStringUTF(s.c_str());

    jclass clazz = env->FindClass("com/android/gl2jni/GL2JNILib");
    jmethodID mid = env->GetStaticMethodID(clazz, "log", "(Ljava/lang/String;)V");

    jobject obj = env->CallStaticObjectMethod(clazz, mid, jstr1);
}

From what I've seen with different examples this should work, but it throws an error: 从我看到的不同的例子,这应该工作,但它会抛出一个错误:

29835-29849/com.android.gl2jni A/libc﹕ Fatal signal 11 (SIGSEGV) at 0x00000000 (code=1), thread 29849 (Thread-17371)

Am I missing something? 我错过了什么吗?

EDIT: 编辑:

I've changed it to GetStaticMethodID. 我已将其更改为GetStaticMethodID。 But after logging the progress of the function I've found out that the line that fails is: 但在记录函数的进度后,我发现失败的行是:

g_JavaVM->GetEnv((void**)&env, JNI_VERSION_1_6);

Which I figure is because g_JavaVM is set as static JavaVM* g_JavaVM = NULL; 我认为这是因为g_JavaVM被设置为static JavaVM* g_JavaVM = NULL; and then never touched again. 然后再也没有碰过。 I'm guessing I need to set this variable, but how? 我猜我需要设置这个变量,但是如何?

Part of my problem was that I didn't initialize The JavaVM. 我的部分问题是我没有初始化JavaVM。 The other part was that I was using C++, but I was trying to use the C functions. 另一部分是我使用C ++,但我试图使用C函数。

The working code is: 工作代码是:

Java: Java的:

public static void log(String s){
        Log.d("Native", s);
}

C++: C ++:

void Log(std::string s){

    JNIEnv *env;
    g_JavaVM->GetEnv((void**)&env, JNI_VERSION_1_6);

    jstring jstr1 = env->NewStringUTF(s.c_str());

    jclass clazz = env->FindClass("com/android/gl2jni/GL2JNILib");
    jmethodID mid = env->GetStaticMethodID(clazz, "log", "(Ljava/lang/String;)V");

    jobject obj = env->CallStaticObjectMethod(clazz, mid, jstr1);
}

//In some initialization function with Environment variable

env->GetJavaVM(&g_JavaVM);

Hopefully this can help other people with the same problem. 希望这可以帮助其他人解决同样的问题。

http://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/functions.html http://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/functions.html

GetStaticMethodID GetStaticMethodID

jmethodID GetStaticMethodID(JNIEnv *env, jclass clazz, const char *name, const char *sig); jmethodID GetStaticMethodID(JNIEnv * env,jclass clazz,const char * name,const char * sig);

Returns the method ID for a static method of a class. 返回类的静态方法的方法ID。 The method is specified by its name and signature. 该方法由其名称和签名指定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM