Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Java compiler opimization

Tags:

android

I have code like this:

MyLog.d("TAG", "debug string " + aVariable + " more debug string =" + anotherVariable);

And MyLog class is like

public void d(String tag, String message) {
    private static final boolean DEBUG = true;
    if (DEBUG) {
        Log.d(tag, message);
    }
}

My question is if I set DEBUG to false, will android java compiler smartly detect that this line of code

MyLog.d("TAG", "debug string " + aVariable + " more debug string =" + anotherVariable);

does nothing

and it won't create temporary string objects for "debug string " + aVariable + " more debug string =" + anotherVariable

like image 857
michael Avatar asked Apr 08 '26 09:04

michael


1 Answers

You're doing the string concatenation before anything related to the DEBUG matters: I doubt that would be optimized out by ProGuard, although the call to Log.d inside MyLog.d would disappear.

If you check the bytecode, it'd be worth reporting back; I'm curious how far ProGuard will follow a call chain to detect dead code. I'd be surprised if the string concatenation went away.

You also can't declare a variable private like that inside a method.

like image 123
Dave Newton Avatar answered Apr 09 '26 22:04

Dave Newton