Android:如何告诉 ShrinkResources 保留某些资源 [英] Android: How to tell ShrinkResources to keep certain resources
问题描述
我的 res/raw 文件夹中有一个 mp3 和一个 ogg 文件,用于 Android 媒体播放器.我像这样使用了 setDataSource
I have an mp3 and an ogg file in my res/raw folder which I used with Android Media Player. I used setDataSource like so
musicPlayer.setDataSource(context, Uri.parse("android.resource://com.me.myapp/" + R.raw.music));
这在从 Android Studio 运行时工作正常,但是,当导出带有 shrinkResouces 启用,它会去除 mp3(如上所述,我也有一个 ogg 文件,我以同样的方式使用它,但也会被去除).以下是 Resouces.txt 文件中的相关结果:
This works fine when running from Android Studio, however, when exporting a signed APK with shrinkResouces enabled, it strips out the mp3 (as mentioned above, I also have an ogg file which I use in the same way and which also gets stripped out). Here is the associated results in the Resouces.txt file:
跳过未使用的资源 res/raw/music.mp3:485531 字节(替换为大小为 0 字节的小型虚拟文件)
Skipped unused resource res/raw/music.mp3: 485531 bytes (replaced with small dummy file of size 0 bytes)
跳过未使用的资源 res/raw/oggmusic.ogg:5335764 字节(替换为大小为 0 字节的小型虚拟文件)
Skipped unused resource res/raw/oggmusic.ogg: 5335764 bytes (replaced with small dummy file of size 0 bytes)
我已经做了一些研究,Google 问题跟踪器上有关于这个问题的错误报告,但是发帖人说他/她使用了上面链接中包含的信息来确保所需的资源没有被删除,并且问题已被标记为预期行为"(我觉得这有点奇怪,因为我不认为这是删除所需资源的预期行为).
I've done some research and there is a bug report on the Google Issue Tracker for this problem, however the poster says he/she used the information contained in the above link to make sure the required resources were not removed and the issue has been marked as 'intended behaviour' (which I find a little odd as I wouldn't have thought it would be intended behaviour for required resources to be removed).
无论如何,我已经尝试按照文档中的说明进行操作,但似乎不起作用,我在这里做错了什么?我创建了一个名为keep.xml"的 xml 文件,并将它放在我的原始文件夹中,与要保留的资源一起.
Anyway, I've tried to do as mentioned in the docs and it doesn't seem to work, what am I doing wrong here? I've created an xml file called 'keep.xml' and placed it into my raw folder alongside the resources to keep.
这是 XML:
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools"
tools:keep="@raw/*.mp3, @raw/*.ogg"/>
这个问题在 Eclipse 中并不明显,但在迁移到 Android Studio 后就出现了.
This issue wasn't evident in Eclipse, but has appeared since migrating to Android Studio.
编辑
我做了更多的测试,我可以确认我在 XML 文件中指定的任何内容都被完全忽略了.如果我使用丢弃"例如,代替或代替 'keep','discard' 中声明的资源出现在生成的 APK 文件中,所以我一定是做错了什么或在某处遗漏了一步.
I did a bit more testing and I can confirm that whatever I specify in the XML file is being completely ignored. If I use "discard" for example instead of, or in place of 'keep', the resources stated in 'discard' appear in the generated APK file, so I must be doing something wrong or missing a step out somewhere.
推荐答案
我刚刚遇到了这个问题,并发现了我做错了什么.
I just experienced this issue and have found out what I was doing wrong.
引用资源时不要指定文件扩展名.
Do not specify file extensions when referring to resources.
我最初所做的(这行不通):
What I did at first (this will not work):
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools"
tools:keep="@raw/vid.mp4"
/>
解决方案,删除文件扩展名:
Solution, drop the file extension:
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools"
tools:keep="@raw/vid"
/>
您可以通过检查可以在 ./app/build/outputs/mapping/<buildType>/resources.txt
中找到的 resources.txt 文件来验证这一点https://developer.android.com/studio/build/shrink-code
You can verify this by inspecting the resources.txt file that you can find at ./app/build/outputs/mapping/<buildType>/resources.txt
https://developer.android.com/studio/build/shrink-code
这篇关于Android:如何告诉 ShrinkResources 保留某些资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!