
One is to pass it through the return value of the native function, and the other is to pass it through the function of the jni layer to call back to the java layer. There are generally two ways to pass the array defined by the Jni layer to the Java layer.

pass the array from the Jni layer to the Java layer Look at the corresponding implementation code of native.c:Ģ. For local pointer use, the pointer must remember to release it through ReleaseByteArrayElements after use, otherwise it will cause a memory leak.įirst look at the definition of Native.java: In fact, the JVM adds a reference count to the array object allocated on the heap to ensure that it will not be released during garbage collection. In the latter, the latter is in the form of pointers, directing the local array pointer to the array address on the Java side. The former is to copy the value and copy the data of the Java-side array to the local array. Generally, there are two functions to get its value, one is GetByteArrayRegion and the other is GetByteArrayElements. The Jni layer receives the byte array passed by the Java layer. I have added comments to the code in key places, the complete code See the attachment at the back of the blog post.

There are two main files of the sample code, one is Native.java, which is the class of the Java layer the other is Native.c, which is the file of the JNI layer. I briefly summarized, from the Java layer to the Jni layer, from the Jni layer to the JAVA layer, there are three ways to transfer, the following is a brief introduction with code examples. Of course, the Jni layer also needs to return the data stream received from the Socket to the Java layer. A typical application is that the Java layer passes the data stream that needs to be sent to the client to the Jni layer, which is sent by the Socket code of the Jni layer.
ANDROID NDK GETBYTEARRAYREGION EXAMPLE ANDROID
In Android development, arrays (byte) are often passed between the Java code and the Jni layer.
