[JAVA] Identify obfuscated method names

Obfuscating and running Android apps can result in exceptions. Even if you look at the stack trace, you can't tell which method caused the exception due to obfuscation. In such a case, it is a method to identify the method in which the exception occurred.

Obfuscation

On Android, the following settings may be made for security and size reduction.

build.gradle


minifyEnabled true
shrinkResources true

If you try to run it with this setting, you may get an exception.

Exceptions occur due to obfuscation

Suppose you have the following implementation:

Calc.java


package com.example.myapplication;

public class Calc {
    public int divide(int param1, int param2) {
        return param1 / param2;
    }
}

MainActivity.java


package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Calc calc = new Calc();
        int result = calc.divide(10, 0);
        System.out.println(result);
    }
}

If you do this, you will get an exception for division by zero.

Stack trace


Caused by: java.lang.ArithmeticException: divide by zero
    at b.a.a.a.a(:5)
    at com.example.myapplication.MainActivity.onCreate(:13)
    at android.app.Activity.performCreate(Activity.java:7009)
    at android.app.Activity.performCreate(Activity.java:7000)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)

Due to obfuscation, the method name where the exception occurred becomes b.a.a.a.a </ code>, and I do not know where the exception occurred.

Identify methods from obfuscated strings

There is a file where you can see the mapping before and after obfuscation. Go to your Android project folder and search for "mapping" in the search field. "Mapping.txt" is displayed in the search results. Open this file. 1.png

The method name b.a.a.a.a </ code> in which the exception occurred is represented by the package name, class name, and method name. Search for mapping.txt by package name and class name ( b.a.a.a </ code>). The corresponding part will be hit below.

mapping.txt


com.example.myapplication.Calc -> b.a.a.a:
    3:3:void <init>() -> <init>
    5:5:int divide(int,int) -> a

If you look at the mapping, you can see that the method name ( a </ code>) where the exception occurred is divide </ code>.