fopen/fread APK Assets from NativeActivity on Android

Mike Weir picture Mike Weir · Aug 6, 2013 · Viewed 8.7k times · Source

I have only been able to find solutions dated 2010 and earlier. So I wanted to see if there was a more up-to-date stance on this.

I'd like to avoid using Java and purely use C++, to access files (some less-or-more than 1MB) stored away in the APK. Using AssetManager means I can't access files like every other file on every other operating system (including iOS).

If not, is there a method in C++ where I could somehow map fopen/fread to the AssetManager APIs?

Answer

Brian picture Brian · Feb 14, 2015

I actually found pretty elegant answer to the problem and blogged about it here.

The summary is:

  • The AAssetManager API has NDK bindings. This lets you load assets from the APK.
  • It is possible to combine a set of functions that know how to read/write/seek against anything and disguise them as a file pointer (FILE*).
  • If we create a function that takes an asset name, uses AssetManager to open it, and then disguises the result as a FILE* then we have something that's very similar to fopen.
  • If we define a macro named fopen we can replace all uses of that function with ours instead.

My blog has a full write up and all the code you need to implement in pure C. I use this to build lua and libogg for Android.