Blankj/AndroidUtilCode · info · UnsupportedOperationException

u can't instantiate me...

Error message

u can't instantiate me...

What it means

IntentUtils is a final utility class whose private constructor throws UnsupportedOperationException("u can't instantiate me..."). All intent helpers (isIntentAvailable, launchApp, shareText, openUrl, getInstallAppIntent, etc.) are static; the constructor exists only to block instantiation, consistent with the library-wide *Utils convention.

Source

Thrown at lib/utilcode/src/main/java/com/blankj/utilcode/util/IntentUtils.java:35

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresPermission;
import androidx.core.content.FileProvider;

import static android.Manifest.permission.CALL_PHONE;

/**
 * <pre>
 *     author: Blankj
 *     blog  : http://blankj.com
 *     time  : 2016/09/23
 *     desc  : utils about intent
 * </pre>
 */
public final class IntentUtils {

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

    /**
     * Return whether the intent is available.
     *
     * @param intent The intent.
     * @return {@code true}: yes<br>{@code false}: no
     */
    public static boolean isIntentAvailable(final Intent intent) {
        return Utils.getApp()
                .getPackageManager()
                .queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY)
                .size() > 0;
    }

    /**
     * Return the intent of install app.
     * <p>Target APIs greater than 25 must hold

View on GitHub (pinned to 7b4caf9e54)

Solutions

  1. Use the static API directly: IntentUtils.isIntentAvailable(intent), IntentUtils.launchApp(pkgName), IntentUtils.openUrl(url), etc.
  2. Add IntentUtils to the exclusion list of any tool that reflectively instantiates classes.
  3. For a contract test, expect UnsupportedOperationException from the reflected constructor.

Example fix

// before (reflection)
IntentUtils iu = IntentUtils.class.getDeclaredConstructor().newInstance();

// after
if (IntentUtils.isIntentAvailable(intent)) startActivity(intent);
Defensive patterns

Strategy: validation

Validate before calling

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

Type guard

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

Try / catch

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

Prevention

When it happens

Trigger: Reflective construction via getDeclaredConstructor().newInstance() with setAccessible, or a framework that new-ups classes by Class reference. A direct `new IntentUtils()` fails to compile because the constructor is private.

Common situations: Coverage tooling that instantiates all classes; 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/fd8f40f99bd3910b. Report an issue: GitHub.