Blankj/AndroidUtilCode · warning · UnsupportedOperationException

u can't instantiate me...

Error message

u can't instantiate me...

What it means

subutil Utils is a final utility class whose private constructor throws UnsupportedOperationException. It holds only a static Application reference and exposes static init/getApp methods; instances are intentionally non-constructible.

Source

Thrown at lib/subutil/src/main/java/com/blankj/subutil/util/Utils.java:40

 *      /  /:/~/::\  /__/\   /  /\  /  /:/~/::\   _____\__\:\   __|  |:|     \__\/\:\
 *     /__/:/ /:/\:| \  \:\ /  /:/ /__/:/ /:/\:\ /__/::::::::\ /__/\_|:|____    \  \:\
 *     \  \:\/:/~/:/  \  \:\  /:/  \  \:\/:/__\/ \  \:\~~\~~\/ \  \:\/:::::/     \__\:\
 *      \  \::/ /:/    \  \:\/:/    \  \::/       \  \:\  ~~~   \  \::/~~~~      /  /:/
 *       \  \:\/:/      \  \::/      \  \:\        \  \:\        \  \:\         /__/:/
 *        \  \::/        \__\/        \  \:\        \  \:\        \  \:\        \__\/
 *         \__\/                       \__\/         \__\/         \__\/
 *     blog  : http://blankj.com
 *     time  : 16/12/08
 *     desc  : utils about initialization
 * </pre>
 */
public final class Utils {

    @SuppressLint("StaticFieldLeak")
    private static Application sApplication;

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

    /**
     * Init utils.
     * <p>Init it in the class of Application.</p>
     *
     * @param context context
     */
    public static void init(final Context context) {
        if (context == null) {
            init(getApplicationByReflect());
            return;
        }
        init((Application) context.getApplicationContext());
    }

    /**
     * Init utils.

View on GitHub (pinned to 7b4caf9e54)

Solutions

  1. Use the static API: Utils.init(context) once and Utils.getApp(); never instantiate.
  2. Configure reflective frameworks to skip final utility classes.
  3. Remove newInstance paths targeting Utils.

Example fix

// before
Utils u = reflectNew(Utils.class); // throws

// after
Utils.init(application);
Application app = Utils.getApp();
Defensive patterns

Strategy: validation

Validate before calling

// subutil Utils is static-only; never instantiate.
Utils.init(application);
Application app = Utils.getApp();

Type guard

private static boolean isStaticUtility(Class<?> c) {
    return Modifier.isFinal(c.getModifiers()) && hasOnlyStaticMethods(c);
}

Prevention

When it happens

Trigger: Reflective instantiation or unsafe allocation of Utils instead of using its static lifecycle methods.

Common situations: Reflection/DI/serialization frameworks that instantiate arbitrary classes; tests that build utilities reflectively.

Related errors


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