MuntashirAkon/AppManager · error · IllegalArgumentException

Invalid hex length " + length

Error message

Invalid hex length " + length

What it means

Validation guard in BinaryXmlPullParser.hexStringToBytes: a hex-encoded binary attribute string must have an even number of characters (2 chars per byte); the parser encountered a string whose length is odd, meaning the attribute value is truncated or corrupted, and refuses to decode it. Faulty input: the odd-length hex string being parsed from the binary XML.

Source

Thrown at libcore/compat/src/main/java/io/github/muntashirakon/compat/xml/BinaryXmlPullParser.java:917

        throw new IllegalArgumentException("Invalid hex char '" + c + "'");
    }

    static String bytesToHexString(byte[] value) {
        final int length = value.length;
        final char[] buf = new char[length * 2];
        int bufIndex = 0;
        for (int i = 0; i < length; i++) {
            byte b = value[i];
            buf[bufIndex++] = HEX_DIGITS[(b >>> 4) & 0x0F];
            buf[bufIndex++] = HEX_DIGITS[b & 0x0F];
        }
        return new String(buf);
    }

    static byte[] hexStringToBytes(String value) {
        final int length = value.length();
        if (length % 2 != 0) {
            throw new IllegalArgumentException("Invalid hex length " + length);
        }
        byte[] buffer = new byte[length / 2];
        for (int i = 0; i < length; i += 2) {
            buffer[i / 2] = (byte) ((toByte(value.charAt(i)) << 4)
                    | toByte(value.charAt(i + 1)));
        }
        return buffer;
    }
}

View on GitHub (pinned to 0152f468fc)

Solutions

  1. Fix the producer to always emit an even number of hex chars (bytesToHexString never produces odd output).
  2. Validate value.length() % 2 == 0 before decoding; pad with a leading '0' only if the original data allows it.
  3. Re-export the attribute from the source data if it was truncated in storage.
  4. Catch IllegalArgumentException and treat the attribute as corrupt.

Example fix

// before
byte[] b = hexStringToBytes("abc"); // odd length throws
// after
String hex = "abc";
if (hex.length() % 2 != 0) hex = "0" + hex; // or reject
byte[] b = hexStringToBytes(hex);
Defensive patterns

Strategy: validation

Validate before calling

boolean evenLen = value != null && value.length() % 2 == 0;

Try / catch

try {
    byte[] b = hexStringToBytes(value);
} catch (IllegalArgumentException e) {
    // treat as corrupt attribute; fall back to stored raw bytes if available
}

Prevention

When it happens

Trigger: Passing a hex string with an odd number of characters to hexStringToBytes() or reading a TYPE_BYTES_HEX attribute whose stored text has odd length.

Common situations: Truncated or hand-trimmed hex values (a leading "0 " accidentally dropped), or a producer that serialized an odd-length string.

Understand the failure class

Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.

Related errors


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