How to resolve "Failed to Connect to Socket" error in Android Studio
Problem
When I tried to debug my Android app, I got this error:
failed to connect to socket 'localabstract:/{package-name}-0/platform-1772206044052.sock': could not connect to localabstract addressThe error appeared in the Debug tab when I clicked the Debug button. I had been working on the project for days without issues, then suddenly debugging stopped working.
Environment
- Android Studio Hedgehog (2023.1.1)
- Android 13 (API 33) target device
- Gradle 8.0
- macOS Sonoma
What happened?
I was debugging my app normally. I could run the app, but when I tried to attach the debugger, the connection failed.
The error message mentioned a “socket” file. I looked up what this means:
Android Studio → ADB → Device/Emulator ↓ ↓ ↓Creates socket Forwards Listens onconnection connection localabstractA socket is a communication endpoint. Android Studio uses Unix domain sockets (the “localabstract” type) to connect to your running app for debugging. The socket file path includes your package name and a timestamp.
When I tried debugging, Android Studio couldn’t find or connect to this socket file.
How to solve it?
I first tried restarting Android Studio and my device:
1. Closed Android Studio2. Killed my app on the device3. Opened Android Studio again4. Connected device again5. Tried to debugThis didn’t work. I still got the same socket error.
Then I tried killing the ADB server:
adb kill-serveradb start-serverThis also didn’t fix the issue.
After searching online, I found a solution. The problem was corrupted build artifacts. Android Studio stores temporary files for each debugging session, and sometimes these get corrupted.
I went to the menu bar and clicked:
Build → Clean ProjectWait for the Clean Project operation to complete (you’ll see the progress bar in the bottom right).
Then I ran:
Build → Rebuild ProjectNow test the debug connection again. I clicked the Debug button, and the debugger attached successfully.
You can see that I succeeded to resolve the socket error.
Alternative method using command line
If the Android Studio menu doesn’t work, you can clean from the terminal:
./gradlew clean./gradlew assembleDebugThis does the same thing as the Build → Clean Project menu option.
The reason
I think the key reason for the error is:
- Android Studio creates socket files in the build directory for debugging connections
- These files contain temporary state for the debug session
- When build artifacts get corrupted (from crashes, force quits, or system issues), the socket file becomes invalid
- The debugger tries to connect to a corrupted or non-existent socket and fails
The Clean Project operation deletes the build directory, which removes the corrupted socket files. When you rebuild, fresh socket files are created and debugging works again.
This issue often happens after:
- Android Studio crashes or force quits
- System restarts without closing Android Studio properly
- Gradle build errors
- Switching between multiple projects
Summary
In this post, I showed how to resolve the “failed to connect to socket” error in Android Studio. The key point is using Clean Project to remove corrupted build artifacts that prevent the debugger from creating a proper socket connection.
Final Words + More Resources
My intention with this article was to help others share my knowledge and experience. If you want to contact me, you can contact by email: Email me
Here are also the most important links from this article along with some further resources that will help you in this scope:
- 👨💻 Android Debug Bridge (ADB) Documentation
- 👨💻 Android Studio Build and Run App Guide
- 👨💻 Reddit Discussion on Android Debugging Issues
Oh, and if you found these resources useful, don’t forget to support me by starring the repo on GitHub!
Comments