1

As the title suggests I am trying to debug my Android app running on a device via USB to the Android Studio debugger.

Basically the code behaves very differently when opened via an intent from the email and there are elements passed to the app it needs to parse and then act upon, so it is not just your normal debugging session.

I have tried the following solutions without success:

debugging app when launched by intent filter

and

Debug android app when it starts on device

Basically I cannot attach the debugger without starting it from the Android Studio, which defeats the purpose. Perhaps I have misunderstood the items above or there is a step in Android Studio that will allow you to attached to remote session on a device that I am unaware of.

What want to do is:

  1. Click a link in an email on the android device
  2. This will trigger the start of the app
  3. The Android Studio debugger will open to my break point at the intent where it parses the input, on my development box attached by USB
  4. I can debug from there.

I can provide email link examples, the example of the intent if needed. I do have USB debugging enabled and can debug normal runs (initiated by Android Studio) on the device without issue.

thanks!

Stephen McCormick
  • 1,706
  • 22
  • 38
  • If you launch it from android studio, then click the link in the email it ought to work. That does not make debugging pointless, as a new Intent will be delivered starting a new Activity. – Gabe Sechan Sep 10 '18 at 19:14
  • It does hook to the debugger, but a new Activity (onCreate()) is not triggered. Instead the onRestart() and onResume() get triggered. Guess I can push the code there too and just see if it behaves correctly? – Stephen McCormick Sep 10 '18 at 20:00
  • If that's the behavior, probably due to some manifest setting, then you need it there too. That's the behavior you'll see if your app is already open when a link is clicked – Gabe Sechan Sep 10 '18 at 20:02
  • Ideas of what manifest setting? I think the problem might also be because the email/intent approach assumes that app is not running, that the code maybe should be modified for that scenario (handle it if the app is already running too). Thanks! – Stephen McCormick Sep 10 '18 at 20:54

1 Answers1

0

Thanks to Gabe Sechan who put me on the right track.

First, I was never directly able to run the app on the device and hook into it via the debugger. Rather, I had to start the App on the device via the Android Studio, then click the link on the email which then hooked into the debugger.

The link in the email looked something like this (it has a built in re-direct to our servers):

https://ourcompany.net/redirect?url=myapp%3A%2F%2FDoStuff/123456

This might have been because the way the Intent was setup. Please don't judge, this was here be I was and I know it is not the recommended way of doing Intents, but because of the existing customer base I am not able to re-factor to better approach, in the AndroidManifest.xml:

 <activity
    android:name="com.mycompany.StartActivity"
    android:configChanges="orientation|keyboardHidden|screenSize|keyboard|navigation"
    android:label="StartActivity"
    android:launchMode="singleTask"
    android:windowSoftInputMode="stateHidden" >
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <data android:scheme="myapp" />
    </intent-filter>
</activity>

As you can see it is designed to run as a singleton, so when the Intent is activated, rather than calling onCreate() with the App running from the Debugger, it calls onNewIntent(). I needed to add the code to that overloaded method too:

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    setIntent(intent);
    processIntentLaunch(); // Code to test or run when Intent is triggered...
}

The processIntentLaunch code:

private void processIntentLaunch() {
    // test if this is from an email
    try {
        Intent intent = getIntent();
        if (intent.getAction() != null) {

            if (Intent.ACTION_VIEW.equals(intent.getAction())) {
                // SAMPLE: myapp://DoStuff/123456
                Uri uri = intent.getData();
                String host = uri.getHost();
                String uriStr =  uri.toString();

                // An anonymous dispatch
                if (host.equalsIgnoreCase("DoStuff")) {
                    String token = uriStr.substring(uriStr.indexOf("DoStuff") + "DoStuff".length()+1, uriStr.length());
                    // Now do stuff with the data. Note query string URI probably better, but again bit by previous design
                }
            }
        }
    }
    catch (Exception ex) {
        // Handle error
    }
}

This was probably for the best, so besides the code being in the OnCreate (in case the App is not already running) if the App is running, then it will still work (and I can debug the code).

Hope that helps someone else, someday.

Stephen McCormick
  • 1,706
  • 22
  • 38