debugging app when launched by intent filter

2019-06-28 05:12发布

I normally debug my apps by pressing the little "bug" icon in Eclipse.

But now I have inserted an intent filter like this in my manifest:

 <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />

While the app launches now automatically when the USB cable is plugged in - I cannot debug it anymore. There is no stop at any breakpoint anymore.

How would I debug the app when launched through an intent?

Many thanks!

3条回答
Anthone
2楼-- · 2019-06-28 05:45

I ran into this problem and I resolved it with the code below. The code is based on the prior answers. (Thanks for the ideas!)

  1. Put this code above your breakpoint.
  2. Run the app using the debug variant, not release. This step puts a debuggable APK on the device.
  3. Kill the app.
  4. Launch the app via your Intent. The app will pause at this code and spam your log with "Waiting for debugger".
  5. From studio, click "Attach debugger to Android process"
  6. If all goes well, the app should stop at your breakpoint.

Wait too long and it will ANR!

    //TODO: Remove me.  Debug only!
    while (!Debug.isDebuggerConnected()) {
        try {
            Log.d(TAG, "Waiting for debugger");
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
查看更多
ゆ 、 Hurt°
3楼-- · 2019-06-28 05:50

I have a Nexus 6P, and in my Developer options there's a "Wait for debugger" toggle. That worked for me. You also have to choose your app as the app to debug. "Select debug app". And of course you have to have installed the debuggable version of your app. Just do a debug launch in Eclipse or Android Studio.

enter image description here

查看更多
SAY GOODBYE
4楼-- · 2019-06-28 05:53

The solution is like this, put a delay in the app when it starts due to the intent filter launch. Then run to the debug window as long as the app is visible there, attach the app process and viola you may debug the app like before

查看更多
登录 后发表回答