Skip to content
Advertisement

How to get method name in android (java)

I want to retrieve the method name on an android app for logging purposes but can’t seem to get it. I’ve already tried doing what was suggested in other answers, such as:

Thread.currentThread().getStackTrace();

and

new Object(){}.getClass().getEnclosingMethod().getName();

To no avail, the first returns me a stack with something like:

getThreadStackTrace
getStackTrace
a
b
a
run
mainLoop
run

The second returns “a” as the method name.

Does anyone know another way to get it or why this is happening?

Advertisement

Answer

These a and b method names appear when code obfuscation is enabled.

In Android, Proguard or R8 does this.

After R8 obfuscates your code, understanding a stack trace is difficult (if not impossible) because names of classes and methods might have been changed. Besides renaming, R8 can also change the line numbers present in the stack traces to achieve additional size savings when writing the DEX files.

If we need to deobfuscate the code, we have to use mapping.txt generated during the build.

Fortunately, R8 creates a mapping.txt file each time it runs, which contains the obfuscated class, method, and field names mapped to the original names. This mapping file also contains information to map the line numbers back to the original source file line numbers. R8 saves the file in the /build/outputs/mapping// directory.

More details on deobfuscation

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement