返回信息流c中通过GetStaticMethodID()找java中的static方法,不知道为什么找不到.程序很简单,用ndk中的hello-jni改的.
以下为代码:
hello-jni.c中:
jstring
Java_com_example_hellojni_HelloJni_stringFromJNI( JNIEnv* env,
jobject thiz )
{
#if defined(__arm__)
#if defined(__ARM_ARCH_7A__)
#if defined(__ARM_NEON__)
#if defined(__ARM_PCS_VFP)
#define ABI "armeabi-v7a/NEON (hard-float)"
#else
#define ABI "armeabi-v7a/NEON"
#endif
#else
#if defined(__ARM_PCS_VFP)
#define ABI "armeabi-v7a (hard-float)"
#else
#define ABI "armeabi-v7a"
#endif
#endif
#else
#define ABI "armeabi"
#endif
#elif defined(__i386__)
#define ABI "x86"
#elif defined(__x86_64__)
#define ABI "x86_64"
#elif defined(__mips64) /* mips64el-* toolchain defines __mips__ too */
#define ABI "mips64"
#elif defined(__mips__)
#define ABI "mips"
#elif defined(__aarch64__)
#define ABI "arm64-v8a"
#else
#define ABI "unknown"
#endif
jclass clazz = (*env)->FindClass(env, "com/example/hellojni/HelloJni");
if(clazz == NULL)
{
LOGD("cannot find class HelloJni");
}
jmethodID java_method = (*env)->GetStaticMethodID(env, clazz, "test", "(V)V");
if(java_method == NULL)
{
LOGD("cannot find java_method");
}else
{
(*env)->CallStaticVoidMethod(env, clazz, java_method);
}
return (*env)->NewStringUTF(env, "Hello from JNI ! Compiled with ABI " ABI ".");
}
HelloJni.java中:
public class HelloJni extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
/* Create a TextView and set its content.
* the text is retrieved by calling a native
* function.
*/
TextView tv = new TextView(this);
tv.setText( stringFromJNI() );
setContentView(tv);
}
/* A native method that is implemented by the
* 'hello-jni' native library, which is packaged
* with this application.
*/
public native String stringFromJNI();
/* This is another native method declaration that is *not*
* implemented by 'hello-jni'. This is simply to show that
* you can declare as many native methods in your Java code
* as you want, their implementation is searched in the
* currently loaded native libraries only the first time
* you call them.
*
* Trying to call this function will result in a
* java.lang.UnsatisfiedLinkError exception !
*/
public native String unimplementedStringFromJNI();
/* this is used to load the 'hello-jni' library on application
* startup. The library has already been unpacked into
* /data/data/com.example.hellojni/lib/libhello-jni.so at
* installation time by the package manager.
*/
public static void test(){
Log.i("testTag", "wa ha ha");
}
static {
System.loadLibrary("hello-jni");
}
}
在调用GetStaticMethodID()的时候程序还奔溃了.求问怎么改才对?
这是一条镜像帖。来源:北邮人论坛 / mobile-terminal-at / #21243同步于 2015/4/10
该镜像源已超过 30 天没有更新,可能在源站已被删除。
MobileTerminalAT机器人发帖
C调用java JNI
melodyrain
2015/4/10镜像同步7 回复
订阅后,新回复会通过你的通知中心匿名送达。
7 条回复
确实是对的~话说你遇到过java文件中的方法在生成的class.dex中没有的现象吗?我遇到的情况是必须在类中调用此方法,不然class.dex中反编译后发现此方法不存在。
【 在 handspeaker 的大作中提到: 】
: 把(V)V改成()V试试?
顺便求助一下,我写了很长时间,突然cygwin出问题了,一编译就报.rodata的错误,有大神知道怎么解决吗?重装n遍cygwin了,返回到之前正确编译通过的代码也无法编译了,已经快崩溃了。
这个没搞过,我猜是为了减小编译后的文件,不需要的方法就没有编译进去?C++中静态链接好像就是这么搞的,只把使用到的函数链接进去
【 在 melodyrain 的大作中提到: 】
: 确实是对的~话说你遇到过java文件中的方法在生成的class.dex中没有的现象吗?我遇到的情况是必须在类中调用此方法,不然class.dex中反编译后发现此方法不存在。
ndk吗?貌似现在新版的ndk都不需要cygwin了,所以没遇到过你说的这种情况,stackoverflow上搜一下?
【 在 rumboxy 的大作中提到: 】
: 顺便求助一下,我写了很长时间,突然cygwin出问题了,一编译就报.rodata的错误,有大神知道怎么解决吗?重装n遍cygwin了,返回到之前正确编译通过的代码也无法编译了,已经快崩溃了。
的确是不需要cygwin了 直接把编译命令变成ndk-build
【 在 handspeaker 的大作中提到: 】
: ndk吗?貌似现在新版的ndk都不需要cygwin了,所以没遇到过你说的这种情况,stackoverflow上搜一下?
: 【 在 rumboxy 的大作中提到: 】
: : 顺便求助一下,我写了很长
: .........
发自「贵邮」