MuntashirAkon/AppManager · error · java.lang.IllegalArgumentException

Invalid ADB port

Error message

Invalid ADB port: ${port}

What it means

setAdbPort persists the local ADB daemon port in preferences, but only after validating it is within 1–65535 via isValidAdbPort. Values outside the valid TCP port range throw IllegalArgumentException before anything is stored.

Solutions

  1. Validate the port is an integer in [1, 65535] before calling setAdbPort
  2. Use Ops.isValidAdbPort(port) as a pre-check and reject invalid input at the UI layer
  3. If the value comes from parsing, ensure Integer.parseInt succeeded and the string was a plain number
  4. Correct the stored/configured value to a real ADB port (default 5555 for adbd over TCP)

Example fix

// before
Ops.setAdbPort(Integer.parseInt(portInput));
// after
int port = Integer.parseInt(portInput.trim());
if (!Ops.isValidAdbPort(port)) {
    throw new IllegalArgumentException("Port must be 1-65535, got " + port);
}
Ops.setAdbPort(port);
Defensive patterns

Strategy: validation

Validate before calling

if (port < 1 || port > 65535) {
    throw new IllegalArgumentException("Port must be 1-65535");
}

Try / catch

try {
    Ops.setAdbPort(port);
} catch (IllegalArgumentException e) {
    // show validation error to user / use default port
    Ops.setAdbPort(DEFAULT_ADB_PORT);
}

Prevention

When it happens

Trigger: Calling Ops.setAdbPort(port) with port < 1 or port > 65535, e.g. a 0 sentinel, a negative number, or an unparsed/overflowed integer from user input.

Common situations: Reading the port from a config file or user input without parsing/validating, passing a raw string cast that yields 0, or accidentally passing a full 'host:port' string numeric remainder.

Understand the failure class

Background: "value must be between 0 and 1" / "out of range" / "must not be negative" errors: fixing range-validation failures across open-source libraries — this error's family across 42 libraries.

Related errors


AI-assisted analysis of MuntashirAkon/AppManager@0152f468fc (2026-09-12). Data as JSON: /api/errors/709818e93cc8948d. Report an issue: GitHub.

Appendix: source

Thrown at app/src/main/java/io/github/muntashirakon/AppManager/servermanager/ServerConfig.java:145

    @AnyThread
    public static boolean getAllowBgRunning() {
        return sPreferences.getBoolean("allow_bg_running", true);
    }

    @AnyThread
    @IntRange(from = 1, to = 65535)
    @NoOps
    public static int getAdbPort() {
        int port = sPreferences.getInt("adb_port", DEFAULT_ADB_PORT);
        return isValidAdbPort(port) ? port : DEFAULT_ADB_PORT;
    }

    @AnyThread
    @NoOps
    public static void setAdbPort(@IntRange(from = 1, to = 65535) int port) {
        if (!isValidAdbPort(port)) {
            throw new IllegalArgumentException("Invalid ADB port: " + port);
        }
        sPreferences.edit().putInt("adb_port", port).apply();
    }

    public static boolean isValidAdbPort(int port) {
        return port >= 1 && port <= 65535;
    }

    @AnyThread
    public static int getLocalServerPort() {
        return Prefs.Misc.getAdbLocalServerPort();
    }

    @WorkerThread
    @NonNull
    public static String getAdbHost(Context context) {
        return getHostIpAddress(context);
    }

View on GitHub (pinned to 0152f468fc)