Blankj/AndroidUtilCode · warning · UnsupportedOperationException

u can't instantiate me...

Error message

u can't instantiate me...

What it means

AdaptScreenUtils is a final utility class with a private constructor that throws UnsupportedOperationException. It holds only static screen-adaptation helpers and (optionally) a static field cache; instances are non-constructible.

Source

Thrown at lib/utilcode/src/main/java/com/blankj/utilcode/util/AdaptScreenUtils.java:25

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;

/**
 * <pre>
 *     author: Blankj
 *     blog  : http://blankj.com
 *     time  : 2018/11/15
 *     desc  : utils about adapt screen
 * </pre>
 */
public final class AdaptScreenUtils {

    private static List<Field> sMetricsFields;

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

    /**
     * Adapt for the horizontal screen, and call it in {@link android.app.Activity#getResources()}.
     */
    @NonNull
    public static Resources adaptWidth(@NonNull final Resources resources, final int designWidth) {
        float newXdpi = (resources.getDisplayMetrics().widthPixels * 72f) / designWidth;
        applyDisplayMetrics(resources, newXdpi);
        return resources;
    }

    /**
     * Adapt for the vertical screen, and call it in {@link android.app.Activity#getResources()}.
     */
    @NonNull
    public static Resources adaptHeight(@NonNull final Resources resources, final int designHeight) {
        return adaptHeight(resources, designHeight, false);

View on GitHub (pinned to 7b4caf9e54)

Solutions

  1. Call the static methods, e.g. AdaptScreenUtils.adaptWidth(res, designWidth) / closeAdapt(res); never instantiate.
  2. Configure reflective frameworks to skip final utility classes.
  3. Remove newInstance paths targeting AdaptScreenUtils.

Example fix

// before
AdaptScreenUtils a = reflectNew(AdaptScreenUtils.class); // throws

// after
AdaptScreenUtils.adaptWidth(getResources(), 1080);
Defensive patterns

Strategy: validation

Validate before calling

// AdaptScreenUtils is static-only; never instantiate.
AdaptScreenUtils.adaptWidth(getResources(), 1080);

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 AdaptScreenUtils instead of using its static methods.

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

Related errors


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