Blankj/AndroidUtilCode · error · UnsupportedOperationException

u can't instantiate me...

Error message

u can't instantiate me...

What it means

Utils is the core initialization class for the whole AndroidUtilCode library. Its private constructor throws UnsupportedOperationException to prevent instantiation. The class holds a static Application reference (sApp) and exposes static init()/getApp(); it is the entry point all other utils depend on, so it must never be constructed.

Source

Thrown at lib/utilcode/src/main/java/com/blankj/utilcode/util/Utils.java:36

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

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

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

    /**
     * Init utils.
     * <p>Init it in the class of UtilsFileProvider.</p>
     *
     * @param app application
     */
    public static void init(final Application app) {
        if (app == null) {
            Log.e("Utils", "app is null.");
            return;
        }
        if (sApp == null) {
            sApp = app;
            UtilsBridge.init(sApp);
            UtilsBridge.preLoad();
            return;

View on GitHub (pinned to 7b4caf9e54)

Solutions

  1. Initialize once in Application.onCreate via Utils.init(this) (or via UtilsFileProvider), then use Utils.getApp() instead of constructing the class.
  2. Provide a Gson InstanceCreator / Jackson mixin returning a sentinel so the constructor is not invoked.
  3. Use Mockito.mockStatic for mocking rather than constructing Utils.
  4. Keep the class final with a private constructor.

Example fix

// before
Utils u = new Utils();

// after
// in Application.onCreate()
Utils.init(this);
Context ctx = Utils.getApp();
Defensive patterns

Strategy: validation

Validate before calling

if (Modifier.isFinal(Utils.class.getModifiers())) {
  throw new IllegalStateException("Utils is static-only; call Utils.init() then Utils.getApp().");
}

Prevention

When it happens

Trigger: Reflective instantiation via Utils.class.getDeclaredConstructor().setAccessible(true).newInstance(), or transitively through Gson/Jackson deserialization, Mockito/PowerMock, Kotlin reflection, or a DI scan.

Common situations: Serialization frameworks binding a field to Utils; mock frameworks in tests; coverage probes; accidental `new Utils()` after weakening modifiers. Because Utils is the library root, constructing it also implies the app never called Utils.init(app).

Related errors


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