Discovering Android – GSON woes on MyTouch 4G – Android 2.2.1

I’m developing an application using GSON for use with JSON-based Restful web services. Now it didn’t seem like it at the time, but there turns out that there is a conflict between HTC’s 2.2.1 release of Android and *ANY* app using GSON library.

The symptom you’ll get will be a “Unable to find Type: your.fully.qualified.ClassName” error message from an underlying TypeNotPresentException. This will likely drive you batty.

For me, the class it couldn’t find was in a third-party jar and I assumed the application was unable to access classes from this jar. I eventually moved the source for the third-party jar into my own project and thought my work was done. Alas, I still got the TypeNotPresentException.

Well, after much searching I eventually stumbled upon the actual problem and eventual solution.

You see, the HTC guys have included GSON themselves as a *public* jar. So, the classes for GSON are already loaded by a classloader which has NO IDEA ABOUT THE CLASSES YOU’RE USING. That’s why no matter what you do, the classes cannot be found once GSON tries to access them.

The work around for this is to use jarjar which will allow you to change the namespace of the GSON classes and use them explicitly, thus avoiding the publicly defined preloaded classes. Here’s a great explanation of how to use jarjar in this specific case.

Leave a Reply