shau-lok / google-gson

Automatically exported from code.google.com/p/google-gson
0 stars 0 forks source link

Reflection error using Desire HD with Froyo (2.2) #255

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Using any nested beans, look at the TwitterTrends GSON example on the net
2. Only happens using desire HD with Froyo 2.2
3.

What is the expected output? What do you see instead?

Get a stacktrace saying cannot find the class declaration for the nested type

What version of the product are you using? On what operating system?

From memory thing it's GSON 1.5 on a HTC desire HD with 2.2

Please provide any additional information below.

Works on HTC Magic with 1.6, Emulator with 2.2 and Nexus One with 2.2.1

Original issue reported on code.google.com by dorian.c...@gtempaccount.com on 26 Oct 2010 at 9:21

GoogleCodeExporter commented 9 years ago
I have developed an app wich uses  the GSON library. It works fine on all 
phones except the desire HD.

My app has a quite complex nested object structure, so I have created an apk 
from the TwitterTrends example Dorian mentioned.

This example works fine in the Emulator.

However, it crashes on the Desire HD. See attached files for stacktrace and 
source.

Original comment by patrickv...@gmail.com on 30 Oct 2010 at 10:09

Attachments:

GoogleCodeExporter commented 9 years ago
If this is a big, it's probably an Android bug and not a GSON bug.

Firstly, does the bug still occur if you force that class to load before you 
use GSON? Ie. if you put this somewhere in your app's initialization:
  Class.forName("pvc.example.twittertrends.model.TwitterTrend");

One theory is that there might be a problem with the class loader used by 
Field.getGenericType(). I have a guess that this might solve it:
    Thread.currentThread().setContextClassLoader(getClass().getClassLoader());

It's weird that this works differently on two different 2.2 devices; it should 
be the same code. Whatever your results, please update this bug so I can track 
it; my team is responsible for Android's reflection code.

Original comment by limpbizkit on 31 Oct 2010 at 2:25

GoogleCodeExporter commented 9 years ago
@limpbizkit
I tested your suggestions and these are my findings:

1) putting Class.forName("pvc.example.twittertrends.model.TwitterTrend"); in 
the onCreate works. It does not generate any error. But the programm still 
crashes when trying to deserialize.

2) putting 
Thread.currentThread().setContextClassLoader(getClass().getClassLoader()); in 
the onCreate doesn't seem to do anything. The programm still crashes when 
trying to deserialize. 

3) I also initialized and filled the TrendingTopics object with dummy data and 
tried to serialize it but that also fails.

4) Serializing an deserializing a String works. Example:
String toGson = new String("Random text");

Gson gson = new Gson();
String gsonString = gson.toJson(toGson);

String fromGson = gson.fromJson(gsonString, String.class);

I tested on a HTC Desire HD and a Samsung Galaxy Tab
On the Tab everything works fine.

Original comment by D.J.Span...@gmail.com on 1 Nov 2010 at 1:22

GoogleCodeExporter commented 9 years ago

Original comment by limpbizkit on 1 Nov 2010 at 10:19

GoogleCodeExporter commented 9 years ago
Hi, in addition to the log posted above the following is logged upon install on 
a HTC Desire HD;

D/dalvikvm( 4038): DexOpt: 'Lcom/google/gson/ExclusionStrategy;' has an earlier 
definition; blocking out
D/dalvikvm( 4038): DexOpt: 
'Lcom/google/gson/AnonymousAndLocalClassExclusionStrategy;' has an earlier 
definition; blocking out
.....
D/dalvikvm( 4038): DexOpt: not verifying 'Lcom/google/gson/ExclusionStrategy;': 
multiple definitions
D/dalvikvm( 4038): DexOpt: not verifying 
'Lcom/google/gson/AnonymousAndLocalClassExclusionStrategy;': multiple 
definitions
definitions

Attached the full log.

Johan

Original comment by markupartist on 5 Nov 2010 at 2:27

Attachments:

GoogleCodeExporter commented 9 years ago
Update, renamed the gson package using jarjar[1] and can confirm that gson is 
now working on a HTC Desire HD. Looks like HTC is using gson internally and has 
made the library public?

Johan

[1] http://code.google.com/p/jarjar/

Original comment by markupartist on 5 Nov 2010 at 2:57

GoogleCodeExporter commented 9 years ago
That's great news; thanks for the update. I'll ask HTC to jarjar their own copy 
so app developers don't have to.

