Proguard obfuscation

1,140 views
Skip to first unread message

Manfred Moser (simpligility)

unread,
Nov 15, 2010, 4:42:47 PM11/15/10
to roboguice
Hi!

I am starting to use proguard at the moment but I am not having any
luck with obfuscating. I can run proguard on my app just fine with
obfuscation disabled but when I enable it the app wont start..

Does anybody have a proguard setup for a roboguice app working where
the app is obfuscated as well?

manfred

Michael Burton

unread,
Nov 15, 2010, 4:46:55 PM11/15/10
to robo...@googlegroups.com
No, sorry, I've only experimented with using it for size reduction.  Have you seen http://android-developers.blogspot.com/2010/09/proguard-android-and-licensing-server.html ?

I imagine the problem is that proguard is optimizing out some of the classes that are instantiated with reflection.  Here's the proguard config I was using when I last looked at this several months ago.  It wasn't well tested so I have no idea how complete it is:

-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>(...);
}
-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.**


-keep public class com.groupon.inject.JsonConverter


Hope that helps a little.

Cheers,
Mike




--
You received this message because you are subscribed to the Google Groups "roboguice" group.
To post to this group, send email to robo...@googlegroups.com.
To unsubscribe from this group, send email to roboguice+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/roboguice?hl=en.


Manfred Moser

unread,
Nov 15, 2010, 4:49:47 PM11/15/10
to robo...@googlegroups.com
Yeah.. I have seen this on the wiki and also migrated it to maven.
From my understanding of proguard it will remove the annotations and
that is a little bit of a problem. So I added -keepattributes *Inject*
but I am still seeing app startup problems.

I will ask around on the proguard forum as well..

manfred

velo

unread,
Dec 5, 2010, 11:14:20 AM12/5/10
to roboguice

Michael Burton

unread,
Dec 5, 2010, 2:30:10 PM12/5/10
to robo...@googlegroups.com
Thanks Velo!! Just to clarify, velo's contribution is for reducing the file size of the app, not for obfuscation.
--
Michael Burton
http://about.me/michaelburton

Manfred Moser

unread,
Dec 6, 2010, 1:37:47 AM12/6/10
to robo...@googlegroups.com
I got obfuscation worked out and plan to blog about it very shortly
(tonight if I dont fall asleep before..)

manfred

Michael Burton

unread,
Dec 6, 2010, 11:33:01 AM12/6/10
to robo...@googlegroups.com
Interestingly, r8 tools released today include proguard built-in.

Manfred Moser

unread,
Dec 6, 2010, 1:34:18 PM12/6/10
to robo...@googlegroups.com
Yeah. That was announced long ago. But you still gotta provide the
configuration and thats where the crux lies..

manfred

Matt Hawley

unread,
Mar 8, 2011, 2:20:19 AM3/8/11
to robo...@googlegroups.com
Manfred - I was just wondering if you ever blogged the work relating to obfuscation with proguard?

Mike Burton

unread,
Mar 8, 2011, 10:26:57 AM3/8/11
to robo...@googlegroups.com, robo...@googlegroups.com
Adam you recently went through this too. Tips?

Sent from my iPhone

On Mar 8, 2011, at 2:20 AM, Matt Hawley <ma...@eworldui.net> wrote:

Manfred - I was just wondering if you ever blogged the work relating to obfuscation with proguard?

--

Manfred Moser

unread,
Mar 8, 2011, 11:33:26 AM3/8/11
to robo...@googlegroups.com
Yes. There are two very helpful blog posts I wrote on my site http://www.simpligility.com. There are links to it on the roboguice proguard wiki page.. 

Adam Tybor

unread,
Mar 8, 2011, 5:31:59 PM3/8/11
to robo...@googlegroups.com
I didn't get obfuscation working, but I got shrinking and optimizing working. I am pleased, it shaved about ~75k off my final apk, and while I haven't benched it, it does feel a little snappier.  I was planning on writing something up once I had a chance to consolidate my rules.  Right now my proguard.cfg is a mess because I just kept adding rules until it passed QA.

