防止 Proguard 删除特定的可绘制对象
在我的 Android 项目中,我在 res/drawable/ 中存储了一些图像,这些图像只能从 Web 视图中加载的 HTML 文件进行访问。例如(HTML 中的代码):
<img src="file:///android_res/drawable/myfriend.png">
这些图像在优化期间被 Proguard 从 apk 中删除。
有人知道保留这些文件的方法吗(即使它们没有直接在 java 代码中使用)?
In my Android project, I have some images stored in res/drawable/ which are accessed only from an HTML file loaded in a Webview. For example (code in HTML):
<img src="file:///android_res/drawable/myfriend.png">
These images are removed by Proguard from apk during optimization.
Does somebody know a way to keep these files (even if they are not used directly in the java code)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我有一个类似的问题,只想添加一些内容。
ProGuard 不会剥夺资源。
如果您只是解压缩您的 apk,您将看到图像文件仍然存在。
问题是 Webkit FileLoader 将尝试使用反射加载您的 R$drawable 类。如果您没有在 proguard.cfg 文件中添加任何 keep 规则,该类将被重命名,因此 Webkit 将无法加载您的资源。
通过将文件放入资产文件夹中,您将绕过 R 类系统,一切都会正常工作。
但这不是一个解决方案,例如,如果您想针对不同的密度使用不同的资源。
我建议您只需添加一个非常基本的保留规则来保留 R 内部类和字段:
I had a similar issue and wanted to add just a few bits.
The resources ARE NOT stripped away by ProGuard.
If you simply unzip your apk you will see that the image files are still there.
The problem is that the Webkit FileLoader will try and load your R$drawable class using reflection. If you do not add any keep rule to your proguard.cfg file that class will be renamed, hence Webkit will not be able to load your resource.
By placing the file into the assets folder your are bypassing the R class system and everything will work.
This is not a solution though if, for example, you want to use different resources for different densities.
I suggest you simply add a very basic keep rule to preserve R inner classes and fields:
我将图像移至“资产”文件夹,这解决了我的问题:
I moved the images to the 'assets' folder which solved my problem:
对于本地化的简单 html(没有链接的本地化本地文件/图像)
对原始文件夹使用此 Proguard 规则:
WebView 的示例 URL:
文件夹结构示例:
For localized simple htmls (no linked localized local files/images)
Use this Proguard rule for raw folder:
Example Url for WebView:
Example folder structure: