JNI-Tips-提要

引:
项目需要用到JNI,实际上还需要在native跟Java之间写一些代码,来连接二位,需要做进一步了解。本文主要对官方的JNI Tips做一些简单的关键提取
参考:
JNI Tips


Evernote连接

XMind 文档下载

JNI Tips.png

文本:

JNI Tips

definition
Java Native Interface
how it works
http://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/jniTOC.html
JavaVM and JNIEnv
JavaVM
one process can only create one JavaVM in Android
JNIEnv
provides most of JNI functions. Your native functions all receive a JNIEnv as first argument
used for thread-local, so you cannot share a JNIEnv between threads (GetEnv can be used to discover the thread’s JNIEnv when no other way)
Threads
a thread will have no JNIEnv unless it’s attached, before this it cannot make JNI calls (all threads are Linux threads, scheduled by the kernel)
Threads attached through JNI must call DetachCurrentThread before they exit
jclass, jmethodID, and jfieldID
steps to access an object’s field from native code
FindClass:—> get the class object reference for the class
GetFieldID:—>Get the field ID for the field with GetFieldID
GetIntField:—>Get the contents of the field with something appropriate
call a method—> get a class object reference and then a method ID
it’s useful to look the values up once and cache the results in your native code. Because there is a limit of one JAVAVM per process
if want to cache IDs when a class is loaded, add a piece of code that allow the native code to cache field offsets
Local and Global Reference
almost every object returned by a JNI function is a “local reference”—>it’s valid for the duration of the current native method in the current thread. Even if the object itself continues to live on after the native method returns, the reference is not valid
the only way to get non-local references is via the functions NewGlobalRef and NewWeakGlobalRef (the global reference is guaranteed to be valid until you call DeleteGlobalRef)
to see if two reference refer to the same object, you must use the IsSameObject function, never use == in native code
jfieldIDs, jmethod are opaque types, not object references, should not be passed to NewGlobalRef
if you attach a native thread with AttachCurrentThread, the code you are running will never automatically free local references until the thread detaches. Any local references you create will have to be deleted manually.
UTF-8 and UTF-16 Strings
java use UTF-16, JNI provides methods that work with Modified UTF-8 as well.
you cannot pass arbitrary UTF-8 data to JNI and expect it to work correctly
Android currently does not require a copy in GetStringChars,however GetStringUTFChars requires an allocation and a conversion to UTF-8. UTF-16 strings are not zero-terminated, \u0000 is allowed, so you need to hang on to the string length like jchar pointer
Don’t forget to Release the strings you Get. the string functions return jchar or jbyte
Data passed to NewStringUTF must be in Modified UTF-8 format
Primitive Arrays
You must Release every array you Get
release mode arguments alternative values
0
buffer freed
JNI_COMMIT
do nothing/ buffer not freed
JNI_ABORT
buffer freed, Earlier wirtes are not aborted
Region calls
advantages
reducing overhead by reducing JNI call requirements
do not require pinning or extra data copies
reduces the risk of programmer error–> no risk of forgetting to call Release
Exception
your must not call JNI functions while an exception is pending, except several special kinds of API: delete/ exception process/stack process/ release process …
Extended Checking
JNI does very little error checking ~~!
Android offers a mode called CheckJNI
array, bad pointers, Class names, Critical call,Direct ByteBuffers, Exceptions, JNIEnvs, jmethodIDs, References,Release modes, Type safety, UTF-8
Native Libraries
steps
call System.loadLibrary
provide a native function : jint JNI_OnLoad(JavaVM
vm void* reserved)
in JNI_OnLoad, register all your native methods
64-bit Considerations
you need to stash your native pointers in a long field rather than an int
Unsupported Features/ Backwards Compatibility
Define class
dynamic lookup of native functions
detaching threads
weak global references
Local references
determining reference type with GetObjectRefType


关注公众号“夕识”,雕刻时光,不负流年

Powered by KyleCe

Copyright © 2015 - 2019 KyleCe All Rights Reserved.

访客数 : | 访问量 :