google/gson · error · JsonIOException

Unable to create instance of ${rawType}; usage of JDK Unsafe

Error message

Unable to create instance of ${rawType}; usage of JDK Unsafe is disabled. Registering an InstanceCreator or a TypeAdapter for this type, adding a no-args constructor, or enabling usage of JDK Unsafe may fix this problem.

What it means

Thrown when GsonBuilder.disableJdkUnsafe() has been called (useJdkUnsafe=false), the type has no accessible no-args constructor, no InstanceCreator, and no special/default-implementation constructor applies. Unsafe would be the only remaining option but it is disabled. If R8 removed all constructors, the message appends an R8-specific hint.

Source

Thrown at gson/src/main/java/com/google/gson/internal/ConstructorConstructor.java:424

   * ObjectConstructor}, which would then choose another way of creating the object. And it supports
   * types which are only serialized but not deserialized (compared to directly throwing an
   * exception when the {@code ObjectConstructor} is requested), e.g. when the runtime type of an
   * object is inaccessible, but the compile-time type is accessible.
   */
  private static final class ThrowingObjectConstructor<T> implements ObjectConstructor<T> {
    private final String exceptionMessage;

    ThrowingObjectConstructor(String exceptionMessage) {
      this.exceptionMessage = exceptionMessage;
    }

    @Override
    public T construct() {
      // New exception is created every time to avoid keeping a reference to an exception with
      // potentially long stack trace, causing a memory leak
      // (which would happen if the exception was already created when the
      // `ThrowingObjectConstructor` is created)
      throw new JsonIOException(exceptionMessage);
    }
  }

  private static final class InstanceCreatorConstructor<T> implements ObjectConstructor<T> {
    private final InstanceCreator<T> instanceCreator;
    private final Type type;

    InstanceCreatorConstructor(InstanceCreator<T> instanceCreator, Type type) {
      this.instanceCreator = instanceCreator;
      this.type = type;
    }

    @Override
    public T construct() {
      return instanceCreator.createInstance(type);
    }
  }
}

View on GitHub (pinned to 310ac341f2)

Solutions

  1. Add a no-args constructor to the class (preferred)
  2. Register an InstanceCreator for the type
  3. Register a custom TypeAdapter for the type
  4. If R8 removed constructors, add keep rules: -keep class com.example.MyClass { <init>(); }
  5. Re-enable Unsafe by removing disableJdkUnsafe() (less safe, not recommended for production)

Example fix

// before
gsonBuilder.disableJdkUnsafe();
class Foo { public Foo(String s) {} } // no no-args ctor

// after
class Foo { public Foo() {} public Foo(String s) {} }
Defensive patterns

Strategy: validation

Validate before calling

// When using disableJdkUnsafe(), verify no-args constructors at startup
Class<?> c = MyType.class;
boolean hasNoArgs;
try { c.getDeclaredConstructor(); hasNoArgs = true; }
catch (NoSuchMethodException e) { hasNoArgs = false; }
if (!hasNoArgs && !hasInstanceCreator(c)) {
  throw new IllegalStateException(c + " has no no-args ctor and Unsafe is disabled; add ctor or InstanceCreator");
}

Try / catch

try {
  return gson.fromJson(json, MyType.class);
} catch (JsonIOException e) {
  if (e.getMessage().contains("usage of JDK Unsafe is disabled")) {
    // add no-args ctor, register InstanceCreator, or re-enable Unsafe
    throw new MyParseException("Unsafe disabled and no ctor for " + MyType.class, e);
  }
  throw e;
}

Prevention

When it happens

Trigger: Gson configured with GsonBuilder.disableJdkUnsafe(); deserializing a class without a no-args constructor and without a registered InstanceCreator/TypeAdapter. The newUnsafeAllocator path returns a ThrowingObjectConstructor because useJdkUnsafe is false.

Common situations: Applications that disable Unsafe for safety/predictability (recommended for Android and JDK 16+); test environments requiring explicit constructors; R8 stripping constructors on top of disabled Unsafe.

Related errors


AI-assisted analysis of google/gson@310ac341f2 (2026-08-10). Data as JSON: /api/errors/d2254194f51e2db8. Report an issue: GitHub.