Skip to content

ProGuard

Donn Felker edited this page Oct 12, 2012 · 1 revision

Using proguard to reduce your application's footprint

Introduction

Storage space on android devices can be heavily constrained, particularly for users of the older generation of devices. The guice library isn't small, but we can reduce its footprint by using proguard to remove unnecessary classes and methods.

Proguard Configuration

Make sure you're using the SDK tools release 8 or later and visit http://developer.android.com/intl/fr/guide/developing/tools/proguard.html. Then use a configuration like the following::

-target 1.6
-dontobfuscate
-dontoptimize
-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses 
-dontpreverify 
-verbose 
-dump ../bin/class_files.txt
-printseeds ../bin/seeds.txt
-printusage ../bin/unused.txt
-printmapping ../bin/mapping.txt 

# The -optimizations option disables some arithmetic simplifications that Dalvik 1.0 and 1.5 can't handle. 
-optimizations !code/simplification/arithmetic 

-keep public class * extends android.app.Activity 
-keep public class * extends android.app.Application 
-keep public class * extends android.app.Service 
-keep public class * extends android.content.BroadcastReceiver 
-keep public class * extends android.content.ContentProvider
-keep class com.google.inject.Binder
-keepclassmembers class * {
    @com.google.inject.Inject <init>(...);
}
# There's no way to keep all @Observes methods, so use the On*Event convention to identify event handlers
-keepclassmembers class * { 
    void *(**On*Event); 
}
-keep public class * extends android.view.View {
    public <init>(android.content.Context);
    public <init>(android.content.Context, android.util.AttributeSet);
    public <init>(android.content.Context, android.util.AttributeSet, int);
    public void set*(...);
} 
-keep public class roboguice.**

Troubleshooting

If proguard is erroneously stripping out any classes you need, chances are you'll need to add some additional -keep directives to your config.txt. Consult the proguard manual for more details.

Further links

Look at this blog post on how to make obfuscation to work and some more tips.

Clone this wiki locally