BlockCanary 接入

我在SDK中集成了BlockCanary,新增加了一个BlockObserver用于后续上报问题堆栈到自己的服务器

public class BlockObserver implements BlockMonitor.BlockObserver{

    @Override
    public void onBlock(BlockInfo blockInfo) {
        report(blockInfo);
    }

    private void report(BlockInfo blockInfo) {
        Log.d("BlockReport", "getFrequentMethods: "
                + blockInfo.getFrequentMethods()
                + "getHeavyMethods: " + blockInfo.getHeavyMethods()
                + "getEnvInfo: " + blockInfo.getEnvInfo()
                + "getTopHeavyMethod: " + blockInfo.getTopHeavyMethod());
        printThreadStackTrace();
    }

    private void printThreadStackTrace(){
        final Thread mainThread = Looper.getMainLooper().getThread();
        Log.d("BlockReport","\nmainThread: " + mainThread.getStackTrace().toString());
        for (StackTraceElement stackTrace : mainThread.getStackTrace()){
            Log.d("BlockReport","\nstackTraceElement: " + stackTrace);
        }
//        for (Map.Entry<Thread, StackTraceElement[]> stackTrace : Thread.getAllStackTraces().entrySet()) {
//            Thread thread = stackTrace.getKey();
//            if (thread!=null) {
//                StackTraceElement[] stack = stackTrace.getValue();
//                for (StackTraceElement stackTraceElement : stack) {
//                    Log.d("BlockReport","\nstackTraceElement: " + stackTraceElement);
//                }
//
//            }
//        }
    }

}

然后我在SDK初始化的时候注册自己的observer

public void start() {
    ...
    BlockMonitor.registerBlockObserver(new BlockObserver());
    BlockCanaryEx.install(new Config(context));
    ...    
}

卡顿测试

我在demoApp中新增一个 2s 的sleep

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ...
        Button gcButton = (Button) findViewById(R.id.gc_button);
        gcButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                doGc();
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                Log.d("BlockReport", "onClick: Sleep->1000");
                Toast.makeText(MainActivity.this,"GC Finished",Toast.LENGTH_SHORT).show();
            }
        });
    }

但是为啥没有堆栈!
这个是我debug时的blockInfo对象

下面这个则是blockCanaryUI堆栈信息显示

发现两者都没有拿到堆栈信息!有谁知道这个是什么情况吗?


↙↙↙阅读原文可查看相关链接,并与作者交流