Issue(android): where to place image assets relative to js bundle file if bundle file is located outside of project root?
We have a small service where we upload JS bundles on each build. So, for example we have app com.example
, if we want to update our app(while app review is pending in the app store) then we:
/data/data/com.example/files/bundle/android.bundle
Imagine that in the react App component we have a single image with path star.png
.
When we activate a new bundle then star image is not shown.
I've checked the image path via https://reactnative.dev/docs/image#resolveassetsource in release build and the image uri resolves just to star
.
So I tried the following file structures:
ā1
data
--data
----com.example
------files
--------bundle
----------android.bundle <= dynamic bundle from remote server
----------star // <= our image
ā2
data
--data
----com.example
------files
--------bundle
----------android.bundle <= dynamic bundle from remote server
----------star.png // <= our image
But still image is not shown if the bundle file is outside of the project root.
Questions:
Thanks in advance
Bundle assets should be placed in the same folder where js bundle file lives(tested only on android).
You can always get bundle asset path via https://reactnative.dev/docs/image#resolveassetsource.
How to test:
star.png
to the project root.import React from 'react';
import {
SafeAreaView,
Text,
StatusBar,
Image
} from 'react-native';
import Star from './star.png';
const App: () => React$Node = () => {
const r = Image.resolveAssetSource(Star);
console.log(r);
return (
<>
<StatusBar barStyle="dark-content" />
<SafeAreaView>
<Text>TestAssetsApp</Text>
<Image
style={{width: 50, height: 50}}
source={Star}
/>
</SafeAreaView>
</>
);
};
export default App;
MainApplication.java
(my app package is called com.testassetsapp
):public class MainApplication extends Application implements ReactApplication {
private final ReactNativeHost mReactNativeHost =
new ReactNativeHost(this) {
@Override
public boolean getUseDeveloperSupport() {
return BuildConfig.DEBUG;
}
@Override
protected List<ReactPackage> getPackages() {
@SuppressWarnings("UnnecessaryLocalVariable")
List<ReactPackage> packages = new PackageList(this).getPackages();
// Packages that cannot be autolinked yet can be added manually here, for example:
// packages.add(new MyReactNativePackage());
return packages;
}
@Override
protected String getJSMainModuleName() {
return "index";
}
// Add this method
@Override
protected String getJSBundleFile() {
return "/data/data/com.testassetsapp/files/android.bundle";
}
};
react-native bundle --platform android --dev false --entry-file index.js --bundle-output android.bundle --assets-dest assets
cd android && ./gradlew installDebug
NOTICE: test only via ./gradlew installDebug
without metro bundler running as it always rewrites bundle path.
Now if you run the app you will get an error "Bundle not found".
android.bundle
file to /data/data/com.testassetsapp/files/android.bundle
Now if you run the app it should start. But star image is not yet displayed.
assets
folder to /data/data/com.testassetsapp/files
so app data structure should be the following:
Now if you restart the app star image should be displayed.