2

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.

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.

which is at

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

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

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

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.

InnocentKiller
  • 5,234
  • 7
  • 36
  • 84
Sachin
  • 501
  • 10
  • 18

1 Answers1

3

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.

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.

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.

Your Java code can access fd using some workarounds.

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.

Community
  • 1
  • 1
Alex Cohn
  • 56,089
  • 9
  • 113
  • 307
  • After looking into your comments for other question about linking lib I have tried below, also I have copied libcutils.so into my libs/armeabi folder from android-ndk-r9c/platforms/android-19/arch-arm/usr/lib/rs/libcutils.so LOCAL_LDLIBS := -llog LOCAL_LDLIBS += -L$(LOCAL_PATH)/libs/libcutils.so LOCAL_LDLIBS += -lcutils Still it gives error : cannot find -lcutils – Sachin Feb 26 '14 at 05:54
  • 1
    Do not depend on libcutils.so in the RS NDK package; it was included erroneously and will be removed in the very near future. (all of the .SOs in there are being removed, only the static libs will remain) – Tim Murray Feb 26 '14 at 07:04
  • 1
    @Sachin: if you have copy/past from `Android.mk` correctly, you copied libcutils to `/libs/armeabi` and told the linker to look for it in `/libs`; no wonder that it's not found. But in light of [Tim](http://stackoverflow.com/users/1988713/tim-murray)'s remark, I would recommend to pull the library from your device or emulator. You can alternatively copy [ashmem-dev.c](https://android.googlesource.com/platform/system/core/+/master/libcutils/ashmem-dev.c) from the official git repository, and link it (or necessary parts of it) statically it into your library. – Alex Cohn Feb 26 '14 at 08:26
  • Thanks all, Some how I am able to link this lib in my project, Now I have valid fd for shared memory. Same I have passed in my application. I am sharing this fd by calling PercelFileDescriptor by pfd = ParcelFileDescriptor.adoptFd(fd); And sending this pfd to other app/process which is remote. In this remote process I am getting PercelFileDescriptor through which FileDescriptor fd = pfd.getFileDescriptor(); FileOutputStream fos = new FileOutputStream(fd); FileChannel fc = fos.getChannel(); int len = fc.write(bytebuf); Some how write len = 0,any suggestion why write is failing – Sachin Feb 26 '14 at 09:16
  • @TimMurray: note that (as of ) `build/core/build-binary.mk` still lists **cutils** and some other leftovers from the `lib/rs` as legitimate system libraries. – Alex Cohn Jun 25 '14 at 09:21