4

My code have been working fine for over a year and after changing some code in other places, I did not change any code in the MainActivity and that is the first form. I now get this error: java.lang.reflect.InvocationTargetException and then java.lang.NoClassDefFoundError: com/google/gson/Gson when trying this:

   Gson gson = new Gson();

I have other programs where I use Gson and they works fine.I have the gson-2.2.4 in the libs folder in my project. I have tried for hours reading all other similar problems but I'm stuck. Cat Log:

06-06 01:47:25.935: I/System.out(4393): debugger has settled (1304)
06-06 01:47:26.120: E/dalvikvm(4393): Could not find class 'com.google.gson.Gson', referenced from method com.comcasystems.routedriver.MainActivity.onCreate
06-06 01:47:26.120: W/dalvikvm(4393): VFY: unable to resolve new-instance 133 (Lcom/google/gson/Gson;) in Lcom/comcasystems/routedriver/MainActivity;
06-06 01:47:26.120: D/dalvikvm(4393): VFY: replacing opcode 0x22 at 0x000b
06-06 01:47:26.120: D/dalvikvm(4393): DexOpt: unable to opt direct call 0x0206 at 0x0d in Lcom/comcasystems/routedriver/MainActivity;.onCreate
06-06 01:47:26.170: E/dalvikvm(4393): Could not find class 'com.google.gson.Gson', referenced from method com.comcasystems.routedriver.MainActivity$1.handleMessage
06-06 01:47:26.170: W/dalvikvm(4393): VFY: unable to resolve new-instance 133 (Lcom/google/gson/Gson;) in Lcom/comcasystems/routedriver/MainActivity$1;
06-06 01:47:26.175: D/dalvikvm(4393): VFY: replacing opcode 0x22 at 0x0024
Arun C
  • 9,035
  • 2
  • 28
  • 42
Kim HJ
  • 1,183
  • 2
  • 11
  • 37

1 Answers1

3

Take care that you included the Gson library in the Java build path and check that proguard is configured appropriately:

# proguard configuration for Gson
-keepattributes Signature
-keep public class com.google.gson
-keep class sun.misc.Unsafe { *; }

-keep class com.comcasystems.routedriver.jsonclasses.** { *; }

Note: the last entry is just a symbolic placeholder for your Json classes that protects them from being obfuscated. You have to list all affected classes here.

Hope this helps ... Cheers!

Trinimon
  • 13,839
  • 9
  • 44
  • 60
  • 1
    I don't think you need to worry about proguard if you are using @SerializedName to mark your attributes. – Christopher Perry Jun 06 '13 at 06:17
  • @Christopher: ... what is probably valid for the Json classes being used. However, there are some additional entries above. Check http://code.google.com/p/google-gson/source/browse/trunk/examples/android-proguard-example/proguard.cfg?r=878 too – Trinimon Jun 06 '13 at 06:27
  • Adding the above to the proguard-project.txt didn't help I get this when it first hit the line. Remember it did work before I only added some activities and renamed some class. dalvik.system.PathClassLoader[/system/framework/com.google.android.maps.jar:/data/app/com.comcasystems.routedriver-2.apk] – Kim HJ Jun 08 '13 at 01:17
  • Ok, I see, and as long as you run the app in the emulator proguard isn't involved anyway - did you? It sounds like you are having problems with your jar file or Java build path. Did you check what *dd619** proposed (check your APK file in the bin folder; just unzip it). May be you added the jar as an external jar what requires the target environment providing this jar by default. – Trinimon Jun 08 '13 at 16:30
  • All the time I have used my device for debugging. I tried to run the debug on the emulator and I get an error. INSTALL_FAILED_MISSING_SHARED_LIBRARY – Kim HJ Jun 09 '13 at 02:01
  • This is what I have in my APK file. META-INF(MANIFEST.MF, CERT.SF, CERT.RSA), res (all Drawable, all layout and Menu), AndroidMainifest.xml, resources.arsc and classes.dex. – Kim HJ Jun 09 '13 at 02:19
  • Ok, you have to take a look into the classes.dex - check for instance http://stackoverflow.com/questions/3593420/android-getting-source-code-from-an-apk-file/15395456#15395456; if it doesn't contain the Gson class there is something wrong with the Java build (path/process). It's really hard to say what going wrong. Guess you tried already to reassign the Gson library or to clean up your project? – Trinimon Jun 09 '13 at 12:14
  • 2
    I created a new project and copied all my files to the new project and added the gson to the libs folder and it's working fine. Thanks. – Kim HJ Jun 10 '13 at 22:16
  • Sometimes it's really wired. A colleague of mine just had a problem with his project setup as well that was working well in my IDE. Finally he found out it was due to a new feature in the ADT version 22 (called `PRIVATE_LIBRARIES`, see http://stackoverflow.com/questions/16596969/libraries-do-not-get-added-to-apk-anymore-after-upgrade-to-adt-22). - and using the newest SDK bundle I had a problem with spaces in the path. Always fun ;) – Trinimon Jun 11 '13 at 06:03
  • This helped in my case. – localhost Nov 08 '13 at 16:50