Debugger disconnecting in Android Studio when debugging a home screen widget
Asked Answered
K

7

34

I am trying to debug an Android home screen widget application that I made. When pressing a button on the widget, the debugger starts and then it just disconnects from the VM. No reason is given. I am using Windows 10.

This is what the logs say:

D/Atlas: Validating map... D/libEGL: loaded
/system/lib/egl/libEGL_emulation.so D/libEGL: loaded
/system/lib/egl/libGLESv1_CM_emulation.so D/libEGL: loaded
/system/lib/egl/libGLESv2_emulation.so

D/         ] HostConnection::get() New Host Connection established 0xef0394f0, tid 3709 
I/OpenGLRenderer: Initialized EGL, version 1.4
D/OpenGLRenderer: Enabling debug mode 0 W/EGL_emulation: eglSurfaceAttrib not implemented 
W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xef1485e0, error=EGL_SUCCESS
D/roboguice.RoboGuice: Using annotation database(s).
D/roboguice.RoboGuice: Using annotation database(s) : [, roboguice]
D/roboguice.RoboGuice: Time spent loading annotation databases : 21
W/art: Verification of com.gdbd.geedeebeedee.model.GdbdWidgetRecord 
    com.gdbd.geedeebeedee.model.GdbdWidgetRecordsRealmImpl.create(java.util.Date) took 127.878ms
W/art: Verification of com.gdbd.geedeebeedee.model.GdbdWidgetRecord
    com.gdbd.geedeebeedee.model.GdbdWidgetRecordsRealmImpl.getTodaysRecord() took 111.586ms
D/GdbdWidgetBase: Received intent: android.appwidget.action.APPWIDGET_ENABLED
D/GdbdWidgetBase: Updating widget index: 0 with id: 23
D/GdbdWidgetBase: Received intent: android.appwidget.action.APPWIDGET_UPDATE
D/GdbdWidgetBase: Received intent: android.appwidget.action.APPWIDGET_UPDATE_OPTIONS
D/GdbdWidgetBase: Received intent: change_day_staus
I/art: Thread[5,tid=3470,WaitingInMainSignalCatcherLoop,Thread*=0xf3c30c00,
    peer=0x22c0a0a0,"Signal Catcher"]: reacting to signal 3
I/art: Wrote stack traces to '/data/anr/traces.txt' 
Disconnected from the target VM, address: 'localhost:8619', transport: 'socket'

I also pulled traces.txt, but haven't found any obvious error message. Maybe I don't know what to look for. When first placing the widget on the screen the debugger doesn't disconnect. This happens with both the official Emulator and GenyMotion. The emulator is Google Nexus 5 5.1.0 API 22.

enter image description here

These are the Gradle build setting:

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.0"
    defaultConfig {
        applicationId "com.gdbd.geedeebeedee"
        minSdkVersion 15
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    } }

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })

    compile group: 'com.google.code.gson', name: 'gson', version: '2.3.1'
    compile 'com.android.support:appcompat-v7:25.0.0'

    compile 'org.roboguice:roboguice:3.+'
    provided 'org.roboguice:roboblender:3.+'

    testCompile 'junit:junit:4.12' }

Does anyone know what is happening and why I cannot debug? Thanks.

Edit 1:

Apparently the process dies.
enter image description here

D/OpenGLRenderer: Enabling debug mode 0 
W/EGL_emulation: eglSurfaceAttrib not implemented 
W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xf3ff6900, error=EGL_SUCCESS
D/roboguice.RoboGuice: Using annotation database(s).
D/roboguice.RoboGuice: Using annotation database(s) : [, roboguice]
D/roboguice.RoboGuice: Time spent loading annotation databases : 19
80:   80 D/         ] Socket deconnection 
D/GdbdWidgetBase: Received intent: change_day_staus
80:   80 D/         ] Socket deconnection
80:   80 D/         ] Socket deconnection
80:   80 D/         ] Socket deconnection
80:   80 D/         ] Socket deconnection 
I/art: Thread[5,tid=13474,WaitingInMainSignalCatcherLoop,Thread*=0xf3c30c00,
    peer=0x12c0a0a0,"Signal Catcher"]: reacting to signal 3
I/art: Wrote stack traces to '/data/anr/traces.txt'

Edit 2 added Android manifest file

`

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

    <meta-data
        android:name="roboguice.modules"
        android:value="com.gdbd.geedeebeedee.model.ModelModule" />


    <activity
        android:name=".activity.StatsActivity"
        android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <receiver android:name=".widget.GdbdWidgetBase" >
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
        </intent-filter>
        <intent-filter>
            <action android:name="change_day_staus" />
        </intent-filter>


        <meta-data android:name="android.appwidget.provider"
            android:resource="@xml/gdbd_widget_info" />

    </receiver>

</application>

`

