Open REInVent650 opened 2 years ago
I did find a workaround for some Android 11 devices, by making this empty class.
public class MyType extends ArrayList<HashMap<String, String>> {
}
then when using from Json I pass it in as the type.
logData = new Gson().fromJson(filePath, MyType.class);
Are you using ProGuard or another code-shrinking solution? Can you say whether the same problem occurs when you don't?
Sorry for necro'ing, but we believe that we have also hit a similar issue.
code (Kotlin):
val sessions = gson.fromJson<MutableMap<Int, Triple<UserModel?, Token?, Boolean?>>>(
sessionsStr,
object : TypeToken<MutableMap<Int, Triple<UserModel, Token, Boolean>>>() {}.type)
exception:
2022-12-06 10:09:33.591 11381-11381/? W/System.err: ka.w: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BOOLEAN at line 1 column 827 path $..c
2022-12-06 10:09:33.591 11381-11381/? W/System.err: at na.n$a.read(ReflectiveTypeAdapterFactory.java:14)
2022-12-06 10:09:33.591 11381-11381/? W/System.err: at na.m.b(ReflectiveTypeAdapterFactory.java:1)
2022-12-06 10:09:33.591 11381-11381/? W/System.err: at na.n$c.c(Unknown Source:0)
2022-12-06 10:09:33.591 11381-11381/? W/System.err: at na.n$a.read(ReflectiveTypeAdapterFactory.java:9)
2022-12-06 10:09:33.591 11381-11381/? W/System.err: at na.q.read(Unknown Source:2)
2022-12-06 10:09:33.591 11381-11381/? W/System.err: at na.h$a.read(MapTypeAdapterFactory.java:21)
2022-12-06 10:09:33.591 11381-11381/? W/System.err: at ka.i.b(Gson.java:5)
2022-12-06 10:09:33.591 11381-11381/? W/System.err: at ka.i.e(Gson.java:4)
2022-12-06 10:09:33.591 11381-11381/? W/System.err: at ka.i.d(Unknown Source:4)
2022-12-06 10:09:33.591 11381-11381/? W/System.err: at df.y0.invoke(RootInteractor.kt:39)
2022-12-06 10:09:33.591 11381-11381/? W/System.err: at bf.b.accept(R8$$SyntheticClass:71)
2022-12-06 10:09:33.591 11381-11381/? W/System.err: at io.reactivex.internal.observers.s.onNext(LambdaObserver.java:2)
2022-12-06 10:09:33.591 11381-11381/? W/System.err: at io.reactivex.internal.operators.observable.q2$a.run(ObservableObserveOn.java:23)
2022-12-06 10:09:33.591 11381-11381/? W/System.err: at io.reactivex.android.schedulers.b$b.run(HandlerScheduler.java:1)
2022-12-06 10:09:33.591 11381-11381/? W/System.err: at android.os.Handler.handleCallback(Handler.java:942)
2022-12-06 10:09:33.591 11381-11381/? W/System.err: at android.os.Handler.dispatchMessage(Handler.java:99)
2022-12-06 10:09:33.591 11381-11381/? W/System.err: at android.os.Looper.loopOnce(Looper.java:201)
2022-12-06 10:09:33.591 11381-11381/? W/System.err: at android.os.Looper.loop(Looper.java:288)
2022-12-06 10:09:33.591 11381-11381/? W/System.err: at android.app.ActivityThread.main(ActivityThread.java:7898)
2022-12-06 10:09:33.591 11381-11381/? W/System.err: at java.lang.reflect.Method.invoke(Native Method)
2022-12-06 10:09:33.591 11381-11381/? W/System.err: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
2022-12-06 10:09:33.591 11381-11381/? W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)
2022-12-06 10:09:33.591 11381-11381/? W/System.err: Caused by: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BOOLEAN at line 1 column 827 path $..c
2022-12-06 10:09:33.591 11381-11381/? W/System.err: at com.google.gson.stream.JsonReader.beginObject(JsonReader.java:7)
2022-12-06 10:09:33.591 11381-11381/? W/System.err: at na.n$a.read(ReflectiveTypeAdapterFactory.java:4)
2022-12-06 10:09:33.591 11381-11381/? W/System.err: ... 21 more
@luiges90, personally I would treat that as separate issue initially because the stack traces are so different. Could you please create a separate issue for this?
Also, could you please make sure you have configured ProGuard / R8 correctly so it does not obfuscate the field names of your classes. Maybe also try using GsonBuilder.addReflectionAccessFilter(ReflectionAccessFilter.BLOCK_ALL_PLATFORM)
(requires Gson 2.9.1 or newer) to be sure you don't serialize any Android classes by accident.
Though might be good to further discuss this in a separate GitHub issue.
Gson version
2.9.0 and 2.8.7
Java / Android version
Android 11 & 12 Java 8
Used tools
Description
NullPointerException when trying to add a HashMap to an ArrayList that Gson has initialized with or without data from a json file.
Expected behavior
A HashMap to be added to the ArrayList
Actual behavior
A NullPointerException occured indicating the ArrayList was either null or not of the expected type on Android 11 and 12. Android 10 and below A HashMap is succesfully added to the ArrayList that Gson initialized.
Reproduction steps
Make sure you are using android 11 or 12
Make a new HashMap add it to the ArrayList that Gson has initialized
Exception stack trace
I will try and put a MRPE together tomorrow