Is there a way to detect (during runtime) if your application is ProGuarded? My goal here is to set a boolean value based on whether the application is ProGuarded or not.
2 Answers
I think it'd be easier if you set a boolean on build variants/flavors that you run ProGuard on.
For example: if you run ProGuard on Release and Staging but not on Debug, set a boolean variable to true on release and staging, but false on debug. This may help

- 1
- 1

- 15,672
- 28
- 94
- 206
-
I like this answer over the reflection one – Petro Jun 02 '15 at 18:09
-
agreed this is of course a better approach..to just set a variable. but, to be fair, he asked about runtime. setting a buildConfigField will define a final field at compile time. – Sam Dozor Jun 02 '15 at 18:13
-
Thank you, while Sam Dozor did answer directly what I asked, this is the better option. I appreciate it. – Hulabaloo Jun 02 '15 at 18:14
This is a bit of a workaround but there are a few avenues by which reflection will solve this.
One way would be for you to first determine a class that is obfuscated/minified in the case of proguard. So say you have a class com.package.Thing
that when proguard is enabled gets minified to be called just com.package.a
(or whatever, it doesn't matter). Then use reflection to see if the original class exists.
Something like this:
boolean isProguardEnabled() {
try{
return Class.forName("com.package.Thing") == null;
catch(ClassNotFoundException cnfe) {
return true;
}
}
Obviously this only works if you know of a class that will be changed when proguard is enabled.
Perhaps more generically, you could look for a class com.package.a
and use that as a signal that proguard is enabled, assuming of course that you don't genuinely have a class by the name of a
. By default proguard will just use the alphabet to come up with class names, but you can also specify in your proguard configuration your own list of class names to use (which presents another way to do this same trick, look for one of those class names that you've specified).

- 40,335
- 6
- 42
- 42
-
Thank you for helping out Sam. I will definitely use this as a secondary option. This method did not occur to me at all. :) – Hulabaloo Jun 02 '15 at 18:16