what is standard process to load assets from C++?

138 views
Skip to first unread message

Phil Dougherty

unread,
Aug 17, 2020, 9:35:30 PM8/17/20
to android-ndk
Using the NDK to develop a native oculus quest appplication: 

I'm finding conflicting and outdated information, and would love to be pointed in the right direction.

On PC, for example, I have a simple folder structure:
- resources
- - images
- - - blah0.png
- - - blah1.png  
- - - ...
- - shaders
- - - blah0.spv
- - - blah1.spv
- - - ...

All I have to do is put my resources folder in the running directory of the .exe and I have easy direct access to them.

For android, I'm getting the idea that things aren't so simple.

my build.gradle uses the following snippet:

  sourceSets {
    main {
      manifest.srcFile 'AndroidManifest.xml'
      assets.srcDirs 'build/cmake/release/arm64-v8a/resources'
    }
  }

(the srcDirs string is the resources directory which CMake creates)

What alterations do I need to make to my C++ code which loads those files essentially as simply as "fopen("resources/images/blah0.png",...);"?

I would love to be pointed to any up-to-date resources that 1. would reassure me that my resources folder is in fact going to the right place in the apk, and 2. would show me how to load these files in C++.

Thanks!

Dan Albert

unread,
Aug 17, 2020, 11:19:59 PM8/17/20
to android-ndk

--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-ndk...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/android-ndk/181ba0cb-2e13-4523-9469-2675510cafc9n%40googlegroups.com.

Phil Dougherty

unread,
Aug 18, 2020, 12:04:33 AM8/18/20
to android-ndk
Ok that's a great start!

I've been experimenting, and when I change my gradle snippet from before to

assets.srcDirs 'some/random/directory/on/my/hd'

then, when I enumerate the root assets directory via 

  AAssetDir *rootAssets = AAssetManager_openDir(aassman, "");
  const char *f = AAssetDir_getNextFileName(rootAssets);
  while(f)
  {
    log(f);
    f = AAssetDir_getNextFileName(rootAssets);
  }
  AAssetDir_close(rootAssets);

I get all of the files that were in that directory, but none of the directories.
This would explain why, when I have my gradle assets.srcDirs set to what it was before ('build/cmake/release/arm64-v8a/resources'), the enumeration prints nothing.

Is it not possible to simply plop my resources directory hierarchy into my apk?
I'm unsure which possibility is occurring:
1. the resources hierarchy is present in the .apk, but "getNextFileName" bypasses directories, and my AAssetManager_open calls to that hierarchy ("resources/images/blah0.png") is inexplicably failing for some other reason
2. the resources hierarchy isn't present, because gradle's assets.srcDirs only copies files and not directories
3. there is some restriction on using gradle, or AAsset, or both which forbids hierarchies altogether?
4. ????

What I want is simply the ability to tell gradle "please put the resources folder and its contents where it is accessible in the .apk". I feel like this is an extremely standard and not-niche request, yet am having a ton of trouble finding any documentation showing just that.

The examples you linked seem to point to a flat resources folder, and then access their content by enumerating the files from that folder (rather than any "hardcoded" relative filepaths).
Another concern of mine is- if I'm pointing to my "resources" folder via gradle, will that copy its contents, but not the folder itself? so if I want the folder itself copied, I should put the resources folder in another folder where it is the sole entry, and instead point to that?

Please excuse my verbosity here- I appreciate your help and want to be clear that what I want is something I would expect should be quite simple!

Phil Dougherty

unread,
Aug 18, 2020, 11:48:40 AM8/18/20
to android-ndk
I've articulated the problem perhaps more concisely (and certainly with better formatting) here:  https://stackoverflow.com/questions/63471752/how-can-i-add-resources-images-sound-files-shaders-etc-to-an-ndk-project
I do think this google groups forum is the only active ndk community I can find, so please don't assume that because it's on stackoverflow I don't need your help!

As always, thank you all for your time and expertise!

Phil Dougherty

unread,
Aug 18, 2020, 12:25:06 PM8/18/20
to android-ndk
I've figured it out- see the stackoverflow link for details (tl;dr: I was pointing to a directory that gradle itself was responsible for building, and so the asset packaging was occurring at unreliable times, among other things).

Thanks for your help!
Reply all
Reply to author
Forward
0 new messages