Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 519
Peterdk Avatar asked May 01 '11 13:05

Peterdk


People also ask

How do you keep a class in ProGuard?

-keepclassmembernames. This is the most permissive keep directive; it lets ProGuard do almost all of its work. Unused classes are removed, the remaining classes are renamed, unused members of those classes are removed, but then the remaining members keep their original names.

What does ProGuard keep do?

-keep Specifies classes and class members (fields and methods) to be preserved as entry points to your code. For example, in order to keep an application, you can specify the main class along with its main method. In order to process a library, you should specify all publicly accessible elements.

Does ProGuard obfuscate package name?

Obfuscating package names myapplication. MyMain is the main application class that is kept by the configuration. All other class names can be obfuscated. Note that not all levels of obfuscation of package names may be acceptable for all code.


1 Answers

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

like image 62
Meriam Avatar answered Oct 24 '22 17:10

Meriam