Blankj/AndroidUtilCode · error · UnsupportedOperationException

u can't instantiate me...

Error message

u can't instantiate me...

What it means

ProcessUtils is a final utility class providing static methods for process information (getForegroundProcessName, getCurrentProcessName, killBackgroundProcesses, etc.). Its private constructor throws UnsupportedOperationException to enforce static-only access.

Source

Thrown at lib/utilcode/src/main/java/com/blankj/utilcode/util/ProcessUtils.java:44

import java.util.Set;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresPermission;

import static android.Manifest.permission.KILL_BACKGROUND_PROCESSES;

/**
 * <pre>
 *     author: Blankj
 *     blog  : http://blankj.com
 *     time  : 2016/10/18
 *     desc  : utils about process
 * </pre>
 */
public final class ProcessUtils {

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

    /**
     * Return the foreground process name.
     * <p>Target APIs greater than 21 must hold
     * {@code <uses-permission android:name="android.permission.PACKAGE_USAGE_STATS" />}</p>
     *
     * @return the foreground process name
     */
    public static String getForegroundProcessName() {
        ActivityManager am =
                (ActivityManager) Utils.getApp().getSystemService(Context.ACTIVITY_SERVICE);
        //noinspection ConstantConditions
        List<ActivityManager.RunningAppProcessInfo> pInfo = am.getRunningAppProcesses();
        if (pInfo != null && pInfo.size() > 0) {
            for (ActivityManager.RunningAppProcessInfo aInfo : pInfo) {
                if (aInfo.importance
                        == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {

View on GitHub (pinned to 7b4caf9e54)

Solutions

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

Example fix

// before
ProcessUtils pu = new ProcessUtils();
String name = pu.getCurrentProcessName();

// after
String name = ProcessUtils.getCurrentProcessName();
Defensive patterns

Strategy: type-guard

Validate before calling

// ProcessUtils is static-only
String procName = ProcessUtils.getCurrentProcessName();
String fgName = ProcessUtils.getForegroundProcessName();

Type guard

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

Try / catch

try {
    ProcessUtils.class.getDeclaredConstructor().setAccessible(true);
    ProcessUtils.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; a serialization library targets the class.

Related errors


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