Blankj/AndroidUtilCode · error · UnsupportedOperationException

u can't instantiate me...

Error message

u can't instantiate me...

What it means

ResourceUtils is a final utility class providing static methods for Android resource access (getDrawable, readFileFromAssets, copyFileFromAssets, readStringFromAssets, etc.). Its private constructor throws UnsupportedOperationException to enforce static-only access.

Source

Thrown at lib/utilcode/src/main/java/com/blankj/utilcode/util/ResourceUtils.java:28

import androidx.annotation.DrawableRes;
import androidx.annotation.RawRes;
import androidx.core.content.ContextCompat;

/**
 * <pre>
 *     author: Blankj
 *     blog  : http://blankj.com
 *     time  : 2018/05/07
 *     desc  : utils about resource
 * </pre>
 */
public final class ResourceUtils {

    private static final int BUFFER_SIZE = 8192;

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

    /**
     * Return the drawable by identifier.
     *
     * @param id The identifier.
     * @return the drawable by identifier
     */
    public static Drawable getDrawable(@DrawableRes int id) {
        return ContextCompat.getDrawable(Utils.getApp(), id);
    }

    /**
     * Return the id identifier by name.
     *
     * @param name The name of id.
     * @return the id identifier by name
     */

View on GitHub (pinned to 7b4caf9e54)

Solutions

  1. Use static methods: ResourceUtils.getDrawable(id), ResourceUtils.readFileFromAssets(name). Never call new ResourceUtils().
  2. Exclude utility classes from DI scanning and serialization configs.
  3. Mock static methods with mockito-inline for testing.

Example fix

// before
ResourceUtils ru = new ResourceUtils();
Drawable d = ru.getDrawable(R.drawable.icon);

// after
Drawable d = ResourceUtils.getDrawable(R.drawable.icon);
Defensive patterns

Strategy: type-guard

Validate before calling

// ResourceUtils is static-only
Drawable d = ResourceUtils.getDrawable(R.drawable.icon);
String content = ResourceUtils.readFileFromAssets("config.json");

Type guard

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

Try / catch

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

Related errors


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