Git Product home page Git Product logo

android_jnihelper's Introduction

每个native 函数,都至少有两个参数(JNIEnv* ,jclass 或者 jobject)

  1. 当native 方法为静态方法时候 ,jcalss 待办native 方法所属的class 对象(JNITest.class
  2. 当native 方法为非静态方法时:jobject代表native 方法所属的对象

jni基本类型

typedef uint8_t  jboolean; /* unsigned 8 bits */   boolean
typedef int8_t   jbyte;    /* signed 8 bits */     byte
typedef uint16_t jchar;    /* unsigned 16 bits */  jchat
typedef int16_t  jshort;   /* signed 16 bits */    short
typedef int32_t  jint;     /* signed 32 bits */	   int 
typedef int64_t  jlong;    /* signed 64 bits */	   long
typedef float    jfloat;   /* 32-bit IEEE 754 */   float
typedef double   jdouble;  /* 64-bit IEEE 754 */   double
java 类型 jni类型 c/c++类型 大小
Boolean jboolean unsigned char 无符号8位
Byte jbyte char 有符号8为
Char jchar unsigned short 无符号16为
Short jshort short 有符号16位
Int jint int 有符号32位
Long jlong long long 有符号64位
Float jfloat float 32位
Double jdouble double 64位

jni 引用类型对象

java类型 原生类型
Java.lang.Class jclass
java.lang.Throwable jthrowable
java.lang.String jstring
Other objects jobjects
java.lang.Object[] jobjectArray
boolean[] jbooleanArray
byte[] jbyteArray
char[] jcharArray
short[] jshortArray
int[] jintArray
long[] jlongArray
float[] jfloatArray
double[] jdoubleArray
other arrays jarray

cmake 头文件,编译,链接

  1. 添加头文件目录INCLUDE_DIRECTORIES 所有的 .h 文件不用再去以路径的形式 引入

    语法

    include_directories([AFTER|BEFORE] [SYSTEM] dir1 [dir2 ...])
    它相当于g++选项中的-I参数的作用,也相当于环境变量中增加路径到CPLUS_INCLUDE_PATH变量的作用
    include_directories(../../../thirdparty/comm/include)
    g++ -I 
    编译程序按照-I指定的路进去搜索头文件。
    -I/home/include/表示将-I/home/include/目录作为第一个寻找头文件的目录,寻找的顺序是:
     /home/include/ -->/usr/include-->/usr/local/include 
     
    

设置so动态最后的输出路径

#设置生成的so动态库最后输出的路径 
方式1
# set(jnilibs "${CMAKE_SOURCE_DIR}/src/main/jniLibs")
#set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${jnilibs}/${ANDROID_ABI})
#方式2
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/../jniLibs/${ANDROID_ABI})

Cmake 个人遇到的错误

  1. android ndk error :undefined reference to.AS做了编译缓存,你需要重新clean一下你的project。 然后refresh linked c++ project.即可!

  2. More than one file was found with OS independent path. gradle 配置增加

    packagingOptions{
        pickFirst  '**'
    }
    
  3. cause executing external native build for cmake cmakelists.txt 生成多个so 文件的时候,字模块的cmakelists.txt addLibray 如下。

    正确的方式

    include_directories("${CMAKE_CURRENT_SOURCE_DIR}/include")
    file(GLOB_RECURSE jni_src "${CMAKE_CURRENT_SOURCE_DIR}/*.cpp")
    add_library( # Sets the name of the library.
                 jnihelper-lib
    
                 # Sets the library as a shared library.
                 SHARED
    
                 # Provides a relative path to your source file(s).
                  ${jni_src}
                 )
    

    错误的方式

    add_library( # Sets the name of the library.
                 native-lib
    
                 # Sets the library as a shared library.
                 SHARED
    
                 # Provides a relative path to your source file(s).
                 src/main/cpp/native-lib.cpp
                 src/main/cpp/JNIHelp.cpp
                  )
    

释放问题: 1,NewByteArray是创建的一个对象,这个对象好像也应该被释放,但是释放了又如何返回给java端呢; 也就是说,这个对象是由java端来维护返回其生命周期和资源占用的 2,ReleaseByteArrayElements(result,buf,0). 为什么和我原来的想法不一致呢?我认为这个维护应该在java的一端维护,但是java的一端是根本无从维护的,因为我们不能要求java一端区分一个字节数组来自于jni调用,还是直接的java语言本身,所以只能由C/C++一方维护.这个结论显然可以推广到jni的其他类似的接口上

android_jnihelper's People

Contributors

gzbbxu avatar

Stargazers

Buerck avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.