在Android的NDK共享内存内存、Android、NDK

2023-09-05 03:10:02 作者:我酷你随意

我想从一个进程/应用程序在传输大量数据到另一个进程/应用程序的安卓,但一些如何创建共享内存使用内存文件中的的Java 层没有按'科技工作。

I wanted to transfer large data from one process/app to another process/app in Android, but some how creating shared memory using Memory File in Java layer doesn't work.

于是试图在Android原生创建共享内存,什么地方我看了 ashmem_create_region API可使用。不过貌似这个API是无法访问或打开。这里面libcutils.so。

So then tried to create shared Memory in android native, somewhere I read ashmem_create_region API can be used. But looks like this API is not accessible or open. It is inside libcutils.so.

这是

android-ndk-r9c/platforms/android-19/arch-arm/usr/lib/rs/libcutils.so

Q1。如何访问该库在Android原生code。

Q1. How to access this library in android native code.

Q2。如果这是不可能访问又是什么在NDK工具给这个目的

Q2. If it is not possible to access then what is purpose of giving this in ndk tool.

Q3。如果不知何故,我挂libcutils.so,并能创建使用libcutils.so那我可以用这个的fd与MemoryFile用Java层映射,因此将避免应用本地副本写入每个数据事务在本机共享内存。

Q3. If somehow I linked libcutils.so and able to create shared Memory in native using libcutils.so Then can I use this fd to map with MemoryFile in Java layer so it will avoid application native copy to write for each data transaction.

推荐答案

libcutils.so 被运NDK启用的 renderscript整合。这仍然是一个未公开的系统库,在这个意义上,Android不承诺支持所有平台上的所有的API,特别是在未来的平台上,看到更多的在Android的NDK用户组。

libcutils.so is shipped in ndk to enable renderscript integration. It remains an undocumented system library, in the sense that Android does not promise to support all its APIs on all platforms, and especially on future platforms, see more on on android-ndk user group.

这说,我应该承认,使用ashmem是相当安全的,因为它是明确的记载的Java API 的,因为第1节。我写了一个简短的解决方案在SO 链接其他地方对libcutils.so 。

This said, I should acknowledge that using ashmem is quite safe, as it is explicitly documented in Java API since v.1. I wrote a short solution for linking against libcutils.so elsewhere on SO.

下面是关于的注意事项与IPC通过这样的讨论ashmem_create_region() API和:的 https://groups.google.com/forum/m/#!topic/android-platform/L6a6Xvn4HSI 。 TL; NR :你仍然需要粘结剂的握手

Here is a discussion about ashmem_create_region() API and caveats with IPC through this: https://groups.google.com/forum/m/#!topic/android-platform/L6a6Xvn4HSI. tl;nr: you still need Binder for handshake.

您的Java code可以访问 FD 使用一些的解决方法的。

Your Java code can access fd using some workarounds.

更新:新NDK的 r9d 是出来了,和 libcutils.so 不再是在平台/ Android的-19 /弓臂/ usr / lib目录目录, @Tim穆雷的承诺在下面的评论。

Update: new NDK r9d is out, and libcutils.so is no longer in the platforms/android-19/arch-arm/usr/lib directory, as @Tim Murray promised in comments below.