So I got annoyed at always having to reconnect to Stetho, and I figured there would be some better way to do this in Android Studio ... and maybe there is, but this is the best I've gotten so far. Putting this together in a gist was inspired by a question on reddit: https://www.reddit.com/r/androiddev/comments/7hz3xy/stetho_anyone_know_of_a_convenient_way_to/
- Use Android Studio custom Run configuration to call a new gradle task
:app:launchStetho
before the app launches - The gradle task uses
launchctl
(docs) to start an out-of-band AppleScript automationopen-stetho.applescript
- The AppleScript waits a few seconds, then controls Chrome to open chrome://inspect and loops until the "Inspect" action is available, and then executes a
click()
against that DOM element
- Choose a location for the applescript and ensure that it's executable
chmod +x launch-stetho.applescript
- Create the launchctl plist in
~/Library/LaunchAgents
- run
launchctl load ~/Library/LaunchAgents/com.whatever.example.launchStetho.plist
- test that it's working by running
launchctl start com.whatever.example.launchStetho
... switch to Chrome and stetho should open. - Create a new configuration in AndroidStudio called "Run + Stetho" or whatever, and at the bottom of the "General" tab, click the
+
button below the "Before Launch" section, and then click "Run Gradle Task" and specify the module (called project here, probably:app
if you click the…
button) and task name:app:launchStetho
- Select this new config and run
- make sure that the launchtl plist is loaded (it should show up if you run
launchctl list
) - make sure that the path in the
<ProgramArguments>
array is where you have placed your applescript, and that it's executable - make sure that the command in the gradle task is correct (the final argument should be the value of the
Label
key in the LaunchAgent plist) - make sure that the selector in the applescript matches the
inspect
link in http://chrome/inspect - if all else fails, try to run any of the individual pieces, starting with the simplest (the applescript), then the gradle task, then the Android Studio config
- if the applescript fails, open the developer inspector of your chrome tab and see if there are any log messages, this may be helpful
- comment on this gist or ping me on twitter https://twitter.com/compassing
Why all the complexity? Basically it comes down to needing a way to let the gradle task to launch Chrome automation complete so it doesn't block the build. I tried various more vanilla ways to re-parent the AppleScript but none of them worked for me. (though https://stackoverflow.com/questions/20338162/how-can-i-launch-a-new-process-that-is-not-a-child-of-the-original-process seems to have some promising candidates.. :) So, launchctl
being an independent daemon that will start processes at will seemed like a clear choice.
- support other platforms
- figure out a way to know which emulator port is being used
- actually send the package of the current app/variant and turn that into the selector in
open-stetho.applescript
- make this easier to set up/install
- i'm sure many other things