[Android/Java] Print call stack backtrace using Throwables for code tracing or debugging


  1. Sometimes attaching to a process isn't the ideal way to trace code.
  2. Sometimes you're just unsure whether a function gets called or not.
  3. Throwables come in handy.


Step 1: Declare Throwable and feed it to Log.d(TAG, MSG, Throwable) 

package com.elinlin010.backtracingdemo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

public class BrokenAppActivity extends AppCompatActivity {

    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_broken_app);

        Throwable t = new Throwable();
        t.fillInStackTrace();
        Log.d("010", "", t);
    }
}

Step 2: See it in logcat.


留言