4

I have a class with static methods for logging, eg:

I'm attempting to remove all these using ProGuard. I don't want to just disable them (That done using logic anyway), I want them to disappear when decompiled.

It is successfully removing the calls to my logging methods, but leaves behind the strings that were concatenated. It completely removes the strings that were fixed.

This is my ProGuard setting:

-assumenosideeffects class com.mypackage.Log { *; }

This is my source:

void doSomething(String name) {
    Log.verbose("Hello!");
    Log.verbose("My name is " + name);
    ...
}

This is what it decompiles to:

void a(String a) {
    new StringBuilder("My name is ").append(a);
    ...
}

This is what I want it to decompile to:

void a(String a) {
    ...
}

For this example, assume that the name parameter is used later on in the method (ie, it shouldn't be optimized out completely).

Should I also be using assumenosideeffects for StringBuilder? I do use StringBuilders elsewhere, that I do want to keep. Would that remove them there, too?

Richard Taylor
  • 2,702
  • 2
  • 22
  • 44
  • What logging framework are you using? With log4j you would write Log.verbose("My name is #0" , name); instead of Log.verbose("My name is " + name);, the concatenation would occur only if necessary, i.e. if loglevel>= verbose. – StephaneM Mar 28 '14 at 13:32
  • possible duplicate of [Removing unused strings during ProGuard optimisation](http://stackoverflow.com/questions/6009078/removing-unused-strings-during-proguard-optimisation) – sschuberth Aug 01 '14 at 10:37
  • ProGuard docs now have samples of removing unused StringBuilders with `-assumenoexternalsideeffects`. – Miha_x64 Apr 25 '18 at 16:24

1 Answers1

2

It's good that you checked if the results were as expected. In this case, ProGuard doesn't know that the StringBuilder operations don't have any net effect, so it leaves them in. You can find a fairly complete discussion in the question Removing unused strings during ProGuard optimisation.

Recent versions of ProGuard have a few heuristic techniques that may solve simple cases like these. ProGuard's commercial extension DexGuard implements a more advanced technique called escape analysis; it can remove the invocations as a result.

(I am the developer of ProGuard and DexGuard)

Community
  • 1
  • 1
Eric Lafortune
  • 45,150
  • 8
  • 114
  • 106
  • Are there any consequences to the above case? I mean if parameter "name" is not modified within the line including "Log...", does removing the reference in that line make any difference even if you use that variable later? I guess not in this simple case. – Kerem Dec 21 '14 at 05:40