Original comment by limpbizkit on 6 Nov 2010 at 5:41

GoogleCodeExporter commented 9 years ago
FYI, this bug also manifests itself on the new MyTouch 4G on T-Mobile.  Exact 
same issue & solution.  HTC's gson is clobbering mine.

Original comment by mikepalu...@gmail.com on 21 Nov 2010 at 8:12

GoogleCodeExporter commented 9 years ago
I can confirm this for HTC Desire HD. The "fix" of renaming the gson package 
with jarjar also works, thx a lot guys....

If anyone else has this problem, here's a quick how-to fix it:
1) Download jarjar (http://code.google.com/p/jarjar/downloads/list)
2) Put jarjar-1.0.jar and gson-1.5.jar in the same folder
3) Create a new text file in this folder (rules.txt)
4) Write the following line in the textfile: rule com.google.gson.** 
com.google.myjson.@1
5) From the commandline, open jarjar with the command "java -jar jarjar.jar 
process rules.txt gson-1.5.jar myjson-1.5.jar"
6) Replace the gson library in your project with myjson and update the imports

Original comment by bgmei...@gmail.com on 22 Nov 2010 at 1:48

GoogleCodeExporter commented 9 years ago
Alternatively, you can incorporate the GSON source into your project and use a 
different namespace/package name.

This is a bigger issue than people realize, I think.  We have handsets in the 
wild now that we cannot guarantee will ever be patched to resolve this issue; 
essentially stock GSON binaries are now broken on Android 2.2.

Original comment by mikepalu...@gmail.com on 22 Nov 2010 at 1:58

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
Thanks for the help on this.  I was running an HTC Evo and not seeing the 
issue, but seeing reports of Force Closes on both the Desire and MyTouch due to 
TypeNotPresentExceptions.  Gonna give this a shot.

Original comment by bigonthe...@gmail.com on 3 Dec 2010 at 7:25

GoogleCodeExporter commented 9 years ago
BotP Dan, it's doesn't manifest on the Incredible, EVO, etc., it's only on 
newer HTC handsets like the MyTouch & Desire.  TypeNotPresent is exactly the 
issue I was running into.  Look through the stack trace, you'll see why.  
Incorporate the GSON source with a new package name or use jarjar on it, and 
you'll be set.

This is pretty bad, shame on HTC for this bug.

Original comment by mikepalu...@gmail.com on 3 Dec 2010 at 9:52

GoogleCodeExporter commented 9 years ago
I am experiancing this too... does anyone have any ideas why it happens? To me 
it seems like the reflector (TypeToken) fails to reflect the object that it's 
supposed to.

I think this would be pretty strait forward to fix.

Original comment by janu...@gmail.com on 24 Jan 2011 at 6:26

GoogleCodeExporter commented 9 years ago
janu...: follow the recommendation in comment number 9 and you'll be alright.

Original comment by johan.mj...@gmail.com on 24 Jan 2011 at 6:32

GoogleCodeExporter commented 9 years ago
thanks a lot, the how-to nailed it (although i am using gson 1.6 by now)

Original comment by stefan.r...@gmail.com on 2 Mar 2011 at 9:47

GoogleCodeExporter commented 9 years ago
I would like to confirm that I experienced this issue also on HTC Desire Z 
running Froyo with gson 1.6. I fixed it by using jarjar and the following steps 
- 

1) Download jarjar (http://code.google.com/p/jarjar/downloads/list)
2) Put jarjar-1.0.jar and gson-1.5.jar in the same folder
3) Create a new text file in this folder (rules.txt)
4) Write the following line in the textfile: rule com.google.gson.** 
com.google.myjson.@1
5) From the commandline, open jarjar with the command "java -jar jarjar.jar 
process rules.txt gson-1.5.jar myjson-1.5.jar"
6) Replace the gson library in your project with myjson and update the imports

Original comment by troubado...@gmail.com on 14 Mar 2011 at 3:55

GoogleCodeExporter commented 9 years ago
Issue 308 has been merged into this issue.

Original comment by limpbizkit on 12 Apr 2011 at 10:52

GoogleCodeExporter commented 9 years ago
Another option is to use Proguard on your Android project. However, ensure that 
your Java to JSON mapping classes are excluded in the proguard config.

Original comment by inder123 on 6 Jul 2011 at 12:32