I wasn't able to find documentation about it, but it seems using * extends {Class} notation doesn't preserve classes that do not directly inherit from {Class}.  So for me, things like 
-keep * extends Activity
-keep * extends View were useless.  

The other weird one is Context Events, I can't figure out the specification on how to preserve methods that have a specific annotation on a method argument.  I ended up using tracking down all the namespaces of events and using those.

I am hoping to find better rules to trim the guice.jar and roboguice.jar down more.

Adam


Matt Hawley

unread,
Mar 9, 2011, 12:07:49 PM3/9/11
to roboguice
I worked out the details with optimization / obfuscation in my
scenario. Here's my proguard.cfg file https://gist.github.com/862555

Matt

On Mar 8, 8:33 am, Manfred Moser <mosa...@gmail.com> wrote:
> Yes. There are two very helpful blog posts I wrote on my sitehttp://www.simpligility.com. There are links to it on the roboguice proguard
> wiki page..
>
>
>
>
>
>
>
> On Tue, Mar 8, 2011 at 7:26 AM, Mike Burton <m...@niskala.org> wrote:
> > Adam you recently went through this too. Tips?
>
> > Sent from my iPhone
>

Adam Tybor

unread,
Mar 10, 2011, 12:29:15 AM3/10/11
to robo...@googlegroups.com
Wow, that works for you, that is pretty minimal?  Maybe I will have some cycles to give it a good refactoring tomorrow.  Thanks for posting its encouraging to know something so simple works.  I would be embarrassed to post mine after seeing that.

Adam

Matt Hawley

unread,
Mar 19, 2011, 3:30:26 AM3/19/11
to roboguice
BTW, I've updated my proguard.cfg file (https://gist.github.com/
862555) after preparing for a new release and noticing that my event
listeners (both methods and injected listener classes) were being
removed. The following will need to be customized for your
application.

-keepclassmembers class * {
void *(net.eworldui.videouploader.events.*);
}

Matt

Adam Tybor

unread,
Mar 19, 2011, 8:37:35 PM3/19/11
to robo...@googlegroups.com
Yup, I had that to.  I also was losing standard events as well since I usually make the observers protected or private.

Anyone know of a way to save all methods with annotation on a method parameter?  It would be far easier to keep methods with @Observes on a parameter rather then making sure to catch all the possible events by package or class name.

I almost wish we went with the @Observes annotation on the method instead of the parameter, however we were trying follow the Seam event api pattern which I believe is also a JSR.

Adam

John Ericksen

unread,
Mar 19, 2011, 8:57:00 PM3/19/11
to roboguice

Adam Tybor

unread,
Mar 19, 2011, 9:03:53 PM3/19/11
to robo...@googlegroups.com
Nope, not for me.  I actually am keeping all attributes but the shrinking still removes the method because its protected / private and only called via reflection.  I believe -keepattributes really just means keep the annotation and not keep everything associated with the annotation like the class or method which is still subject to being removed by the shrinking process.

Matt Hawley

unread,
Mar 20, 2011, 12:40:28 AM3/20/11
to roboguice
Yeah, I couldn't find in the proguard refcard how to indicate
attributes on parameters, so I don't think it's possible. I agree,
wish it was simpler.

Adam Tybor

unread,
Mar 20, 2011, 12:20:52 PM3/20/11
to robo...@googlegroups.com

Adam Tybor

unread,
Mar 21, 2011, 7:14:25 PM3/21/11
to robo...@googlegroups.com
Just got a reply to my thread... until they add support for this, which is not a high priority, the work around is to use a marker annotation at the method level.

