如何强制 Proguard 保留我的 .xml 资源文件?

发布于 2024-11-05 01:41:01 字数 713 浏览 5 评论 0原文

我成功地在我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

撩发小公举 2024-11-12 01:41:01

您应该在 res/raw 下添加一个新文件 keep.xml。

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools"
    tools:keep="@layout/l_used*_c,@layout/l_used_a,@layout/l_used_b*"
    tools:discard="@layout/unused2" />

在tools:keep中,您列出了要保留的布局。如果您在代码中使用反射来获取可绘制对象,您甚至可以保留特定的可绘制对象。

tools:keep="@drawable/ico_android_home_infosperso"

我更喜欢添加 tools:shrinkMode="strict" ,这样我可以确保不会消除任何资源,除非它不用于当然。您可能想查看此链接缩小代码
不要忘记将这些行添加到您的 proguard 规则中:

-keepattributes InnerClasses -keep class **.R -keep class **.R$* {;; }

You shoud add a new file keep.xml under res/raw.

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools"
    tools:keep="@layout/l_used*_c,@layout/l_used_a,@layout/l_used_b*"
    tools:discard="@layout/unused2" />

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

tools:keep="@drawable/ico_android_home_infosperso"

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>; }

╰つ倒转 2024-11-12 01:41:01

首先,事实证明,使用ant和我的build.xml脚本根本没有处理.xml资源文件。您需要手动向 build.xml 添加复制操作,以便将资源文件复制到输出目录。

尽管如此,解决了这个问题后,proguard 还是搞乱了我的工作。解决方案是:

 -keeppackagenames the.package.where.the.file.is.kept

这确保可以通过调用 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:

 -keeppackagenames the.package.where.the.file.is.kept

This made sure that the .xml file could be found by calling the Library.class.getResource.

唔猫 2024-11-12 01:41:01

您应该将文件存储在适当的 /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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文