GoogleCodeExporter commented 9 years ago
inder123 - can you give an example of what you mean by 'ensure that your Java 
to JSON mapping classes are excluded in the proguard config'. I'm using the 
modified gson library with jarjar, and I'm trying to build release, but it 
calls to gson.fromJSON do not work properly because of proguard. I've seen 
several fixes on the internet about how to handle this with native gson, but 
not after using the jarjar file to produce a myjson jar file.

Original comment by BrianEmo...@gmail.com on 12 Dec 2011 at 5:27

GoogleCodeExporter commented 9 years ago
Issue 391 has been merged into this issue.

Original comment by limpbizkit on 21 Dec 2011 at 8:07

GoogleCodeExporter commented 9 years ago
Issue 398 has been merged into this issue.

Original comment by limpbizkit on 5 Jan 2012 at 5:43

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
Issue 403 has been merged into this issue.

Original comment by limpbizkit on 31 Jan 2012 at 1:56

GoogleCodeExporter commented 9 years ago
See also http://code.google.com/p/android/issues/detail?id=23245

Original comment by jessewil...@google.com on 15 Feb 2012 at 7:23

GoogleCodeExporter commented 9 years ago
it is showin error in unzipping jarjar

Exception in thread "main" java.util.zip.ZipException: error in opening zip file
    at java.util.zip.ZipFile.open(Native Method)
    at java.util.zip.ZipFile.<init>(ZipFile.java:131)
    at java.util.jar.JarFile.<init>(JarFile.java:150)
    at java.util.jar.JarFile.<init>(JarFile.java:114)
    at com.tonicsystems.jarjar.ext_util.StandaloneJarProcessor.run(StandaloneJarProcessor.java:31)
    at com.tonicsystems.jarjar.Main.process(Main.java:94)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:616)
    at com.tonicsystems.jarjar.MainUtil.runMain(MainUtil.java:37)
    at com.tonicsystems.jarjar.Main.main(Main.java:50)

Original comment by nileshji...@gmail.com on 27 Jun 2012 at 5:55

