{"action":"created","issue":{"url":"https://api.github.com/repos/google/gson/issues/817","repository_url":"https://api.github.com/repos/google/gson","labels_url":"https://api.github.com/repos/google/gson/issues/817/labels{/name}","comments_url":"https://api.github.com/repos/google/gson/issues/817/comments","events_url":"https://api.github.com/repos/google/gson/issues/817/events","html_url":"https://github.com/google/gson/issues/817","id":143951850,"number":817,"title":"JNI Error in Android","user":{"login":"abhijith-gururaj","id":7941660,"avatar_url":"https://avatars.githubusercontent.com/u/7941660?v=3","gravatar_id":"","url":"https://api.github.com/users/abhijith-gururaj","html_url":"https://github.com/abhijith-gururaj","followers_url":"https://api.github.com/users/abhijith-gururaj/followers","following_url":"https://api.github.com/users/abhijith-gururaj/following{/other_user}","gists_url":"https://api.github.com/users/abhijith-gururaj/gists{/gist_id}","starred_url":"https://api.github.com/users/abhijith-gururaj/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/abhijith-gururaj/subscriptions","organizations_url":"https://api.github.com/users/abhijith-gururaj/orgs","repos_url":"https://api.github.com/users/abhijith-gururaj/repos","events_url":"https://api.github.com/users/abhijith-gururaj/events{/privacy}","received_events_url":"https://api.github.com/users/abhijith-gururaj/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":5,"created_at":"2016-03-28T11:06:53Z","updated_at":"2016-04-12T20:08:58Z","closed_at":null,"body":"I am getting a 'JNI DETECTED AN ERROR IN APPLICATION\" while using gson 2.5.jar in Android. Can anyone tell me how to solve this? Appreciate it.\r\nI am using a sub-class of Parse Object using Parse SDK.\r\nHere is the stack trace: \r\n\r\n```\r\n03-28 14:01:15.445: E/art(28683): JNI DETECTED ERROR IN APPLICATION: can't make objects of type java.util.concurrent.locks.Lock: 0x70dd7858\r\n03-28 14:01:15.445: E/art(28683):     in call to AllocObject\r\n03-28 14:01:15.445: E/art(28683):     from java.lang.Object sun.misc.Unsafe.allocateInstance(java.lang.Class)\r\n03-28 14:01:15.445: E/art(28683): \"main\" prio=5 tid=1 Runnable\r\n03-28 14:01:15.445: E/art(28683):   | group=\"main\" sCount=0 dsCount=0 obj=0x7617aef8 self=0x7fa917c400\r\n03-28 14:01:15.445: E/art(28683):   | sysTid=28683 nice=0 cgrp=default sched=0/0 handle=0x7face5d2c0\r\n03-28 14:01:15.445: E/art(28683):   | state=R schedstat=( 657346072 17671229 611 ) utm=52 stm=13 core=1 HZ=100\r\n03-28 14:01:15.445: E/art(28683):   | stack=0x7fe28ac000-0x7fe28ae000 stackSize=8MB\r\n03-28 14:01:15.445: E/art(28683):   | held mutexes= \"mutator lock\"(shared held)\r\n03-28 14:01:15.445: E/art(28683):   at sun.misc.Unsafe.allocateInstance(Native method)\r\n03-28 14:01:15.445: E/art(28683):   at java.lang.reflect.Method.invoke!(Native method)\r\n03-28 14:01:15.445: E/art(28683):   at com.google.gson.internal.UnsafeAllocator$1.newInstance(UnsafeAllocator.java:48)\r\n03-28 14:01:15.445: E/art(28683):   at com.google.gson.internal.ConstructorConstructor$14.construct(ConstructorConstructor.java:223)\r\n03-28 14:01:15.445: E/art(28683):   at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:207)\r\n03-28 14:01:15.445: E/art(28683):   at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:117)\r\n03-28 14:01:15.445: E/art(28683):   at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:217)\r\n03-28 14:01:15.445: E/art(28683):   at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:117)\r\n03-28 14:01:15.445: E/art(28683):   at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:217)\r\n03-28 14:01:15.445: E/art(28683):   at com.google.gson.Gson.fromJson(Gson.java:861)\r\n03-28 14:01:15.445: E/art(28683):   at com.google.gson.Gson.fromJson(Gson.java:826)\r\n03-28 14:01:15.445: E/art(28683):   at com.google.gson.Gson.fromJson(Gson.java:775)\r\n03-28 14:01:15.445: E/art(28683):   at com.google.gson.Gson.fromJson(Gson.java:747)\r\n03-28 14:01:15.445: E/art(28683):   at b5.project.medibro.FeedItemDetails.onCreate(FeedItemDetails.java:47)\r\n03-28 14:01:15.445: E/art(28683):   at android.app.Activity.performCreate(Activity.java:6583)\r\n03-28 14:01:15.445: E/art(28683):   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1114)\r\n03-28 14:01:15.445: E/art(28683):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2531)\r\n03-28 14:01:15.445: E/art(28683):   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2666)\r\n03-28 14:01:15.445: E/art(28683):   at android.app.ActivityThread.-wrap11(ActivityThread.java:-1)\r\n03-28 14:01:15.445: E/art(28683):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1493)\r\n03-28 14:01:15.445: E/art(28683):   at android.os.Handler.dispatchMessage(Handler.java:111)\r\n03-28 14:01:15.445: E/art(28683):   at android.os.Looper.loop(Looper.java:207)\r\n03-28 14:01:15.445: E/art(28683):   at android.app.ActivityThread.main(ActivityThread.java:5769)\r\n03-28 14:01:15.445: E/art(28683):   at java.lang.reflect.Method.invoke!(Native method)\r\n03-28 14:01:15.445: E/art(28683):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)\r\n03-28 14:01:15.445: E/art(28683):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)\r\n```\r\n\r\nThe error is occurring at the last line of this code:\r\n```\r\n        gson=new Gson();\r\n        String jsonObj=getIntent().getStringExtra(\"feedItem\");\r\n        item = gson.fromJson(jsonObj, FeedItem.class);\r\n```\r\n\r\nI have logged the json string which I am getting. The formatted json string is \r\n```\r\n{\r\n   \"feedItemChannel\":\"mdldsrgXN1\",\r\n   \"estimatedData\":{\r\n      \"feedTopic\":\"Testing\",\r\n      \"feedComments\":2,\r\n      \"createdBy\":\"KXTQtpfBSW\",\r\n      \"feedQuestion\":\"Test Question \",\r\n      \"feedDesc\":\"Test \"\r\n   },\r\n   \"hashedObjects\":{\r\n\r\n   },\r\n   \"isDeleted\":false,\r\n   \"isDeletingEventually\":0,\r\n   \"mutex\":{\r\n\r\n   },\r\n   \"operationSetQueue\":[\r\n      {\r\n\r\n      }\r\n   ],\r\n   \"saveEvent\":{\r\n      \"callbacks\":[\r\n\r\n      ]\r\n   },\r\n   \"state\":{\r\n      \"className\":\"FeedItem\",\r\n      \"createdAt\":1458798818385,\r\n      \"isComplete\":true,\r\n      \"objectId\":\"mdldsrgXN1\",\r\n      \"serverData\":{\r\n         \"feedTopic\":\"TestTopic\",\r\n         \"createdBy\":\"KXTQtpfBSW\",\r\n         \"feedComments\":2,\r\n         \"feedQuestion\":\"Test Question \",\r\n         \"feedDesc\":\"Test \"\r\n      },\r\n      \"updatedAt\":1458803553636\r\n   },\r\n   \"taskQueue\":{\r\n      \"lock\":{\r\n         \"sync\":{\r\n            \"state\":0\r\n         }\r\n      }\r\n   }\r\n}\r\n```"},"comment":{"url":"https://api.github.com/repos/google/gson/issues/comments/209083324","html_url":"https://github.com/google/gson/issues/817#issuecomment-209083324","issue_url":"https://api.github.com/repos/google/gson/issues/817","id":209083324,"user":{"login":"ionspin","id":144139,"avatar_url":"https://avatars.githubusercontent.com/u/144139?v=3","gravatar_id":"","url":"https://api.github.com/users/ionspin","html_url":"https://github.com/ionspin","followers_url":"https://api.github.com/users/ionspin/followers","following_url":"https://api.github.com/users/ionspin/following{/other_user}","gists_url":"https://api.github.com/users/ionspin/gists{/gist_id}","starred_url":"https://api.github.com/users/ionspin/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ionspin/subscriptions","organizations_url":"https://api.github.com/users/ionspin/orgs","repos_url":"https://api.github.com/users/ionspin/repos","events_url":"https://api.github.com/users/ionspin/events{/privacy}","received_events_url":"https://api.github.com/users/ionspin/received_events","type":"User","site_admin":false},"created_at":"2016-04-12T20:08:58Z","updated_at":"2016-04-12T20:08:58Z","body":"It seems to behaves differently based on the underlying Android implementation, I used a simple test app:\r\n\r\n```java\r\npublic interface A {\r\n        void method();\r\n    }\r\n\r\n...\r\nUnsafeAllocator unsafeAllocator = UnsafeAllocator.create();\r\n        Log.d(TAG, \"Try to allocate interface\");\r\n        A a = null;\r\n        try {\r\n            a = unsafeAllocator.newInstance(A.class);\r\n        } catch (Exception e) {\r\n            Log.e(TAG, \"\", e);\r\n        }\r\n        Log.d(TAG, \"Try to call a method on allocated interface\");\r\n        a.method();\r\n        Log.d(TAG, \"Method called\");\r\n...\r\n```\r\nOn Nexus 5X running Android N preview 1 that would cause VM to abort and report JNI error, then try to restart the app and immediately crash again entering a infinite loop\r\n\r\n```\r\n04-12 21:25:00.284 11396-11396/ionspin.com.unsafetest D/MainActivity: Try to allocate interface\r\n04-12 21:25:00.294 11396-11396/ionspin.com.unsafetest A/art: art/runtime/java_vm_ext.cc:468] JNI DETECTED ERROR IN APPLICATION: can't make objects of type ionspin.com.unsafetest.MainActivity$A: 0x12d79380\r\n04-12 21:25:00.295 11396-11396/ionspin.com.unsafetest A/art: art/runtime/java_vm_ext.cc:468]     in call to AllocObject\r\n04-12 21:25:00.295 11396-11396/ionspin.com.unsafetest A/art: art/runtime/java_vm_ext.cc:468]     from java.lang.Object sun.misc.Unsafe.allocateInstance(java.lang.Class)\r\n04-12 21:25:00.295 11396-11396/ionspin.com.unsafetest A/art: art/runtime/java_vm_ext.cc:468] \"main\" prio=5 tid=1 Runnable\r\n```\r\n\r\nBut on Nexus 5 running Android 5.1.1, instantiation would succeed, while a method call would cause a segmentation fault\r\n```\r\n04-12 21:31:57.317 27351-27351/? D/MainActivity: Try to allocate interface\r\n04-12 21:31:57.317 27351-27351/? D/MainActivity: Try to call a method on allocated interface\r\n04-12 21:31:57.317 27351-27351/? A/libc: Fatal signal 11 (SIGSEGV), code 1, fault addr 0x2c in tid 27351 (.com.unsafetest)\r\n04-12 21:31:57.430 8534-8534/? I/DEBUG: *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***\r\n04-12 21:31:57.430 8534-8534/? I/DEBUG: Build fingerprint: 'google/hammerhead/hammerhead:5.1.1/LMY48M/2167285:user/release-keys'\r\n04-12 21:31:57.430 8534-8534/? I/DEBUG: Revision: '11'\r\n04-12 21:31:57.430 8534-8534/? I/DEBUG: ABI: 'arm'\r\n04-12 21:31:57.430 8534-8534/? I/DEBUG: pid: 27351, tid: 27351, name: .com.unsafetest  >>> ionspin.com.unsafetest <<<\r\n04-12 21:31:57.430 8534-8534/? I/DEBUG: signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x2c\r\n```\r\n\r\nWhile on LG D772 (G3 mini or something like that running 4.4.2) it actually threw the exception\r\n\r\n```\r\n04-12 21:38:04.219 7803-7803/ionspin.com.unsafetest D/MainActivity: Try to allocate interface\r\n04-12 21:38:04.239 7803-7803/ionspin.com.unsafetest E/MainActivity: java.lang.reflect.InvocationTargetException\r\n                                                                        at java.lang.reflect.Method.invokeNative(Native Method)\r\n                                                                        at java.lang.reflect.Method.invoke(Method.java:515)\r\n                                                                        at com.google.gson.internal.UnsafeAllocator$1.newInstance(UnsafeAllocator.java:48)\r\n```\r\n\r\nUnfortunately I don't have a device running Marshmallow to test on currently\r\n\r\nWhile I completely agree that either a transient modifier or a TypeAdapter implementation is the right solution for this problem, maybe adding a check if the class modifiers contain interface or abstract and throwing a RuntimeException could help spotting this kind of issue quicker. Especially on some versions of Android where the only feedback would be a segmentation fault.\r\n"}}