4

I have an NDK library that I am creating that needs to contain and access a binary data file (.dat extension). I am having trouble getting the compiled library to see this file. To make things a little more difficult, I am doing this within a library package.

I think it would work if, during my Android.mk file, I copy this .dat file to my app's resources folder, and then access that from within the app, but I feel like there must be a better way.

Any suggestions?

Ned
  • 6,280
  • 2
  • 30
  • 34

1 Answers1

7

Instead of resources, put it in the assets folder; NDK provides API to access assets from native code.

Often, we unpack some "files" from the resources or assets to the file system (e.g. /sdcard) on the first run of the app after install. This approach works best when the files must be used by external apps and libs (e.g. to play sounds), or when these files will be changing.

Finally, you can link the data into your .so during ndk-build. This will resolve the question how the .dat file will be copied into the app folder, but reading it may be tricky, and modifying - impossible. You don't need to create a huge library. You can create a mock-up library that contains the data. If I understand correctly, you can ignore the file structure, headers, etc. You only need a file named lib something .so in your libs/armeabi (or other) folder.

Alex Cohn
  • 56,089
  • 9
  • 113
  • 307
  • This answer got me along the right path. What I ended up doing was adding a line to Android.mk to copy the file to the app's Assets folder. When the app launches I copy this file to the SD card, and finally pass in that path to my CPP function. I did it this way so that it's not compressed, and I wanted to be able to access it using the file access methods within my CPP library rather than modifying it for NDK-specific file access. – Ned Sep 03 '14 at 17:37
  • 1
    Thanks, @MithunSarkerShuvro. I have changed the [link](https://developer.android.com/ndk/reference/group/asset). Luckily, now this info is available on official **developer.android.com/ndk**. – Alex Cohn May 16 '19 at 08:37