GoogleCodeExporter commented 9 years ago
The suggestion provided 
[here](https://sites.google.com/site/gson/gson-on-android) does not work, and 
this bug is reproducible in most of Samsung's devices. 

Here I've repackaged to com.google.myjson as you can see in the trace : 

08-08 16:00:48.071: D/UNHANDLED_EXCEPTION(687): Caused by: 
java.lang.TypeNotPresentException: Type org.redpin.android.core.Map not present
08-08 16:00:48.071: D/UNHANDLED_EXCEPTION(687):     at 
org.apache.harmony.luni.lang.reflect.ImplForType.getRawType(ImplForType.java:64)
08-08 16:00:48.071: D/UNHANDLED_EXCEPTION(687):     at 
org.apache.harmony.luni.lang.reflect.ImplForType.getResolvedType(ImplForType.jav
a:73)
08-08 16:00:48.071: D/UNHANDLED_EXCEPTION(687):     at 
org.apache.harmony.luni.lang.reflect.ListOfTypes.resolveTypes(ListOfTypes.java:6
1)
08-08 16:00:48.071: D/UNHANDLED_EXCEPTION(687):     at 
org.apache.harmony.luni.lang.reflect.ListOfTypes.getResolvedTypes(ListOfTypes.ja
va:53)
08-08 16:00:48.071: D/UNHANDLED_EXCEPTION(687):     at 
org.apache.harmony.luni.lang.reflect.ImplForType.getResolvedType(ImplForType.jav
a:72)
08-08 16:00:48.071: D/UNHANDLED_EXCEPTION(687):     at 
org.apache.harmony.luni.lang.reflect.ListOfTypes.resolveTypes(ListOfTypes.java:6
1)
08-08 16:00:48.071: D/UNHANDLED_EXCEPTION(687):     at 
org.apache.harmony.luni.lang.reflect.ListOfTypes.getResolvedTypes(ListOfTypes.ja
va:53)
08-08 16:00:48.071: D/UNHANDLED_EXCEPTION(687):     at 
org.apache.harmony.luni.lang.reflect.ImplForType.getResolvedType(ImplForType.jav
a:72)
08-08 16:00:48.071: D/UNHANDLED_EXCEPTION(687):     at 
org.apache.harmony.luni.lang.reflect.ListOfTypes.resolveTypes(ListOfTypes.java:6
1)
08-08 16:00:48.071: D/UNHANDLED_EXCEPTION(687):     at 
org.apache.harmony.luni.lang.reflect.ListOfTypes.getResolvedTypes(ListOfTypes.ja
va:53)
08-08 16:00:48.071: D/UNHANDLED_EXCEPTION(687):     at 
org.apache.harmony.luni.lang.reflect.ImplForType.getResolvedType(ImplForType.jav
a:72)
08-08 16:00:48.071: D/UNHANDLED_EXCEPTION(687):     at 
org.apache.harmony.luni.lang.reflect.Types.getType(Types.java:29)
08-08 16:00:48.071: D/UNHANDLED_EXCEPTION(687):     at 
java.lang.Class.getGenericSuperclass(Class.java:882)
08-08 16:00:48.071: D/UNHANDLED_EXCEPTION(687):     at 
com.google.myjson.reflect.TypeToken.getSuperclassTypeParameter(TypeToken.java:89
)
08-08 16:00:48.071: D/UNHANDLED_EXCEPTION(687):     at 
com.google.myjson.reflect.TypeToken.<init>(TypeToken.java:65)
08-08 16:00:48.071: D/UNHANDLED_EXCEPTION(687):     at 
org.redpin.android.net.Request$6.<init>(Request.java:194)
08-08 16:00:48.071: D/UNHANDLED_EXCEPTION(687):     at 
org.redpin.android.net.Request.setTypes(Request.java:194)
08-08 16:00:48.071: D/UNHANDLED_EXCEPTION(687):     at 
org.redpin.android.net.Request.<init>(Request.java:83)
08-08 16:00:48.071: D/UNHANDLED_EXCEPTION(687):     at 
org.redpin.android.net.Request.<init>(Request.java:71)
08-08 16:00:48.071: D/UNHANDLED_EXCEPTION(687):     at 
org.redpin.android.net.home.RemoteEntityHome.performRequest(RemoteEntityHome.jav
a:112)
08-08 16:00:48.071: D/UNHANDLED_EXCEPTION(687):     at 
org.redpin.android.net.home.RemoteEntityHome.performRequest(RemoteEntityHome.jav
a:66)
08-08 16:00:48.071: D/UNHANDLED_EXCEPTION(687):     at 
org.redpin.android.net.home.MapRemoteHome.getMapList(MapRemoteHome.java:62)
08-08 16:00:48.071: D/UNHANDLED_EXCEPTION(687):     at 
org.redpin.android.net.SynchronizationManager.sync(SynchronizationManager.java:1
44)
08-08 16:00:48.071: D/UNHANDLED_EXCEPTION(687):     at 
org.redpin.android.net.SynchronizationManager.onCreate(SynchronizationManager.ja
va:61)
08-08 16:00:48.071: D/UNHANDLED_EXCEPTION(687):     at 
android.app.ActivityThread.handleCreateService(ActivityThread.java:2363)

Tested on the 4.1 Jelly Bean sdk and also seen on all of the 4.0.4 ICS Android 
devices I've tried with. 

Original comment by sentient...@gmail.com on 8 Aug 2012 at 10:38

GoogleCodeExporter commented 9 years ago
@sentient.lemon: something must be misconfigured on your end. Are you 
repackaging only Gson or everything?

Original comment by limpbizkit on 9 Aug 2012 at 4:54

GoogleCodeExporter commented 9 years ago
Yes only Gson was repackaged. Anyway 

I fixed this problem, but I'm not sure what made it work. The only thing 
different this time was that I used JDK and JRE 1.6 instead of 1.7. 

Thanks limpbizkit

Original comment by sentient...@gmail.com on 9 Aug 2012 at 9:42

GoogleCodeExporter commented 9 years ago
@ comment #9
step 2: is the "gson 1.5" the version which works on all other devices? 

Original comment by fli...@gmail.com on 14 Nov 2012 at 5:32

GoogleCodeExporter commented 9 years ago
I know this issue is old, but it still seems to cause crashes on old HTC 
devices. I'm using Gson togher with Spring for Android 
(GsonHttpMessageConverter). Unfortunately the repackaging workaround does not 
work for me, because the GsonHttpMessageConverter obviously requires to "old" 
com.google.code.gson.Gson class. How can I fix that?

Gson 2.2.3, Spring for Android 1.0.1

Original comment by f...@getyourguide.com on 27 Nov 2013 at 10:41