Blankj/AndroidUtilCode · error · UnsupportedOperationException

u can't instantiate me...

Error message

u can't instantiate me...

What it means

LogUtils is a final utility class providing configurable logging with tag formatting, type-aware object serialization, and background thread execution. Its private constructor throws UnsupportedOperationException to enforce static-only access. The class holds a static Config instance and an ExecutorService for async logging.

Source

Thrown at lib/utilcode/src/main/java/com/blankj/utilcode/util/LogUtils.java:109

            "┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄";
    private static final String TOP_BORDER     = TOP_CORNER + SIDE_DIVIDER + SIDE_DIVIDER;
    private static final String MIDDLE_BORDER  = MIDDLE_CORNER + MIDDLE_DIVIDER + MIDDLE_DIVIDER;
    private static final String BOTTOM_BORDER  = BOTTOM_CORNER + SIDE_DIVIDER + SIDE_DIVIDER;
    private static final int    MAX_LEN        = 1100;// fit for Chinese character
    private static final String NOTHING        = "log nothing";
    private static final String NULL           = "null";
    private static final String ARGS           = "args";
    private static final String PLACEHOLDER    = " ";
    private static final Config CONFIG         = new Config();

    private static SimpleDateFormat simpleDateFormat;

    private static final ExecutorService EXECUTOR = Executors.newSingleThreadExecutor();

    private static final SimpleArrayMap<Class, IFormatter> I_FORMATTER_MAP = new SimpleArrayMap<>();

    private LogUtils() {
        throw new UnsupportedOperationException("u can't instantiate me...");
    }

    public static Config getConfig() {
        return CONFIG;
    }

    public static void v(final Object... contents) {
        log(V, CONFIG.getGlobalTag(), contents);
    }

    public static void vTag(final String tag, final Object... contents) {
        log(V, tag, contents);
    }

    public static void d(final Object... contents) {
        log(D, CONFIG.getGlobalTag(), contents);
    }

View on GitHub (pinned to 7b4caf9e54)

Solutions

  1. Use static methods: LogUtils.v(), LogUtils.d(), LogUtils.i(), LogUtils.w(), LogUtils.e(). Use LogUtils.getConfig() for configuration.
  2. Exclude LogUtils from DI scanning, serialization targets, and keep-rule instantiation.
  3. For testing, mock static methods with mockito-inline instead of instantiating.

Example fix

// before
LogUtils logger = new LogUtils();
logger.d("message");

// after
LogUtils.d("message");
Defensive patterns

Strategy: type-guard

Validate before calling

// LogUtils is static-only — never instantiate
// Configure LogUtils via LogUtils.getConfig() at app startup
LogUtils.getConfig().setGlobalTag("MyApp").setLogSwitch(BuildConfig.DEBUG);

Type guard

static boolean isLogUtils(Class<?> c) {
    return c == LogUtils.class;
}
// If true, only use static methods — never reflectively instantiate

Try / catch

try {
    LogUtils.class.getDeclaredConstructor().setAccessible(true);
    LogUtils.class.getDeclaredConstructor().newInstance();
} catch (UnsupportedOperationException e) {
    // Use LogUtils.d(), LogUtils.v(), etc. statically
}

Prevention

When it happens

Trigger: Reflective instantiation by DI/mock/serialization frameworks, or direct reflection calls (Class.getDeclaredConstructor().newInstance()). The constructor itself is private so normal compilation prevents direct new LogUtils().

Common situations: Mockito mock creation targeting LogUtils; Gson/Jackson deserialization with the class as a target type; a DI component scanner that traverses utility packages; ProGuard keep rules that preserve the class for reflective construction.

Related errors


AI-assisted analysis of Blankj/AndroidUtilCode@7b4caf9e54 (2026-08-14). Data as JSON: /api/errors/c87017e5b3ef4e62. Report an issue: GitHub.