react-native - 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>
解决方案
我发现问题的原因shrinkResources true
在build.gradle
.
ProGuard 不承认我需要这些文件。要检查删除了哪些资源,如果您已shrinkResources true
启用,请检查您的<build-directory>/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_作为一种解决方法,可以通过一个命令保留所有图像。
信息来源:
推荐阅读
- cakephp - 数组控制器 cakephp 上的分页
- excel - 在公式中查找和替换文件名并重新计算公式
- javascript - 在 React 应用程序中渲染外部网页
- python - 使用 functools lru_cache,如何确定函数是否返回了缓存数据?
- typescript - 在 PhpStorm 项目中使用带有 Typescript 的 window.openDatabase
- kendo-ui - Kendo UI Grid ServerBound ClientTemplate 替代条件语句
- sql - 使用条件选择结果与另一个表连接
- javascript - 如何修改 ES6 类的构造函数
- json - 如何使用 Python 解析 Spark 1.6 中包含空格、额外双引号和反斜杠的格式不正确的 JSON 字符串?
- python - 如何在 tkinter 中等待多次按键?