0

I am getting the following in my application:

StrictMode policy violation: android.os.strictmode.NonSdkApiUsedViolation: Lsun/misc/Unsafe;->theUnsafe:Lsun/misc/Unsafe;
    at android.os.StrictMode.lambda$static$1(StrictMode.java:428)
    at android.os.-$$Lambda$StrictMode$lu9ekkHJ2HMz0jd3F8K8MnhenxQ.accept(Unknown Source:2)
    at java.lang.Class.getDeclaredField(Native Method)
    at com.google.gson.internal.UnsafeAllocator.create(Unknown Source:13)
    at com.google.gson.internal.ConstructorConstructor$14.<init>(Unknown Source:9)
    at com.google.gson.internal.ConstructorConstructor.newUnsafeAllocator(Unknown Source:2)
    at com.google.gson.internal.ConstructorConstructor.get(Unknown Source:54)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(Unknown Source:16)
    at com.google.gson.Gson.getAdapter(Unknown Source:74)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(Unknown Source:42)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(Unknown Source:123)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(Unknown Source:22)
    at com.google.gson.Gson.getAdapter(Unknown Source:74)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(Unknown Source:42)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(Unknown Source:123)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(Unknown Source:22)
    at com.google.gson.Gson.getAdapter(Unknown Source:74)
    at com.google.gson.Gson.fromJson(Unknown Source:16)
    at com.google.gson.Gson.fromJson(Unknown Source:4)
    at com.google.gson.Gson.fromJson(Unknown Source:9)
    at com.google.gson.Gson.fromJson(Unknown Source:0)
    at com.wefi.sdk.common.GsonConvert.readFromJson(Unknown Source:26)

From what I have seen I need to add disableJdkUnsafe() to my GsonBuilder call. I tried that but I get unknown symbol.

I am using Gson version 2.8.5 but also tried it with 2.8.9. What am I missing?

theblitz
  • 6,683
  • 16
  • 60
  • 114
  • 1
    It hasn't been released yet: https://github.com/google/gson/compare/gson-parent-2.8.9..master (master=47dea2eefc6d2816cddb3f30689070285491733c as of today). If I'm not mistaken, you can work around this issue by registering an `InstanceCreator` (the implementation can be compile-time and runtime-based). – terrorrussia-keeps-killing Feb 06 '22 at 12:51
  • Or you could solve this by adding a no-args constructor (and make sure that nested classes are `static`) or you might have to implement a custom [`TypeAdapter`](https://javadoc.io/doc/com.google.code.gson/gson/latest/com.google.gson/com/google/gson/TypeAdapter.html), it really depends on which class you are deserializing. With more information we can probably provide more useful recommendations. – Marcono1234 Feb 06 '22 at 16:40
  • `disableJdkUnsafe()` would not help you here, the only difference would be that you get a more helpful exception message, because instance creation using `Unsafe` is the fallback in case nothing else worked. `disableJdkUnsafe()` is mainly intended for the cases where you don't have any StrictMode checks, and might by accident depend on `Unsafe` without even being aware of it. – Marcono1234 Feb 06 '22 at 16:41

0 Answers0