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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5848761/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 13:06:05  来源:igfitidea点击:

How can I force Proguard to keep my .xml resource file?

javaandroidembedded-resourceproguard

提问by Peterdk

I am succesfully using proguard for my Android apps.

我成功地为我的 Android 应用程序使用了 proguard。

However, with one app I am having trouble.

但是,使用一个应用程序时我遇到了麻烦。

This app uses a java library that has a .xmlfile that is stored in the package.

此应用程序使用一个 Java 库,该库包含一个.xml存储在包中的文件。

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.

当 proguard 被禁用时,这个库工作得很好。但是,运行 proguard 时,似乎 xml 文件被完全剥离了。

Relevant proguard.cfg

相关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?

关于如何强制保留此 xml 文件的任何想法?

采纳答案by Peterdk

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.

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

Still, having solved that, proguard messed up my work. Solution was:

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

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

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

这确保可以通过调用 .xml 文件找到 .xml 文件Library.class.getResource

回答by Meriam

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

您应该在 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" />

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 中,您列出要保留的布局。如果您在代码中使用反射来获取可绘制对象,您甚至可以保留特定的可绘制对象

tools:keep="@drawable/ico_android_home_infosperso"

I prefer to add tools:shrinkMode="strict"so that i make sur 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:

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

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

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

回答by Malcolm

You should store files in the appropriate /res/xml, /res/assets, or /res/rawfolder 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.

您应该将文件存储在适当的/res/xml/res/assets/res/raw文件夹中,并通过相应的资源管理系统或资产管理器访问它们。您可以在Android 开发人员指南 中阅读有关提供资源的更多信息。