How to programatically determine if an Android application is obfuscated?
I have and Android app where vari开发者_运维问答ous people test both development builds and release builds. The development builds are not obfuscated and I would like to be able to programatically determine at runtime if the application has been obfuscated or not.
Is there a way to do this?
Here's one idea: add a class to your code base that is not used at all. Proguard will obfuscate and/or remove it. So, loading it via reflection in the app ought to cause ClassNotFoundException
if it's been run through ProGuard.
As @Sean proposed use a class which has no (external) dependencies.
But beware, ProGuard can detect the use of reflection, so you must somehow load class from string name, by not using a string literal (text resource maybe?): http://proguard.sourceforge.net/index.html#/FAQ.html%23forname
Select a class which is always renamed by Proguard after obfuscation. Its name is ExampleClass.java
in the code below. Check its name in runtime with the following line:
ExampleClass.java
...
public static final boolean OBFUSCATED = !ExampleClass.class.toString().contains("ExampleClass");
That's all. No helper class or method is required and works without causing exceptions.
精彩评论