zoukankan      html  css  js  c++  java
  • Problems of Android NDK

    For the detail of JNI functions:

      http://www.public.iastate.edu/~java/docs/guide/nativemethod/functions.doc.html#17314


    Note:

     1.The Definition about JNIEnv is different in C and C++.

     #if defined(__cplusplus)

    typedef _JNIEnv JNIEnv;
    typedef _JavaVM JavaVM;
    #else
    typedef const struct JNINativeInterface* JNIEnv;
    typedef const struct JNIInvokeInterface* JavaVM;
    #endif

    In C,JNI use(*env)-> to get the value of the method pointer.

       jsize len = (*env)->GetArrayLength(env,array);

     In C++,JNIEvn has the inner member function which can deal with the pointer.

       jsize len =env->GetArrayLength(array);

     So you may get the following error if you apply the C's usage to C++: 

    $ make APP=hello-jni-C++
    Android NDK: Building for application 'hello-jni-C++'
    Compile++ thumb: hello-jni-C++ <= sources/samples/hello-jni-C++/hello-jni.cpp
    sources/samples/hello-jni-C++/hello-jni.cpp: In function '_jstring* Java_com_example_hellojni_HelloJni_stringFromJNI(JNIEnv*, _jobject*)':
    sources/samples/hello-jni-C++/hello-jni.cpp:29: error: base operand of '->' has non-pointer type '_JNIEnv'
    make: *** [out/apps/hello-jni-C++/android-1.5-arm/objs/hello-jni-C++/hello-jni.o] Error 1

     JNI can only use C complier,so you have to add extern "C" when you use C++. or you'll get  java.lang.UnsatisfiedLinkError

       JNIEnv * env--> JNI interface pointer

       JObject thiz-->this pointer

     


    Problems:

    1.Fatal signal 7 (SIGBUS) at 0x00000000 (code=128)

      origin:

    *(pRect+i) = *((RECT*)(inbuf+n));

      solution:

    //RECT temp = *((RECT*)(inbuf+n));
    //*(pRect+i) = temp;

      memcpy((pRect+i), (RECT*)(inbuf+n), sizeof(RECT));

      analyse:NDK's compiler may be different from linux or the cygwin is not perfect.

      ARM cpu访问地址必须4字节对齐。指针从一个大的内存块中截取时必须考虑4字节对齐。

    2.Fatal signal 11 (SIGBUS)

    Someone says "This is segmentation fault, means that the program accessed a memory location that was not assigned.  ". Maybe.

    3.java.lang.UnsatisfiedLinkError

    Make sure that:

      a. you have added extern "C" to the C++ part

      b. you jni method name is right(package name_class name_ method name)

  • 相关阅读:
    利用Clojure统计代码文件数量和代码行数
    Workflow:添加工作流存储功能
    MongoDB:最简单的增删改查(Oops,可能太简单了)
    《WF in 24 Hours》读书笔记
    推荐一个学习python的网站
    Inter系列处理器名称浅析
    [Android1.5]TextView跑马灯效果
    Code::Blocks 的配色方案
    PuTTY + Xming 远程使用 Linux GUI
    Linux下查看文件和文件夹大小
  • 原文地址:https://www.cnblogs.com/qiengo/p/2573577.html
Copyright © 2011-2022 走看看