简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM