2

I have a situation where i have an instance variable TAG which is nothing but my class name which i fetch using method MainActivity.class.getsimpleName().

Eg. public static final String TAG=MainActivity.class.getsimpleName();

I am using this TAG variable for my google analytics tracking. As a matter of fact because of pro guard my variable is being obfuscated and i get some useless string as my class name for tracking, thus analysis is not possible and i have 100's of classes to hard code all the TAG variable's of each class (which i will as my last resort).

i also want to obfuscate the code to maintain some safety for my app. Is there a way i can only exclude TAG variable from being obfuscated or any way i can get my analysis meaningful.

How can i solve this problem efficently?

farhan patel
  • 1,490
  • 2
  • 19
  • 30

2 Answers2

3

public static final String TAG = "MainActivity";

And if you want to change the value of TAG, let AndroidStudio refactor it for you.

That's the only possibility since TAG is not obfuscated. This is your TAG owner that is obfuscated.

If I am right, your Activity are fine with this (because AndroidManifest require the exact syntax). But your Fragments are. That is still less value to refactor.

Try the replaceAll AndroidStudio function with regex like to save your time, like:

find : TAG = (.+).class.getSimpleName\(\); replace :TAG = "$1";

Täg
  • 431
  • 4
  • 17
  • i really like you're solution but how can i change TAG variable from different classes to their particular classname.i.e `MainActivity`,`SplashActivity` etc. – farhan patel Jul 28 '16 at 05:25
  • With the regex I suggest, you capture the class name in the `(.+)` and use it with `$1`. Ctrl + Shift + R (or cmd + shift + R for mac) to manage a replaceAll (don't forget to cross the `Regular expression` checkbox). If you want to auto generate the new classes with a TAG already written, juste edit the template of the Class. On mac : Preferences > Editor > File and Code Templates >> Class => Add ` public static final TAG = "${NAME}";` in the class block. – Täg Jul 28 '16 at 08:55
  • If the row is formatted with blank space, this reg-ex might come in handy to keep formatting: find: `TAG( *) = (.+).class.getSimpleName\(\);` replace: `TAG$1 = "$2";` – jayeffkay Jan 31 '17 at 15:39
0

As a downside, such a solution largely nullifies the size/obfuscation improvements, since the code now contains the original class names again, albeit indirectly.

Eric Lafortune
  • 45,150
  • 8
  • 114
  • 106