Android NDK调用具有不同名称空间的本机方法 [英] Android NDK calling native method with different namespace
问题描述
我是JNI编程的新手 我可以像调用其他JNI名称空间定义一样调用本机方法吗?
i'm new from JNI programming Can i call a native method like from a different JNI namespace definition?
例如:
package com.example.hellojni;
public class HelloJni extends Activity
{
public native int MyMethod();
public native int OtherMethod(); // this works have the namespace package=jniexport interface
static {System.loadLibrary("hello-jni");}
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
int b = OtherMethod(); //works
int a = MyMethod(); //ERROR java.lang.reflect.InvocationTargetException
}
}
================File.c================
JNIEXPORT int JNICALL Java_Cardioline_HelloJni_MyMethod(JNIEnv *env, jobject thisObj)
{
return 1;
}
JNIEXPORT int JNICALL Java_com_example_hellojni_HelloJni_OtherMethod(JNIEnv *env, jobject thisObj)
{
return 2;
}
推荐答案
否,JNI方法名称由javah预先确定.尝试从这里开始:
No, the JNI method name is predetermined by javah. Try starting here:
http://blog.edwards-research.com/2012/04/tutorial-android-jni/
javah将为您提供标头,并且您必须将方法声明复制到.c文件中.
javah will provide you with the header and you must copy the method declarations into your .c file.
如果要调用的另一个类中有JNI方法,请在Java中调用另一个类.基本上改变这个:
If you have a JNI method in another class that you want to call, call the other class in java. Basically change this:
int a = MyMethod();
对此:
int a = new CardioLine.HelloJni().MyMethod();
通常,我具有Java文件到.c/.cpp文件的一对一映射.这可能也是您想要的(对于具有本地方法的Java类).
Typically, I have a one-to-one mapping of Java files to .c/.cpp files. That's probably what you want too (for Java classes with native methods).
这篇关于Android NDK调用具有不同名称空间的本机方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!