问题
I am working on developing an application in which I have an application layer in Java and one service which is nothing but a plugin in C++. I need to call native code APIs from the application layer which will be called through the JNI layer which is developed by the AOSP framework.
Now the problem is, I need to pass a chunk of data from the application layer to the native layer which will pass to the native layer through JNI calls.
Instead of passing data directly, I would like to use sharedMemory (or any efficient way). Can anyone please suggest to me that how can I pass the data from the application layer to the native code?
Is there any method available in such a way that I can allocate memory in the native layer and access that memory location from the application layer to store the data into that memory location?
I can't use the user implemented JNI layer.
回答1:
Short answer: NO.
Longer answer: You mentioned AOSP, so I assume you are working with Android OS.
The JNI implementation in AOSP is very particular, and you can communicate with it only via function arguments and primitive return values.
It is possible to create new instances of Java objects in native code using JNIEnv methods, but it is highly discouraged.
In general, the whole process of moving large amounts of data between Java and native over JNI is problematic.
Read this for more details: https://developer.android.com/training/articles/perf-jni
来源:https://stackoverflow.com/questions/65061395/passing-data-between-java-and-c