adb 埠自定義及原理說明

maweiliang發表於2018-10-25

日常開發的時候,adb埠經常被一些其他程式佔用,譬如一些手機助手軟體會頻繁的導致5037埠不可用,其實我們可以自定義adb的埠,而且一勞永逸,不用在頻繁的 adb kill-server的命令 或者查詢佔用埠的程式去殺掉,這也太麻煩。

  • win系統配置ANDROID_ADB_SERVER_PORT

在系統環境變數中定義 ANDROID_ADB_SERVER_PORT 的值即可,這樣adb在執行的時候回使用我們配置的ANDROID_ADB_SERVER_PORT值,而不是原來預設的5037這個埠,

環境變數

  • 配置ANDROID_ADB_SERVER_PORT作用

大家好奇為啥通過配置ANDROID_ADB_SERVER_PORT就可以修改adb的埠,看看adb的原始碼

    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 port where 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;
    }
複製程式碼

上面的只是擷取了主要獲取埠號的方法getAdbServerPort(),該方法首先獲取作業系統屬性ANDROID_ADB_SERVER_PORT配置的值,如果找到就返回,否則才會返回預設的值DEFAULT_ADB_PORT,也是5037這個埠,所有我們通過配置ANDROID_ADB_SERVER_PORT是可以修改adb埠的目的

如果配置完ANDROID_ADB_SERVER_PORT該屬性,檢測不到裝置的話,重啟一下電腦就好

完整原始碼連線

相關文章