When you develop Android applications using Gson library, you might face the java.lang.VerifyError error when running an app on devices manufactured with Gson library already bundled with the OS. To solve the problem, we recommend you to patch the bundled library by renaming the library package. That's how we fixed our app. But let’s start from the beginning.
Recently, one of our Android apps caused a strange error:
Caused by: java.lang.VerifyError:
This error appeared on a limited number of devices but still we couldn't ignore it.
Searching for the problem
The cause for this problem can be quickly revealed by looking at the logcat output, but we've received this kind of error message from Google Play with only a stack trace that contained very little information. From the stack trace we found out that java.lang.VerifyError happened in some class used from WebServiceHelper.
The java.lang.VerifyError exception is typically raised when the JRE cannot find a method at runtime or method signatures of the called method and the actual method don't match. For example, calling String.isEmpty() will raise java.lang.VerifyError on devices running Android 2.2 or lower, because this method is only available in API version 9 and above.
Obviously, to avoid this kind of errors the application should be tested on every version of Android down to the minimum supported API version specified in android:minSdkVersion attribute in the manifest. Android Lint can also help in finding potentially missing methods. However, in the described case we did not have invalid method calls and the WebServiceHelper class in question was really simple. The only possible place for the error was the following line of code:
return new Gson().fromJson(response, responseClass);
A web search showed that on certain devices, predominantly HTC, Gson library is found in the classpath and the application uses the system library instead of the bundled library. Very likely this results in a version mismatch between the two libraries, in which case the error occurs.
One suggested fix we found involved repackaging the bundled library and renaming the library package from com.google.gson to com.google.myjson in the process. We successfully did this using a handy utility program called Jar Jar Links.
If you encounter this kind of error in your project, you can fix it in a few easy steps:
- Download Jar Jar Links.
- Place the files jarjar-1.3.jar and google-gson-1.6.jar into a same folder.
- Create a file rules.txt in that same folder.
- Add the following line to rules.txt: rule com.google.gson.** com.google.myjson.@1
- Run this command from the command prompts: java -jar jarjar-1.3.jar process rules.txt google-gson-1.6.jar google-gson-1.6-patched.jar
- Replace the library jar in your project with the patched version and update the import statements accordingly.
We suggest using the described method if you develop Android applications using Gson library — your app will work stable on a greater number of mobile devices. Tested and confirmed by Azoft.