My favorites | Sign in
Project Home Downloads Issues Source
New issue   Search
for
  Advanced search   Search tips   Subscriptions
Issue 255: Reflection error using Desire HD with Froyo (2.2)
35 people starred this issue and may be notified of changes. Back to list
Status:  Started
Owner:  limpbizkit


Sign in to add a comment
 
Reported by dorian.c...@gtempaccount.com, Oct 26, 2010
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

Oct 30, 2010
#1 patrickv...@gmail.com
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.



twittertrends.zip
340 KB   Download
twittertrendsbuglog.txt
5.9 KB   View   Download
Oct 30, 2010
Project Member #2 limpbizkit
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.
Status: Started
Nov 1, 2010
#3 D.J.Span...@gmail.com
@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.
Nov 1, 2010
Project Member #4 limpbizkit
(No comment was entered for this change.)
Owner: limpbizkit
Nov 5, 2010
#5 markupartist
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
gson-htc-desire-hd.txt
29.6 KB   View   Download
Nov 5, 2010
#6 markupartist
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] https://code.google.com/p/jarjar/
Nov 5, 2010
Project Member #7 limpbizkit
That's great news; thanks for the update. I'll ask HTC to jarjar their own copy so app developers don't have to.
Nov 21, 2010
#8 mikepalu...@gmail.com
FYI, this bug also manifests itself on the new MyTouch 4G on T-Mobile.  Exact same issue & solution.  HTC's gson is clobbering mine.
Nov 21, 2010
#9 bgmei...@gmail.com
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 (https://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
Nov 21, 2010
#10 mikepalu...@gmail.com
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.
Dec 3, 2010
#12 bigonthe...@gmail.com
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.
Dec 3, 2010
#13 mikepalu...@gmail.com
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.
Jan 24, 2011
#14 janu...@gmail.com
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.
Jan 24, 2011
#15 johan.mj...@gmail.com
janu...: follow the recommendation in comment number 9 and you'll be alright.
Mar 2, 2011
#16 stefan.r...@gmail.com
thanks a lot, the how-to nailed it (although i am using gson 1.6 by now)
Mar 14, 2011
#17 troubado...@gmail.com
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 (https://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
Apr 12, 2011
Project Member #18 limpbizkit
 Issue 308  has been merged into this issue.
Jul 5, 2011
Project Member #19 inder123
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.
Dec 12, 2011
#20 BrianEmo...@gmail.com
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.
Dec 21, 2011
Project Member #21 limpbizkit
 Issue 391  has been merged into this issue.
Jan 4, 2012
Project Member #22 limpbizkit
 Issue 398  has been merged into this issue.
Jan 31, 2012
Project Member #24 limpbizkit
 Issue 403  has been merged into this issue.
Jun 27, 2012
#26 nileshji...@gmail.com
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)

Aug 8, 2012
#27 sentient...@gmail.com
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.java:73)
08-08 16:00:48.071: D/UNHANDLED_EXCEPTION(687): 	at org.apache.harmony.luni.lang.reflect.ListOfTypes.resolveTypes(ListOfTypes.java:61)
08-08 16:00:48.071: D/UNHANDLED_EXCEPTION(687): 	at org.apache.harmony.luni.lang.reflect.ListOfTypes.getResolvedTypes(ListOfTypes.java:53)
08-08 16:00:48.071: D/UNHANDLED_EXCEPTION(687): 	at org.apache.harmony.luni.lang.reflect.ImplForType.getResolvedType(ImplForType.java:72)
08-08 16:00:48.071: D/UNHANDLED_EXCEPTION(687): 	at org.apache.harmony.luni.lang.reflect.ListOfTypes.resolveTypes(ListOfTypes.java:61)
08-08 16:00:48.071: D/UNHANDLED_EXCEPTION(687): 	at org.apache.harmony.luni.lang.reflect.ListOfTypes.getResolvedTypes(ListOfTypes.java:53)
08-08 16:00:48.071: D/UNHANDLED_EXCEPTION(687): 	at org.apache.harmony.luni.lang.reflect.ImplForType.getResolvedType(ImplForType.java:72)
08-08 16:00:48.071: D/UNHANDLED_EXCEPTION(687): 	at org.apache.harmony.luni.lang.reflect.ListOfTypes.resolveTypes(ListOfTypes.java:61)
08-08 16:00:48.071: D/UNHANDLED_EXCEPTION(687): 	at org.apache.harmony.luni.lang.reflect.ListOfTypes.getResolvedTypes(ListOfTypes.java:53)
08-08 16:00:48.071: D/UNHANDLED_EXCEPTION(687): 	at org.apache.harmony.luni.lang.reflect.ImplForType.getResolvedType(ImplForType.java: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.java:112)
08-08 16:00:48.071: D/UNHANDLED_EXCEPTION(687): 	at org.redpin.android.net.home.RemoteEntityHome.performRequest(RemoteEntityHome.java: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:144)
08-08 16:00:48.071: D/UNHANDLED_EXCEPTION(687): 	at org.redpin.android.net.SynchronizationManager.onCreate(SynchronizationManager.java: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. 
Aug 8, 2012
Project Member #28 limpbizkit
@sentient.lemon: something must be misconfigured on your end. Are you repackaging only Gson or everything?
Aug 9, 2012
#29 sentient...@gmail.com
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
Nov 14, 2012
#30 fli...@gmail.com
@ comment #9
step 2: is the "gson 1.5" the version which works on all other devices? 
Nov 27, 2013
#31 f...@getyourguide.com
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
Sign in to add a comment

Powered by Google Project Hosting