如何强制 Proguard 保留我的 .xml 资源文件?
我成功地在我的 Android 应用程序中使用了 proguard。
然而,使用一个应用程序我遇到了麻烦。
此应用程序使用一个 Java 库,该库具有存储在包中的 .xml
文件。
InputStream istream = Library.class.getResourceAsStream("resource.xml");
当 proguard 被禁用时,这个库可以很好地工作。然而,运行 proguard 时,似乎 xml 文件被完全剥离了。
相关 proguard.cfg
-optimizationpasses 5
-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-dontpreverify
#-dontobfuscate
#-repackageclasses '' //THIS IS DISABLED
-keepattributes *Annotation*
-keepattributes Signature
-verbose
-dontwarn roboguice.activity.RoboMapActivity
-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*
关于如何强制保留此 xml 文件有什么想法吗?
I am succesfully using proguard for my Android apps.
However, with one app I am having trouble.
This app uses a java library that has a .xml
file that is stored in the package.
InputStream istream = Library.class.getResourceAsStream("resource.xml");
This library works great when proguard is disabled. However, running proguard, it seems that the xml file is just completely stripped away.
Relevant proguard.cfg
-optimizationpasses 5
-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-dontpreverify
#-dontobfuscate
#-repackageclasses '' //THIS IS DISABLED
-keepattributes *Annotation*
-keepattributes Signature
-verbose
-dontwarn roboguice.activity.RoboMapActivity
-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*
Any ideas on how to force keep this xml file?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该在 res/raw 下添加一个新文件 keep.xml。
在tools:keep中,您列出了要保留的布局。如果您在代码中使用反射来获取可绘制对象,您甚至可以保留特定的可绘制对象。
我更喜欢添加
tools:shrinkMode="strict"
,这样我可以确保不会消除任何资源,除非它不用于当然。您可能想查看此链接缩小代码。不要忘记将这些行添加到您的 proguard 规则中:
-keepattributes InnerClasses -keep class **.R -keep class **.R$* {;; }
You shoud add a new file keep.xml under res/raw.
In tools:keep, you list the layouts you want to keep. You can even keep specefic drawables if you use reflection in your code to get your drawable
I prefer to add
tools:shrinkMode="strict"
so that I make sure no resource is eliminated unless it is not used for sure. You may want to have a look at this link Shrink your code.Don't Forget to add these lines to your proguard rules:
-keepattributes InnerClasses -keep class **.R -keep class **.R$* { <fields>; }
首先,事实证明,使用ant和我的build.xml脚本根本没有处理.xml资源文件。您需要手动向 build.xml 添加复制操作,以便将资源文件复制到输出目录。
尽管如此,解决了这个问题后,proguard 还是搞乱了我的工作。解决方案是:
这确保可以通过调用
Library.class.getResource
找到 .xml 文件。First of all, It turned out that using ant and my build.xml script didn't process the .xml resource file at all. You'll need to manually add a copy action to the build.xml for the resource files to be copied to the output dir.
Still, having solved that, proguard messed up my work. Solution was:
This made sure that the .xml file could be found by calling the
Library.class.getResource
.您应该将文件存储在适当的
/res/xml
、/res/assets
或/res/raw
文件夹中,并通过资源访问它们相应的管理系统或资产管理人。您可以在 Android 开发人员指南中了解有关提供资源的更多信息。You should store files in the appropriate
/res/xml
,/res/assets
, or/res/raw
folder and access them through the resource management system or the asset manager correspondingly. You can read more about providing resources in Android Developer's Guide.