JNI - how to use multiple Jni wrapper instances with different fields?

后端 未结 2 485
有刺的猬
有刺的猬 2021-01-23 07:30

background

I have an android project that uses JNI (using NDK) to code in both Java and C/C++.

I\'ve created a Jni java wrapper on the java side that will do a

2条回答
  •  庸人自扰
    2021-01-23 08:25

    You need to have C++ classes on the JNI side, and you need to associate an instance of the C++ class with each instance of your JNI wrapper class. You will need to add native methods to new and delete the C++ class instances, and you will need a bullet-proof way of ensuring that the delete-calling method gets called every time an instance of your JNI wrapper class is released, e.g. via a close() method, finally{} blocks, or even a finalize() method: this is one case where its usage is legitimate. You need to store a pointer to the C++ instance in each Java instance, e.g. as a Java long, and you need to get hold of that on the C++ side and cast it to a C++ class instance to get hold of the per-instance data.

提交回复
热议问题