JessYanCoding/AndroidAutoSize · error · java.lang.IllegalStateException

you can't instantiate me!

Error message

you can't instantiate me!

What it means

AutoSize is a static utility class for the AndroidAutoSize screen-adaptation library; its constructor is intentionally private and throws IllegalStateException to forbid instantiation. All entry points (AutoSize.init, autoConvertDensity, etc.) are static, so creating an instance is always a programming mistake.

Solutions

  1. Use the static methods directly, e.g. AutoSize.initCompat(application), never construct the class
  2. Remove any 'new AutoSize()' call or reflection-based instantiation
  3. Wrap reflective instantiation in try-catch or skip classes that are static utility holders

Example fix

// before
AutoSize autoSize = new AutoSize();
// after
AutoSize.initCompat(application);
Defensive patterns

Strategy: try-catch

Validate before calling

if (Modifier.isPrivate(AutoSize.class.getDeclaredConstructor().getModifiers())) { /* utility class: use statics */ }

Type guard

boolean isUtilityClass(Class<?> c) { return Modifier.isPrivate(c.getDeclaredConstructors()[0].getModifiers()); }

Try / catch

try { ctor = AutoSize.class.getDeclaredConstructor(); ctor.setAccessible(true); ctor.newInstance(); } catch (InvocationTargetException e) { Throwable cause = e.getCause(); /* IllegalStateException: intended design, use static methods */ }

Prevention

When it happens

Trigger: Calling new AutoSize() (directly or via reflection) instead of using the static methods on the class.

Common situations: Developers copying 'new SomeHelper()' patterns, dependency-injection containers or code generators attempting to instantiate every class, or reflection-based tests instantiating classes with private constructors.

Related errors


AI-assisted analysis of JessYanCoding/AndroidAutoSize@e402ecdd99 (2026-09-07). Data as JSON: /api/errors/4acd6f622d019772. Report an issue: GitHub.

Appendix: source

Thrown at autosize/src/main/java/me/jessyan/autosize/AutoSize.java:60

 * 自定义 {@link View} 都会达到适配的效果, 如果某个页面不想使用适配请让该 {@link Activity} 实现 {@link CancelAdapt}
 * <p>
 * 任何方案都不可能完美, 在成本和收益中做出取舍, 选择出最适合自己的方案即可, 在没有更好的方案出来之前, 只有继续忍耐它的不完美, 或者自己作出改变
 * 既然选择, 就不要抱怨, 感谢 今日头条技术团队 和 张鸿洋 等人对 Android 屏幕适配领域的的贡献
 * <p>
 * Created by JessYan on 2018/8/8 19:20
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * ================================================
 */
public final class AutoSize {
    private static SparseArray<DisplayMetricsInfo> mCache = new SparseArray<>();
    private static final int MODE_SHIFT = 30;
    private static final int MODE_MASK  = 0x3 << MODE_SHIFT;
    private static final int MODE_ON_WIDTH  = 1 << MODE_SHIFT;
    private static final int MODE_DEVICE_SIZE  = 2 << MODE_SHIFT;

    private AutoSize() {
        throw new IllegalStateException("you can't instantiate me!");
    }

    /**
     * 检查 AndroidAutoSize 是否已经初始化
     *
     * @return {@code false} 表示 AndroidAutoSize 还未初始化, {@code true} 表示 AndroidAutoSize 已经初始化
     */
    public static boolean checkInit() {
        return AutoSizeConfig.getInstance().getInitDensity() != -1;
    }

    /**
     * 由于 AndroidAutoSize 会通过 {@link InitProvider} 的实例化而自动完成初始化, 并且 {@link AutoSizeConfig#init(Application)}
     * 只允许被调用一次, 否则会报错, 所以 {@link AutoSizeConfig#init(Application)} 的调用权限并没有设为 public, 不允许外部使用者调用
     * 但由于某些 issues 反应, 可能会在某些特殊情况下出现 {@link InitProvider} 未能正常实例化的情况, 导致 AndroidAutoSize 未能完成初始化
     * 所以提供此静态方法用于让外部使用者在异常情况下也可以初始化 AndroidAutoSize, 在 {@link Application#onCreate()} 中调用即可
     *
     * @param application {@link Application}

View on GitHub (pinned to e402ecdd99)