Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Eclipse: Add external JARS to Android project....NoClassDefFoundError

 
Naishadh Parmar
Ranch Hand
Posts: 95
C++ Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I want to add one external jar to my Android project in Eclipse IDE.

I used the following methods:

1) Add the jar to the "/libs" folder

2) Add the jar to the "/libs" folder and then right-click on it and then add it to the build path

3) Add the jar to the "/libs" folder and then right-click on it and then add it to the build path then go to "Order and Export" and exporting my jar

4) Add the jar to the "/libs" folder and then right-click on it and then add it to the build path then go to "Order and Export" and reorder them placing my external jar the top and then export it

5) Go to the project properties and add the jar and then again 3) and 4)

6) Add the <uses-library> tag to the AndroidManifest (which showed a completely different error)

None of them worked.

It shows no error when coding but when I am testing it shows NoClassDefFoundError.

How do I go about it?

Thanks
 
Naishadh Parmar
Ranch Hand
Posts: 95
C++ Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the message before the error

01-04 18:37:56.113: E/dalvikvm(12153): Could not find class 'com.google.gson.Gson', referenced from method com.applix.nsa.AddActivity.onClick
01-04 18:37:56.113: W/dalvikvm(12153): VFY: unable to resolve new-instance 730 (Lcom/google/gson/Gson;) in Lcom/applix/nsa/AddActivity;

The error

01-04 18:41:06.209: E/AndroidRuntime(12153): FATAL EXCEPTION: main
01-04 18:41:06.209: E/AndroidRuntime(12153): java.lang.NoClassDefFoundError: com.google.gson.Gson
01-04 18:41:06.209: E/AndroidRuntime(12153): at com.applix.nsa.AddActivity.onClick(AddActivity.java:55)

 
Ron McLeod
Bartender
Pie
Posts: 963
59
Android Eclipse IDE Java Linux MySQL Database Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am pretty sure you only have to drop the jar in the libs directory. I have an app which uses gson-2.2.3.jar and I didn't have to do anything else.

Can you post the code fragment which is causing the problem?
 
Tim Holloway
Saloon Keeper
Posts: 18304
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I normally do not copy "external" jars to my eclipse projects. Instead, I tell Eclipse to include them in the project build path as external jars. For some of the really common external jars, such as Tomcat's servlet and JSP API jars, I go one step further and have them defined as symbolic references.

That's opposed to "external" as in third-party jars specific to the project, which I would generally copy into the project and use the library "Add jar" button on the project build dialog.

Note that defining a jar in the Eclipse build path may not be enough. If you build externally using Maven or Ant, those tools have their own inclusion mechanisms that have to be configured.

Finally, the classpath used when running an app in Eclipse may not be identical to the build classpath. In particular, if the profile for a test run was made before the library was added to the build profile, you should check the debug/run profile you are using to make sure that it picks up the jar, and if it didn't, update that profile. You can edit the debug/run profile by pulling the Run/Run Configurations... (or Debug Configurations) dialog, then searching it for the run profile of that project.
 
Naishadh Parmar
Ranch Hand
Posts: 95
C++ Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim,

I went into Run/Run Configurations. Didn't find run profile.

Please advise
 
Tim Holloway
Saloon Keeper
Posts: 18304
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, since this is an Android app, the rules are very different.

You may have a fundamental problem, since Android doesn't really run Java, it runs Dalvik, and Dalvik classes are not binary compatible with actual Java classes (or vice versa, for that matter). So if you dump a Java Jar into an Android app, it won't work.

You would have to re-create the classes in your Java Jar as Dalvik (Android) classes and include them in the installed module (apk), then run them under the Android emulator.
 
Ron McLeod
Bartender
Pie
Posts: 963
59
Android Eclipse IDE Java Linux MySQL Database Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Binary jars work fine with Android apps.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34973
379
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ron McLeod wrote:Binary jars work fine with Android apps.

Android doesn't compile to Java and can't run byte code. Android has a support library of actual Android jars.

What feature do you need in GSON? Android provides a JSONObject class built in which does much of what the GSON library does.
 
Ron McLeod
Bartender
Pie
Posts: 963
59
Android Eclipse IDE Java Linux MySQL Database Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are wrong with both your statements.

Android does compile to byte code and then runs a tool called dx to translate/cross-compile Java byte code into instructions which run on the Dalvik VM.

I use gson-2.2.3 in one Android app and it works fine.

The support library that you reference is used to provide applications which are targeted for earlier API versions (like Gingerbread for example) to use the newer APIs (like support for fragments).
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34973
379
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ron McLeod wrote:I use gson-2.2.3 in one Android app and it works fine.

I just tried with gson-2.2.4 and it does in fact work fine. And you are correct that all I had to do was drop in the lib directory. No mucking around with classpath or manifest.

Ron McLeod wrote:Android does compile to byte code and then runs a tool called dx to translate/cross-compile Java byte code into instructions which run on the Dalvik VM.

I did know that. I didn't realize it cross-compile code in jars too.

Ron McLeod wrote:The support library that you reference is used to provide applications which are targeted for earlier API versions (like Gingerbread for example) to use the newer APIs (like support for fragments).

Good to know.

Ron McLeod wrote:You are wrong with both your statements.

I stand corrected. I'm giving you two cows for standing your ground when two moderators told you this is impossible. Thank you for that. I'm happy to have learned something new.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic