sokol
sokol copied to clipboard
Android: bundle resources in sokol-samples build script
After starting the sample "loadpng-sapp" on Android 9 I just see a solid red screen. Other samples seem to work fine.
I built the sample on Windows 10 using these commands:
./fips setup android
./fips set config sapp-android-make-debug
./fips build
./fips run loadpng-sapp
Just linking some related tickets and PRs, I need to do another round of Android fixes eventually. I'm currently not sure if file loading ever worked on Android, the required assets might not be copied onto the device (in "fips run"), or sokol_fetch.h might attempt to load them from the wrong location.
https://github.com/floooh/sokol/issues/356
https://github.com/floooh/sokol/pull/220
Ok, I just checked, and the loadpng-sapp sample indeed doesn't work (or any of the other samples loading files), this is because there's no mechanism in place yet to package the resource files into the APK.
@floooh Thank you so much for looking into it! What's the rough timeline for adding this mechanism? Greetings from Friedrichshain, Heiko
What's the rough timeline for adding this mechanism?
I don't know. I don't commit to timelines for my hobby stuff ;)
This isn't a sokol issue though but a toolchain / build-system issue, so a fix would need to go into fips (and people using other build systems would need to come up with their own solution).
@floooh Thank you for the quick reply! Is there a way to get assets into the APK "manually" so sokol_fetch.h finds them?
I think this requires two things:
-
before creating the APK, the data files must be copied into the "right place", which means this fips helper script would probably need to be extended: https://github.com/floooh/fips/blob/8e67bb6810409fcfb43746ec24589f8df77b6525/tools/android-create-apk.py#L101
-
the paths which are passed to sokol_fetch.h must be changed to absolute, and those absolute paths must "somehow" be obtained from Android...
This assumes that Android maps the APK content into the filesystem, which I'm not sure of. It's been a long time since I looked at this.
In case this is useful, I solved this by modifying your android packaging script.
Basically I create a new assets
folder and copy the files I want to access into that folder.
Then I add them to the unaligned .apk
with the aapt
tool (right after adding the .so
with aapt
) and continue the process normally. I don't think so but I converted your fips android packaging script to powershell some time ago and might have mode some extra changes but I think this is the only relevant part to adding assets.
To access the files later from a NativeActivity
you can use the AAssetManager
API.
Something like this:
#include <android/native_activity.h>
// returns a heap allocated binary data buffer, null terminated when reading as a string (read_str == true)
unsigned char* android_fileRead(ANativeActivity* nativeActivity, const char* file_name, int* out_buffer_size, bool read_str) {
AAssetManager* asset_manager = nativeActivity->assetManager;
AAsset* asset = AAssetManager_open(asset_manager, file_name, AASSET_MODE_UNKNOWN);
int size = AAsset_getLength(asset);
// if reading as a string, then add 1 extra byte for the null char
unsigned char* data_buffer = ce_calloc(1, sizeof unsigned char * (size + (read_str ? 1 : 0)));
*out_buffer_size = AAsset_read(asset, data_buffer, size);
AAsset_close(asset);
if (read_str) {
data_buffer[size] = '\0';
*out_buffer_size = *out_buffer_size + 1;
}
return data_buffer;
}