修复Android studio的adb无法连接手机问题

发布于:2025-09-11 ⋅ 阅读:(18) ⋅ 点赞:(0)

复制下面的内容到一个文本txt里面然后把里面的Android studio路径和sdk路径改成你自己的,然后改成把.txt改成bat
右键管理员运行

@echo off
REM Deep Fix for "Couldn't terminate the existing process" error
REM This script will completely reset ADB and device connection

echo ============================================
echo     DEEP ADB FIX - Process Termination Error
echo ============================================
echo.

set "ADB=E:\IDE\android-sdk\platform-tools\adb.exe"

REM Step 1: Force kill all ADB and related processes
echo [1/8] Force killing all processes...
taskkill /F /IM adb.exe 2>nul
taskkill /F /IM adb.exe /T 2>nul
taskkill /F /IM studio64.exe 2>nul
taskkill /F /IM java.exe 2>nul
taskkill /F /IM javaw.exe 2>nul
timeout /t 3 >nul
echo      Done!
echo.

REM Step 2: Kill ADB server multiple times to ensure it's dead
echo [2/8] Ensuring ADB server is completely stopped...
"%ADB%" kill-server 2>nul
timeout /t 2 >nul
"%ADB%" kill-server 2>nul
timeout /t 2 >nul
echo      Done!
echo.

REM Step 3: Delete ALL ADB cache and temp files
echo [3/8] Deleting all ADB cache and locks...
rmdir /S /Q "%USERPROFILE%\.android\cache" 2>nul
rmdir /S /Q "%USERPROFILE%\.android\build-cache" 2>nul
del /F /Q "%USERPROFILE%\.android\adbkey" 2>nul
del /F /Q "%USERPROFILE%\.android\adbkey.pub" 2>nul
del /F /Q "%USERPROFILE%\.android\*.lock" 2>nul
del /F /Q "%TEMP%\adb*.tmp" 2>nul
echo      Cache cleared!
echo.

REM Step 4: Reset USB drivers
echo [4/8] Resetting USB connection...
echo      Please unplug your phone now!
echo      Press any key after unplugging...
pause >nul
echo.
echo      Now plug your phone back in
echo      Press any key after plugging in...
pause >nul
echo      USB reset done!
echo.

REM Step 5: Start ADB with specific port
echo [5/8] Starting ADB on clean port...
"%ADB%" -P 5037 start-server
timeout /t 3 >nul
echo      ADB started!
echo.

REM Step 6: Force reconnect devices
echo [6/8] Force reconnecting devices...
"%ADB%" reconnect 2>nul
"%ADB%" reconnect device 2>nul
timeout /t 2 >nul
echo      Reconnection attempted!
echo.

REM Step 7: Kill app on device
echo [7/8] Attempting to kill app on device...
"%ADB%" shell am force-stop com.example.myapplication 2>nul
"%ADB%" shell pm clear com.example.myapplication 2>nul
echo      App processes cleared!
echo.

REM Step 8: Final device check
echo [8/8] Final device status:
echo ============================================
"%ADB%" devices -l
echo.
"%ADB%" get-state 2>nul
echo ============================================

echo.
echo Fix completed! Now try:
echo 1. In Android Studio: File -^> Invalidate Caches and Restart
echo 2. After restart, try running the app again
echo.
pause

网站公告

今日签到

点亮在社区的每一天
去签到