Image resources disappearing from react-native app

2019-07-01 16:44发布

问题:

I'm having a strange problem that I cant quite figure out the reason for. I have an app created in react-native and I'm using react-native-navigation to handle the navigation. The problem is that the images I add to buttons, tabs, etc disappear after I unplug my device from the computer.

To make it clear; all my buttons have icons while I'm connected to the computer and use the app, but once I disconnect from the computer and open the app every icon disappears.

Here is how I load the images

 Navigation.startTabBasedApp({
tabs: [
  {
    label: 'One',
    screen: 'Screen1',
    icon: require('./images/icon1.png'),
    selectedIcon: require('./images/icon1_selected.png'),
    title: 'Screen One'
  },
  {
    label: 'Two',
    screen: 'Screen2',
    icon: require('./images/icon2.png'),
    selectedIcon: require('./images/icon2_selected.png'),
    title: 'Screen Two'
  }
]
});

any ideas or help on this issue?

回答1:

Pure guesswork in the comments, but turned out that react-native run-android doesn't automatically bundle your assets as it's a development build. If you need to test the build while completely disconnected from your computer, you can install using a release build using --variant=release as that will always bundle your assets with the apk.