In daily development, adb port is often occupied by some other programs. For example, some mobile assistant software frequently causes port 5037 to be unavailable. In fact, we can customize adb port once and for all, and do not need to kill the process that occupies the port in the frequent ADB kill-server command or search for the process to kill. It’s too much trouble.

  • Win system configuration ANDROID_ADB_SERVER_PORT

ANDROID_ADB_SERVER_PORT = ANDROID_ADB_SERVER_PORT = 5037; ANDROID_ADB_SERVER_PORT = 5037;

  • Configure ANDROID_ADB_SERVER_PORT

You may wonder why you can modify adb ports by configuring ANDROID_ADB_SERVER_PORT

    private static final String SERVER_PORT_ENV_VAR = "ANDROID_ADB_SERVER_PORT"; //$NON-NLS-1$
    // Where to find the ADB bridge.
    static final String DEFAULT_ADB_HOST = "127.0.0.1"; //$NON-NLS-1$ static final int DEFAULT_ADB_PORT = 5037; /** * Returns the portwhere adb server should be launched. This looks at:
     * <ol>
     *     <li>The system property ANDROID_ADB_SERVER_PORT</li>
     *     <li>The environment variable ANDROID_ADB_SERVER_PORT</li>
     *     <li>Defaults to {@link #DEFAULT_ADB_PORT} if neither the system property nor the env var
     *     are set.</li>
     * </ol>
     *
     * @return The port number where the host's adb should be expected or started. */ private static int getAdbServerPort() { // check system property Integer prop =  Integer.getInteger(SERVER_PORT_ENV_VAR); if (prop ! = null) { try { return validateAdbServerPort(prop.toString()); } catch (IllegalArgumentException e) { String msg = String.format( "Invalid value (%1$s) for ANDROID_ADB_SERVER_PORT system property.", prop); Log.w(DDMS, msg); } } // when system property is not set or is invalid, parse environment property try { String env = System.getenv(SERVER_PORT_ENV_VAR); if (env ! = null) { return validateAdbServerPort(env); } } catch (SecurityException ex) { // A security manager has been installed that doesn't allow access to env vars.
            // So an environment variable might have been set, but we can't tell. // Let's log a warning and continue with ADB's default port. // The issue is that adb would be started (by the forked process having access // to the env vars) on the desired port, but within this process, we can't figure out
            // what that port is. However, a security manager not granting access to env vars
            // but allowing to fork is a rare and interesting configuration, so the right
            // thing seems to be to continue using the default port, as forking is likely to
            // fail later on in the scenario of the security manager.
            Log.w(DDMS,
                    "No access to env variables allowed by current security manager. "
                            + "If you've set ANDROID_ADB_SERVER_PORT: it's being ignored.");
        } catch (IllegalArgumentException e) {
            String msg = String.format(
                    "Invalid value (%1$s) for ANDROID_ADB_SERVER_PORT environment variable (%2$s).",
                    prop, e.getMessage());
            Log.w(DDMS, msg);
        }
        // use default port if neither are set
        return DEFAULT_ADB_PORT;
    }
Copy the code

ANDROID_ADB_SERVER_PORT () : ANDROID_ADB_SERVER_PORT () : ANDROID_ADB_SERVER_PORT (); Also the port 5037, so we can modify the purpose of adb port by configuring ANDROID_ADB_SERVER_PORT

If the ANDROID_ADB_SERVER_PORT attribute is configured and the device cannot be detected, restart the computer

Full source code link