Is there a way to log filenames in release build using timber for android?

1.5k views Asked by At

I just started using timber for logging on android, we are trying to add filenames/class-names to log message. I was able to add filenames in debug build using debug tree, but I'm not finding a way to add filenames in the release tree. Is there a possibility to add the filename in the release tree with R8 enabled?

2

There are 2 answers

2
ymindstorm On

As described here there are several ways to go about this.

For release builds

When you generate an actual release build of the app that you intend to publish on the Play Store you also get a mapping.txt for every release artifact. Upload this file (be careful - every build generates a new file, so you must use the exact file that was generated with your artifact) and Google will deobfuscate crash logs in the Google Play Console for you.

You also have the option to use the retrace command line tool to translate logs you receive from this build yourself using mapping.txt.

For non-release builds/internal testing

Since you will not be releasing this artifact to the world it is possibly acceptable to break your own code obfuscation. You can use rules in the proguard-rules.pro file to keep relevant information during R8 shrinking.

Check the docs for all relevant parameters. Especially useful are -dontobfuscate, -keeppackagenames and -keepattributes (with params like SourceFile and LineNumberTable).

Remember that changes in shrinking and obfuscating code can break thinks like reflection and resource lookup when you do these manually. So make sure your obfuscated release build works as expected after fiddling with these parameters.

1
Halil Sahin On

Okay use it like this: Define a TAG variable with your class name in your whole classes as:

private static final String TAG = "Your class name";

then pass your TAG:

Timber.tag(TAG).d("Error %s!", error);