Edit 3 traces.txt https://ufile.io/c41eb

Edit 4 GdbdWidgetBase

@Inject
    GdbdWidgetRecords gdbdWidgetRecords;

    public void onHandleUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        // Perform this loop procedure for each App Widget that belongs to this provider
        for (int widgetIndex = 0; widgetIndex < appWidgetIds.length; widgetIndex++) {
            int widgetId = appWidgetIds[widgetIndex];
            Log.d(TAG, "Updating widget index: " + widgetIndex + " with id: " + widgetId);

            GdbdWidgetRecord todaysRecord = gdbdWidgetRecords.getTodaysRecord();

            RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.gdbd_widget);

            if(todaysRecord.getCurrentStatus() == DayStatus.GOODDAY)
                views.setImageViewResource(R.id.btnClicker, R.drawable.ic_gd);
            else if(todaysRecord.getCurrentStatus() == DayStatus.BADDAY)
                views.setImageViewResource(R.id.btnClicker, R.drawable.ic_bd);
            else if(todaysRecord.getCurrentStatus() == DayStatus.NOTSET)
                views.setImageViewResource(R.id.btnClicker, R.drawable.ic_undecided);


            Intent intent = new Intent(context, GdbdWidgetBase.class);
            intent.setAction(CHANGE_DAY_STATUS);
            intent.putExtra(WIDGET_ID_EXTRA, widgetId);
            PendingIntent changeStatusIntent = PendingIntent.getBroadcast(context, widgetId, intent,
                    PendingIntent.FLAG_CANCEL_CURRENT);
            views.setOnClickPendingIntent(R.id.btnClicker, changeStatusIntent);

            scheduleUpdateAtMidnight(context, widgetId);

            appWidgetManager.updateAppWidget(widgetId, views);
        }
    }

    @Override
    public void onHandleReceived(Context context, Intent intent) {
        Log.d(TAG, "Received intent: " + intent.getAction());
        //Log.d(TAG,"getDebugUnregister: " + (getDebugUnregister()?"true":"false"));

        if (CHANGE_DAY_STATUS.equals(intent.getAction())) {

            int appWidgetId = intent.getIntExtra(WIDGET_ID_EXTRA, -1);
            if (appWidgetId >= 0) {
                GdbdWidgetRecord todaysRecord = gdbdWidgetRecords.getTodaysRecord();
                Log.d(TAG, "today record: " + todaysRecord.toString());
                DayStatus newDayStatus = DayStatus.NOTSET;

                if(todaysRecord.getCurrentStatus() == DayStatus.NOTSET)
                    newDayStatus = DayStatus.GOODDAY;
                else if(todaysRecord.getCurrentStatus() == DayStatus.GOODDAY)
                    newDayStatus = DayStatus.BADDAY;
                else if(todaysRecord.getCurrentStatus() == DayStatus.GOODDAY)
                    newDayStatus = DayStatus.NOTSET;

                Log.d(TAG, "newDayStatus: " + newDayStatus);

                gdbdWidgetRecords.changeTodaysStatus(newDayStatus);
                onHandleUpdate(context, AppWidgetManager.getInstance(context), new int[]{appWidgetId});
            }
        }
    }

    private void scheduleUpdateAtMidnight(Context context, int widgetId) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        calendar.set(Calendar.SECOND, 1);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.add(Calendar.DAY_OF_YEAR, 1);

        Intent updateAtMidnightIntent = new Intent(context, GdbdWidgetBase.class);
        updateAtMidnightIntent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
        updateAtMidnightIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, new int[]{widgetId});

        PendingIntent broadcastIntent = PendingIntent.getBroadcast(context, widgetId + 1,
                updateAtMidnightIntent, PendingIntent.FLAG_CANCEL_CURRENT);

        AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), broadcastIntent);
        } else {
            alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), broadcastIntent);
        }
    }

Edit 5 The breakpoint is always in the onReceive() method of my AppWidget. For other intents like android.appwidget.action.APPWIDGET_ENABLED it takes more before the debugger stops, but my app still dies

01-09 22:28:20.861 3881-3881/com.gdbd.geedeebeedee D/GdbdWidget: Received intent: android.appwidget.action.APPWIDGET_ENABLED
01-09 22:29:20.854 3881-3886/com.gdbd.geedeebeedee I/art: Thread[2,tid=3886,WaitingInMainSignalCatcherLoop,Thread*=0x7fbcebe06000,peer=0x2ac070a0,"Signal Catcher"]: reacting to signal 3
01-09 22:29:21.013 3881-3886/com.gdbd.geedeebeedee I/art: Wrote stack traces to '/data/anr/traces.txt'

Whereas for my own intent that is sent when a button is pressed within the widget it takes 10 seconds.

