Blankj/AndroidUtilCode · info · UnsupportedOperationException

u can't instantiate me...

Error message

u can't instantiate me...

What it means

DeviceUtils is a final utility class whose private constructor throws UnsupportedOperationException("u can't instantiate me..."). All device queries (rooted status, SDK version, device model, etc.) are static, so no instance is needed; the constructor only enforces the no-instantiation contract shared across the library's *Utils classes.

Source

Thrown at lib/utilcode/src/main/java/com/blankj/utilcode/util/DeviceUtils.java:45

import androidx.annotation.RequiresPermission;

import static android.Manifest.permission.ACCESS_WIFI_STATE;
import static android.Manifest.permission.CHANGE_WIFI_STATE;
import static android.Manifest.permission.INTERNET;
import static android.content.Context.WIFI_SERVICE;

/**
 * <pre>
 *     author: Blankj
 *     blog  : http://blankj.com
 *     time  : 2016/8/1
 *     desc  : utils about device
 * </pre>
 */
public final class DeviceUtils {

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

    /**
     * Return whether device is rooted.
     *
     * @return {@code true}: yes<br>{@code false}: no
     */
    public static boolean isDeviceRooted() {
        String su = "su";
        String[] locations = {"/system/bin/", "/system/xbin/", "/sbin/", "/system/sd/xbin/",
                "/system/bin/failsafe/", "/data/local/xbin/", "/data/local/bin/", "/data/local/",
                "/system/sbin/", "/usr/bin/", "/vendor/bin/"};
        for (String location : locations) {
            if (new File(location + su).exists()) {
                return true;
            }
        }
        return false;

View on GitHub (pinned to 7b4caf9e54)

Solutions

  1. Call the static API directly: DeviceUtils.isDeviceRooted(), DeviceUtils.getSDKVersion(), etc.
  2. Exclude final utility classes from any reflective instantiation framework.
  3. In a contract test, assert UnsupportedOperationException is thrown by the reflected constructor.

Example fix

// before (reflection)
DeviceUtils du = DeviceUtils.class.getDeclaredConstructor().newInstance();

// after
boolean rooted = DeviceUtils.isDeviceRooted();
Defensive patterns

Strategy: validation

Validate before calling

// Never construct DeviceUtils; it is static-only.
if (clazz == DeviceUtils.class || Modifier.isFinal(clazz.getModifiers())) {
    // skip instantiation; use DeviceUtils.xxx() statically
}

Type guard

if (clazz.getName().endsWith("Utils") && Modifier.isFinal(clazz.getModifiers())) {
    // static-only; do not newInstance()
}

Try / catch

try {
    Constructor<?> c = DeviceUtils.class.getDeclaredConstructor();
    c.setAccessible(true);
    c.newInstance();
} catch (UnsupportedOperationException e) {
    // expected; use DeviceUtils.xxx() instead
}

Prevention

When it happens

Trigger: Reflective instantiation via getDeclaredConstructor().newInstance() (with setAccessible), or passing the Class to a reflective factory/coverage tool. A normal `new DeviceUtils()` is a compile error because the constructor is private.

Common situations: Coverage tooling that instantiates every class; DI containers scanning packages; a developer who assumes instance methods; reflective test harnesses that exercise all constructors.

Related errors


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