Image doesn't show after bundling into the apk
Here is the project structure:
android-project
+---app
| \---src
| \---main
| \---assets
| \---java
| \---res
+---reactnative
| \---assets
| \[email protected]
| \[email protected]
| \[email protected]
----index.android.js
Here's the code to show the image:
<Image
source={require('./reactnative/assets/image.png')}
resizeMode='contain'
style={styles.image} />
The above code works fine when running together with localhost react-native start. But it doesn't work after bundling react native resources into the apk:
react-native bundle --platform android -dev false --entry-file index.android.js \
--bundle-output app/src/main/assets/index.android.bundle \
--assets-dest app/src/main/res/
Here's the output:
android-project
+---app
| \---src
| \---main
| \---assets
| \---index.android.bundle
| \---java
| \---res
| \---drawable-xxxhdpi
| \---reactnative_assets_image1.png
| \---drawable-xxhdpi
| \---reactnative_assets_image1.png
| \---drawable-xhdpi
| \---reactnative_assets_image1.png
| \---drawable-mdpi
| \---reactnative_assets_image1.png
Is it the right way to generate the apk with react native? Please troubleshoot. Thanks!
:+1: got the same problem with iOS too.
@ms88privat for ios, you can do this way:
react-native bundle --assets-dest ./ --entry-file ./ReactComponent/main.ios.js --bundle-output ./Dist/main.jsbundle --platform ios. Change the the name of bundle output and assets-dest as you wish.- Put Dist and assets folder as folder references in your project.

After debug around the RCTImageView.m, I recognize that they load our assets as the same way with external assets, So we just make sure the path is correct, and It works !
It's interesting that there is no one else ever asked this question. Am I the only one getting this issue with 0.14.2 on Android?
I have tried the latest example on master. Same issue.
I am not sure if that solution is correct @hoangviet. From my experience, you should not add any assets nor main.jsbundle to your app. The only thing you need is to attach react-native-xcode.sh script as a next build phase and that will bundle all the things automagically for you, both for release and for demo purposes.
@ide side note - I love that feature a lot! That script actually made the CI lots easier, esp. as a part of another process.
@hoangviet Do you have a same solution for Android ? @rockerhieu I have the same issue. Do you find a solution ?
Edit : it's ok with rockerhieu command line.
@rockerhieu why reactnative_assets_image1.png ? it should be reactnative_assets_image.png
I also meet this bug in IOS with bundled file~~~
Fixed this issue with @hoangviet answer. Please noticed that, when you add the assets folder into xcode project, use the operation "copy reference" rather not "create group".
@hoangviet Thanks very much.
This should work without the assets option in the bundler. At least, it has for me.
Assets do not seem to be bundled into the main offline bundle by default. Definitely does not work on iOS using 0.18. It works fine in dev mode when image is fetched from dev server but obviously I want to deploy the app standalone.
What are the instructions for iOS and Android to get the assets/images into the deployed app? What is the syntax of the shell commands for packager that need to be run. Where does React look in the app bundle when images are 'required' using a relative path to the hosting React component??
Thanks.
Follow this process:
- Run the bundle command from your project root (adjust as needed):
react-native bundle \
--platform ios \
--entry-file index.ios.js \
--bundle-output ./main.jsbundle \
--assets-dest ./ \
--dev false
- Above step will generate an "assets" folder and a "main.jsbundle" file. Add them both to your project in Xcode.
- Finally, make sure that the images path in your JS follow the same path relative to the assets folder. E.g. if your image has:
source={require('./app/img/image.png'), that file should be in "assets/app/img/image.jpg".
@rockerhieu Thanks for bringing up this issue!
This issue should be resolved in later versions of React Native. Assets are also automatically bundled by the iOS and Android release builds.
Just for anybody trying to Run Release mode / main.jsbundle / assets on iOS Simulator and Images are blank, they work on device but not in Simulator.
React-Native 0.36.0. iOS 10.1 Simulator
hey guys
found the trick base on Image doesn't show after bundling into the apk.
as you know require('index.html') work on simulator and emulator for both &driod and IOS
but problem will faced when you bundle and you want release your app
for android just follow this enablejavascript and copy you files in assets folder in YOUR_APP/app/src/main/assets and use just picture below.
and for ios just bundle package with
react-native bundle --entry-file index.ios.js --platform ios --dev false --bundle-output ios/main.jsbundle --assets-dest ios
when bundle created you have main.bundle.js and asset folder
just copy you files in .xcworkspace like this image
and set free all refrence in will be and unchanged just like mine.
i hope this tutorial help you from confusing release app in RN