Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get stack trace during runtime android

Tags:

android

trace

My app is not behaving as I expect. After the device is going to sleep (black screen) and is turned on, the device shows the screen of my app, but then a moment later part of the screen disappears - the tabbed viewpager which is below the toolbar.

I put a break point at the last point where my app is doing anything and the screen is still fine, so it is something the system is doing but I do not know what.

Is there a way to turn on a stack trace so that very method the system is using from that point on is logged and maybe that will allow me to find out why the tabbed viewpager disappears?

I should mention that the app is still functioning properly and I am able to open the navigation drawer from the toolbar and use all it's functions but I cannot see anymore the tabbed viewpager, until I recreate it.

like image 359
Zvi Avatar asked Aug 05 '16 09:08

Zvi


2 Answers

If you are able to get back the logs properly, most probably the issue may be resolved. As others mentioned, logcat is a good option.

A refined solution can be an efficient logger system, which will capture all logs. You can write these to a local file for your future reference.

JakeWharton's Hugo is such an awesome library where you need only little modifications using annotations @DebugLog . This will capture your application logs and print in logcat.

Your bundle.gradle (Module:app) should be modified as follows

apply plugin: 'com.android.application'
apply plugin: 'hugo'
android {
    compileSdkVersion 24
    buildToolsVersion "23.0.3"
    ....... Other configurations

In the bundle.gradle (Project) these should be present

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.1.2'
        classpath 'com.jakewharton.hugo:hugo-plugin:1.2.1'
    }
}

In order to make hugo track your application logs @DebugLog needs to be added . For example , in your class methods. You can find this in hugo sample

Now inorder to get logs back, do the following .

The following code does not have to do anything with hugo library, it will just capture logs from logcat.

Inside and AsyncTask do the following and write the content to a file and verify those.

        @Override
        protected String doInBackground(Void... voids) {
            String responseString = null;

                try {

                    Process process = Runtime.getRuntime().exec("logcat -b main -d");
                    BufferedReader bufferedReader = new BufferedReader(
                            new InputStreamReader(process.getInputStream()));
                    String line;
                    StringBuilder log=new StringBuilder();
                    while ((line = bufferedReader.readLine()) != null) {

                            log.append(line);
                            log.append('\n');
                            log.append("-----------");
                    }
                    responseString = log.toString();

                } catch (Exception e) {
                }
            return responseString;
        }

It may look complicated, but believe me, its way good enough than other libraries and simplifies developers work.

You can also try with Runtime.getRuntime().exec("logcat -d");

Good Luck , Try it out. !!

like image 162
Sreehari Avatar answered Oct 15 '22 00:10

Sreehari


Android Studio includes a debugger that allows you to debug apps running on the Android Emulator or a connected Android device. With the Android Studio debugger, you can:

Select a device to debug your app on. Set breakpoints in your code. Examine variables and evaluate expressions at runtime.

For more info visit https://developer.android.com/studio/debug/index.html

like image 41
Shanto George Avatar answered Oct 14 '22 22:10

Shanto George