The -keep options currently don't support templates containing parameter annotations. One can easily imagine support though, just like the current class annotations and class member annotations. Adding it is very well possible but not trivial (parameter patterns are currently matched against the descriptor string as a whole, for instance allowing wildcards to match groups of parameters). It's the first time someone is asking for it, so I'm not sure how commonly useful this can be. I'll keep it in mind for now. As a solution, you should try finding different patterns, or maybe introduce an additional annotation for the methods to be preserved. Eric
 

Alec Plumb

unread,
Sep 21, 2011, 9:11:53 PM9/21/11
to robo...@googlegroups.com
I've been able to use this successfully to keep my methods with @Observes (although it depends on the naming pattern of the event objects)


-keepclassmembers class * {
    void *(**On*Event);
}

Michael Burton

unread,
Sep 23, 2011, 1:36:33 PM9/23/11
to robo...@googlegroups.com
--
You received this message because you are subscribed to the Google Groups "roboguice" group.
To view this discussion on the web visit https://groups.google.com/d/msg/roboguice/-/U4jsSy2Ow4YJ.

Matthew Willis

unread,
Oct 13, 2011, 11:49:02 AM10/13/11
to roboguice
Does anyone have a working (with 1.2-SNAPSHOT) proguard.cfg that I
could try out? I can't seem to get my application to load even with a
dummy activity that uses no roboguice features other than inheriting
from RoboFragmentActivity.

I always get a java.lang.NoSuchMethodException for the method
startFinalizer, despite attempts to keep said method around.

Also, does anyone else get warnings on launch saying "Could not load
Finalizer in its own class loader"?

Michael Burton

unread,
Oct 13, 2011, 11:55:04 AM10/13/11
to robo...@googlegroups.com
Does anyone have a working (with 1.2-SNAPSHOT) proguard.cfg that I
could try out? I can't seem to get my application to load even with a
dummy activity that uses no roboguice features other than inheriting
from RoboFragmentActivity.

I always get a java.lang.NoSuchMethodException for the method
startFinalizer, despite attempts to keep said method around.

Sorry no, I haven't been playing around with proguard.  There have been some recent conversations in this forum though from people who have it working.



Also, does anyone else get warnings on launch saying "Could not load
Finalizer in its own class loader"?

Manfred Moser

unread,
Oct 13, 2011, 3:03:59 PM10/13/11
to robo...@googlegroups.com
Great news and congratulations!

I will upgrade now and let you know if I find any problems.

Manfred

--
You received this message because you are subscribed to the Google Groups "roboguice" group.

Matt Hawley

unread,
Oct 31, 2011, 12:34:03 PM10/31/11
to roboguice
I've got ProGuard working with Guice 3.0 and RoboGuice 2.0b2, but it
was a simple hack job because I couldn't get it working any other way
(I don't know the internals of either well enough). So, I added the
following

-keep class com.google.inject.** { *; }
-keep class javax.inject.** { *; }
-keep class javax.annotation.** { *; }
-keep class roboguice.** { *; }

Which basically says don't obfuscate or remove anything from Guice/
RoboGuice or remove the common stuff, like @Nullable or Provider<T>.
Here's my latest Proguard.cfg file for reference.

https://gist.github.com/1327908

Once RoboGuice 2 gets locked down, those with internal knowledge can
help eliminate the full exclusion of RoboGuice / Guice :D

Matt

Matthew Willis

unread,
Oct 31, 2011, 12:58:46 PM10/31/11
to robo...@googlegroups.com
FWIW, I use this in my proguard config:

-keepattributes *Annotation*
-keep public class roboguice.** { *; }

-keep class **.Finalizer
-keepclassmembers class ** { *** startFinalizer( ... ); }

-keepclassmembers class * { 
  void *(**On*Event); 
}

I'm not sure if that ends up being more or less inclusive than what you have.

RoboGuice is pretty small so I didn't worry about optimizing that exclusion rule.

Cheers,
Matthew

--
You received this message because you are subscribed to the Google Groups "roboguice" group.
Reply all
Reply to author
Forward
0 new messages