Blankj/AndroidUtilCode · error · UnsupportedOperationException

u can't instantiate me...

Error message

u can't instantiate me...

What it means

RomUtils is a utility class providing static methods for detecting the device's Android ROM manufacturer (isHuawei, isXiaomi, isOppo, getRomInfo, etc.) by reading system properties. Its private constructor throws UnsupportedOperationException to enforce static-only access.

Source

Thrown at lib/utilcode/src/main/java/com/blankj/utilcode/util/RomUtils.java:61

    private static final String[] ROM_SONY      = {"sony"};
    private static final String[] ROM_GIONEE    = {"gionee", "amigo"};
    private static final String[] ROM_MOTOROLA  = {"motorola"};

    private static final String VERSION_PROPERTY_HUAWEI  = "ro.build.version.emui";
    private static final String VERSION_PROPERTY_VIVO    = "ro.vivo.os.build.display.id";
    private static final String VERSION_PROPERTY_XIAOMI  = "ro.build.version.incremental";
    private static final String VERSION_PROPERTY_OPPO    = "ro.build.version.opporom";
    private static final String VERSION_PROPERTY_LEECO   = "ro.letv.release.version";
    private static final String VERSION_PROPERTY_360     = "ro.build.uiversion";
    private static final String VERSION_PROPERTY_ZTE     = "ro.build.MiFavor_version";
    private static final String VERSION_PROPERTY_ONEPLUS = "ro.rom.version";
    private static final String VERSION_PROPERTY_NUBIA   = "ro.build.rom.id";
    private final static String UNKNOWN                  = "unknown";

    private static RomInfo bean = null;

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

    /**
     * Return whether the rom is made by huawei.
     *
     * @return {@code true}: yes<br>{@code false}: no
     */
    public static boolean isHuawei() {
        return ROM_HUAWEI[0].equals(getRomInfo().name);
    }

    /**
     * Return whether the rom is made by vivo.
     *
     * @return {@code true}: yes<br>{@code false}: no
     */
    public static boolean isVivo() {
        return ROM_VIVO[0].equals(getRomInfo().name);

View on GitHub (pinned to 7b4caf9e54)

Solutions

  1. Use static methods: RomUtils.isHuawei(), RomUtils.getRomInfo(). Never call new RomUtils().
  2. Exclude utility classes from DI scanning and serialization configs.
  3. Mock static methods with mockito-inline for testing.

Example fix

// before
RomUtils ru = new RomUtils();
boolean huawei = ru.isHuawei();

// after
boolean huawei = RomUtils.isHuawei();
Defensive patterns

Strategy: type-guard

Validate before calling

// RomUtils is static-only
boolean huawei = RomUtils.isHuawei();
RomUtils.RomInfo info = RomUtils.getRomInfo();

Type guard

static boolean isStaticUtility(Class<?> c) {
    return c.getSimpleName().endsWith("Utils");
}

Try / catch

try {
    RomUtils.class.getDeclaredConstructor().setAccessible(true);
    RomUtils.class.getDeclaredConstructor().newInstance();
} catch (UnsupportedOperationException e) {
    // Use static methods
}

Prevention

When it happens

Trigger: Direct or reflective instantiation by DI frameworks, mocking libraries, serialization frameworks, or manual reflection.

Common situations: DI component scanning traverses the utility package; a test framework instantiates all classes for coverage; a serialization library targets the class; instrumentation frameworks that instantiate all loaded classes.

Related errors


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