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
- Add a no-args constructor to the class (preferred)
- Register an InstanceCreator for the type
- Register a custom TypeAdapter for the type
- If R8 removed constructors, add keep rules: -keep class com.example.MyClass { <init>(); }
- 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
- With disableJdkUnsafe(), ensure every deserialized class has a no-args constructor
- Register InstanceCreators for classes that can't have no-args constructors
- Add R8/ProGuard keep rules: -keep class com.example.** { <init>(); }
- Run a startup self-test that attempts construction of all model types
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
- Unable to create instance of ${rawType}; Register an Instanc
- Unable to create instance of ${rawType}. Registering an Inst
- Failed to invoke constructor '${ReflectionHelper.constructor
- Interfaces can't be instantiated! Register an InstanceCreato
- Cannot allocate ${c}. Usage of JDK sun.misc.Unsafe is enable
AI-assisted analysis of google/gson@310ac341f2 (2026-08-10).
Data as JSON: /api/errors/d2254194f51e2db8.
Report an issue: GitHub.