Skip to content

Commit

Permalink
fix: android release mode assets (#263)
Browse files Browse the repository at this point in the history
Android release mode bundles `glb` assets into the `raw` folder if they
are bundled with the `require` syntax, instead of linking them with
something like `react-native-assets`. This avoids having to bundle the
assets twice if you are using them via `require`, which works correctly
on iOS.

Otherwise the asset cannot be found by android. And this is a problem
only in release mode Android.
  • Loading branch information
p-sebastian authored Jan 5, 2025
1 parent 362a210 commit 69d295b
Showing 1 changed file with 11 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,18 @@ ByteBuffer loadAsset(String uriString) throws Exception {
}
}

// It's bundled into the Android resources/assets
Log.i(NAME, "Assumed assetName: " + uriString);
try (InputStream stream = reactContext.getAssets().open(uriString)) {
return streamToDirectByteBuffer(stream);
int rawResourceId = reactContext.getResources().getIdentifier(uriString, "raw", reactContext.getPackageName());
// It's bundled into the Android resources/assets
if (rawResourceId == 0) {
try (InputStream stream = reactContext.getAssets().open(uriString)) {
return streamToDirectByteBuffer(stream);
}
// For assets bundled with 'require' instead of linked, they are bundled into `res/raw` in release mode
} else {
try (InputStream stream = reactContext.getResources().openRawResource(rawResourceId)) {
return streamToDirectByteBuffer(stream);
}
}
}

Expand Down

0 comments on commit 69d295b

Please sign in to comment.