{
  "$type": "site.standard.document",
  "canonicalUrl": "https://segunfamisa.com/posts/android-debugging-set-debug-app",
  "coverImage": {
    "$type": "blob",
    "ref": {
      "$link": "bafkreigb2pqggv5thrhv7o5fstliua3blqwwa4p4m4anrmuksvw36r5wbe"
    },
    "mimeType": "image/png",
    "size": 18224
  },
  "description": "Android debugging tip - attach Android Studio debugger at app start up",
  "path": "/posts/android-debugging-set-debug-app",
  "publishedAt": "2020-04-26T07:00:00.000Z",
  "site": "at://did:plc:a5mekodp4afxadlpr4hp2wci/site.standard.publication/3mm2oa7vz5327",
  "tags": [
    "android studio",
    "tips",
    "debuggger",
    "adb"
  ],
  "textContent": "Recently, I had to debug a deeplink problem that happens only on fresh app start-up. I was particularly\ninterested in seeing what was happening at different parts of the app when the app\nstarts up. Naturally, I put Log.d() statements allover the app,\nbut it wasted a lot of my time. Every time I added a new log statement, I had to rebuild\nthe app.\n\nIn this short mini-blog, I'm going to share something I learned from a colleague - how to set a debug app and what it means.\n\nHow can I set a debug app?\nA \"debug app\" is the app that you want to debug.\n\nWhen you set a debug app, the app is not launched until a debugger is attached. On launching the app,\nyou see a dialog with \"Waiting for Debugger\" as its title. An example of the dialog\nis as shown in the screenshot below.\n\n<p align=\"center\">\n\t<img src=\"/images/waiting-for-debugger.png\"\n  alt=\"Screenshot showing the Android system waiting for debugger when debug app is launched\">\n</p>\n\nBy the time you see this dialog, you can (set break up points and) attach your debugger, then the app launch will resume.\n\nThere are two ways you can set your debug app - via the developer options in your device settings\nor via an adb command.\n\nSetting debug app from developer options\nYou can set the debug app and configure other options via the UI in developer options. To set it up:\n\n1. Navigate to Settings -> System -> Developer options and then scroll to the Debugging section.\n2. Click on \"Select debug app\".\n3. Select the application you want to debug from the list.\n4. Check the \"Wait for debugger\" option - this is what activates the behaviour we want\nwhere the debugged application waits for the debugger to attach before launching.\n\nThe screenshots below show the step 2-4:\n\n<p align=\"center\">\n\t<img src=\"/images/set-debug-app-2-4.png\"\n  alt=\"Screenshots of steps 2 - 4\">\n</p>\n\nAnd with that, you have set your app as the debug app.\n\nTo clear the debug app, you can deselect your app by selecting the \"Nothing\" option\nwhen you get to step 3 above.\n\nSetting debug app from adb\nIt is also possible to set the debug app from adb. I personally find this option\nquicker and more convenient than the other approach, but that's just me.\n\nThe feature is available via the set-debug-app adb command. Typical usage is:\n\nAs seen from the adb docs page:\n -w flag is what sets the \"wait for debugger\" option (like we did with the UI)\n --persistent flag is what tells the system to save this, and not treat as a one-off action\n\nTo clear the debug app via adb, you can use this command:\n\nBonus - set up command line aliases\n\nThe commands are pretty long, so I wrote aliases that helped me shorten them.\nThe aliases on Mac and Linux operating systems look like this:\n\nUsage of the alias makes it really, clean and convenient:\n\nConclusion\n\nI find setting debug app really handy when debugging app start-up problems. For example, if you want to debug\nsome code that you have in your Application class' onCreate() method, like the initialization of some important classes or SDKs you have in your app.\n\nDo you also have some nice debugging tips you use when developing for Android? Please feel\nfree to share them in the comments.\n\nThanks for reading,\n\nCheers!",
  "title": "Debugging Android app launch by setting debug app"
}