How to get Proguard to save a .xml resource file?

I have been using proguard successfully for my Android apps.

However, I have problems with one application.

This application uses the Java library with the .xml file that is stored in the package.

 InputStream istream = Library.class.getResourceAsStream("resource.xml"); 

This library works great when proguard is disabled. However, by running proguard, it seems that the xml file is simply completely deleted.

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 get this XML file saved?

+12
source share
3 answers

First of all, it turned out that using ant and my build.xml script did not process the .xml resource file at all. You will need to manually add the copy action to the build.xml file for the resource files that will be copied to the output directory.

However, having decided this, proguard ruined my work. The solution was:

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

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

+9
source

You must add a new keep.xml file in 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 the tools: save, you list the layouts that you want to save. You can even save special hand-drawn objects if you use reflection in your code to get your

 tools:keep="@drawable/ico_android_home_infosperso" 

I prefer to add tools:shrinkMode="strict" so that I don't exclude anything unless it is used for sure. You might want to take a look at this link. Shorten the code . Remember to add these lines to your Proguard rules:

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

+6
source

You must store the files in the appropriate folder /res/xml , /res/assets or /res/raw and access them through the resource management system or asset manager, respectively. For more information on providing resources, see the Android Developers Guide .

+1
source

All Articles