01-09 22:34:25.353 4314-4314/com.gdbd.geedeebeedee D/GdbdWidget: Received intent: change_day_staus
01-09 22:34:35.362 4314-4319/com.gdbd.geedeebeedee I/art: Thread[2,tid=4319,WaitingInMainSignalCatcherLoop,Thread*=0x7fbcebe06000,peer=0x2ac070a0,"Signal Catcher"]: reacting to signal 3
01-09 22:34:35.549 4314-4319/com.gdbd.geedeebeedee I/art: Wrote stack traces to '/data/anr/traces.txt'
Krona answered 2/1, 2017 at 10:13 Comment(16)
Please provide your AndroidManifest.xml, does your AppWidget run in the same process as the app?Buatti
I added the AndroidManifest. How do I check if my AppWidget runs in the same process as the app? I am new to Android development. Thank youKrona
Does it raise any warnings/dialogs on the device? Are you able to retrieve the traces.txt mentioned here: I/art: Wrote stack traces to '/data/anr/traces.txt'Buatti
I did not see any warning dialogs on the device. I uploaded traces.txt here ufile.io/c41ebKrona
Can you also provide the code for .widget.GdbdWidgetBase?Buatti
yes, I can, see the last edit. ThanksKrona
Let us continue this discussion in chat.Buatti
have you tried to debug same app with different USB cable, sometimes cable create problems.Spermicide
I only tried on virtual devices GenyMotion and Android AVD that comes with Android Studio.Krona
Where are you setting the breakpoint in your code? which part after the click to you want to debug?Moeller
I have a class GdbdWidget extends AppWidgetProvider and I set the breakpoint in the onReceive() method within this classKrona
will the app hang before dies? If you suspect there is an error inside the code you gave you could use try and catch.Pamplona
Are you using google map?Casmey
@Pravin D No, no google mapsKrona
There are plenty of reasons for killing a process; Application crash, by system in case of low memory etc...Casmey
you can try with wifi debuggingNuzzle
K
19

I went to the emulated device's Settings -> Developer Options and enabled "Show all ANRs" and now a warning appears "Your application is not responding" and I can tap "WAIT" and it seems to work.

Krona answered 22/1, 2017 at 9:45 Comment(1)
I issued adb kill-server and chose to launch no activity. Then it started working. Not sure why...Dolan
G
9

This is not a solution to stop the detaching. But you can re-attach the debugger directly afterwards. You can attach the debugger to running processes with the button in the toolbar:

enter image description here

I know that it is not ideal, but maybe it is already enough to continue debugging for you.

Gilbertson answered 9/1, 2017 at 13:55 Comment(2)
My apps process is killed and I can no longer attach to it, I have to restart it.Krona
same happening as @KronaBurny
G
3

Have you tried disabling the Instant run on Android Studio. May be that is conflicting while debugging.

Garfield answered 9/1, 2017 at 17:37 Comment(3)
I tried that and it did not work. I did notice something though. On my custom intent change_day_status, the debugger crashes in 10 seconds, on other events like android.appwidget.action.APPWIDGET_ENABLED it takes a minute.Krona
Could you let me know where all are you using your debug points. I think you are using the debug points on lines which have huge work to do be done on the UI thread may be because of which debugger is detaching.Garfield
I have a class GdbdWidget extends AppWidgetProvider and I set the breakpoint in the onReceive() method within this classKrona
T
1

Use this plugin of Android studio. ADB WIFI

Connect both devices 1) PC/Laptop 2) Mobile on the same network and do this Tools -> Android -> ADB WIFI -> ADB USB to WIFI.

Thanks

Transduction answered 11/1, 2017 at 7:19 Comment(2)
a message appears saying "Can't connect to wireless or get a valid IP address"Krona
both devices (PC/laptop and mobile) must be on the same wifi network, it should work. first, connect with USB and then run that procedure after connected you can disconnect the cable.Transduction
P
1

I had similar issue. turned out the break-point was on the line that had to do some UI task (intent call to other activity). just move your break-point to some other place that does not involve UI processing. I solved my problem this way.

Presence answered 11/9, 2020 at 10:36 Comment(0)
S
0

Here it is the answer to this question https://mcmap.net/q/452021/-android-studio-debugger-stops-unexpectedly

enter image description here

Savior answered 15/2, 2023 at 11:1 Comment(0)
S
-3

May Be this might be help you. Please once you check the below suggestion

you choose your device and next choose your package name in the right side you can see next to regex choose from the drop down show only slected application enter image description here

Sacramentarian answered 2/1, 2017 at 10:22 Comment(1)
I did what you suggested. Please see the edit above. Apparently the process dies. But I don't know why.Krona

© 2022 - 2024 — McMap. All rights reserved.