【问题标题】:react-native some images are loaded, other notreact-native 加载了一些图像,其他没有
【发布时间】:2019-10-24 11:46:39
【问题描述】:

环境

  • react-native 0.60

会发生什么:

我有一个组件,它使用<ImageBackground> 创建一种图像按钮。

如果我将应用程序捆绑为 debugRelease,它就像一个魅力,并且显示每个按钮的 所有图像。 如果我将应用程序捆绑为 releaseBundle,则会丢失 7 个图像中的 4 个(所有时间都是相同的图像)。

我的尝试

所以按钮 1 不起作用,按钮 2 - 4 起作用,而按钮 5 - 7 甚至不起作用。 可以肯定的是,图像文件没有损坏,我用一个新名称复制了按钮 2 的图像,并用这个替换了图像 1。 无论如何,图片都没有显示!

所以我启动了 Android Studio 并在 debug-APK 和 release-APS 中打开了 apk 来检查里面的文件。 结果如下:

您是否有任何线索,为什么只有一些图像文件没有以正确的方式捆绑到 Release-APK 中(即使文件 spenden.jpg 是工作文件 kontakt.jpg 的副本,例如如上所述)?

这是我实现它的方式:

...

const getHomeButtonList = (data, dropdowns) => [
  {
    id: 'offer',
    bgImage: { uri: 'erwin' },
    btnText: 'ANGEBOTE \n DER SKG',
    navigateTo: 'Offer',
  },
  {
    id: 'contact',
    bgImage: { uri: 'kontakt' },
    btnText: 'KONTAKTE',
    navigateTo: 'Contact',
    data: {
      data,
      dropdowns,
    },
  },
  {
    id: 'videos',
    bgImage: { uri: 'videos' },
    btnText: 'VIDEOS',
    navigateTo: 'VideoList',
  },
  {
    id: 'innovationen',
    bgImage: { uri: 'innovationen' },
    btnText: 'INNOVATION IN MEDIZIN UND \nGESUNDHEITSWESEN',
    navigateTo: 'CategorizedPage',
    data: {
      headerTitle: 'Innovationen',
      headerImage: 'innovationen',
      data: data.innovations,
    },
  },
  {
    id: 'nebenwirkungen',
    bgImage: { uri: 'nebenwirkungen' },
    btnText: 'HILFE BEI NEBENWIRKUNGEN',
    navigateTo: 'CategorizedPage',
    data: {
      headerTitle: 'Nebenwirkungen',
      headerImage: 'nebenwirkungen',
      data: data.sideeffects,
    },
  },
  {
    id: 'beratung',
    bgImage: { uri: 'consultation' },
    btnText: 'BERATUNG UND BEGLEITUNG',
    navigateTo: 'CategorizedPage',
    data: {
      headerTitle: 'Beratung',
      headerImage: 'consultation',
      data: data.advice,
    },
  },
  {
    id: 'spenden',
    bgImage: { uri: 'spenden' },
    btnText: 'SPENDEN',
    navigateTo: 'Browser',
    navigateUrl: url.donateUrl,
  },

];
...

Grid = (props) => {
    const { columns, orientation } = this.state;
    return (
      <FlatList
        data={props}
        key={orientation}
        keyExtractor={(item) => item.id}
        renderItem={({ item }) => (
          <View style={(orientation === 'portrait') ? [
            styles.btn,
            styles.btnFirst] : [landscape.btn, styles.btnFirst]}
          >
            <TouchableOpacity
              style={[styles.touchBtn, styles[item.id]]}
              onPress={() => this.pressImageButton(item)}
            >
              <ImageBackground
                resizeMode="cover"
                style={styles.imageBackground}
                source={item.bgImage}
              >
                <View style={styles.imageOverlay} />
                <Text style={[styles.btnText]}>
                  {item.btnText}
                </Text>
              </ImageBackground>
            </TouchableOpacity>
          </View>
        )}
        numColumns={columns}
      />
    );
  };

...

        <View style={(orientation === 'portrait')
          ? styles.btnContainer
          : landscape.btnContainer}
        >
          {this.Grid(getHomeButtonList(data, dropdowns))}
        </View>

【问题讨论】:

    标签: react-native imagebackground


    【解决方案1】:

    我发现问题的原因是shrinkResources true in build.gradle

    ProGuard 无法识别我需要这些文件。 要检查删除了哪些资源,如果您启用了shrinkResources true,请检查您的&lt;build-directory&gt;/build/outputs/mapping/release/resources.txt

    就我而言,有如下条目:

    Skipped unused resource res/drawable/consultation.jpg: 55893 bytes (replaced with small dummy file of size 0 bytes)
    Skipped unused resource res/drawable/angebote.jpg: 71099 bytes (replaced with small dummy file of size 0 bytes)
    Skipped unused resource res/drawable/nebenwirkungen.jpg: 57250 bytes (replaced with small dummy file of size 0 bytes)
    Skipped unused resource res/drawable/redbox_top_border_background.xml: -1 bytes (replaced with small dummy file of size 104 bytes)
    Skipped unused resource res/drawable/signet_sachsen.gif: 1925 bytes (replaced with small dummy file of size 0 bytes)
    Skipped unused resource res/drawable/spenden.jpg: 68376 bytes (replaced with small dummy file of size 0 bytes)
    
    

    如何解决

    创建一个名为keep.xml 的文件并将其放在目录/android/app/src/main/res/raw 中。 (如果目录raw 不存在,则创建它。

    把这个放到文件里:

    <?xml version="1.0" encoding="utf-8"?>
    <resources xmlns:tools="http://schemas.android.com/tools"
               tools:keep="@drawable/spenden,@drawable/angebote" />
    

    重要提示:不要这样写文件类型:@drawable/spenden.jpg。这不会像预期的那样工作。但是您可以对文件名使用通配符,如下所示:@drawable/img_ 作为一种解决方法,可以通过一个命令保留所有图像。

    信息来源:

    Troubleshooting shringResources

    Customize which resources to keep

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-02
      • 1970-01-01
      • 1970-01-01
      • 2016-05-17
      • 1970-01-01
      相关资源
      最近更新 更多