aspose file tools*
The moose likes IDEs, Version Control and other tools and the fly likes Eclipse: Add external JARS to Android project....NoClassDefFoundError Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Engineering » IDEs, Version Control and other tools
Bookmark "Eclipse: Add external JARS to Android project....NoClassDefFoundError" Watch "Eclipse: Add external JARS to Android project....NoClassDefFoundError" New topic
Author

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

Naishadh Parmar
Ranch Hand

Joined: Jun 02, 2011
Posts: 95


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

Joined: Jun 02, 2011
Posts: 95

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
Ranch Hand

Joined: Feb 12, 2013
Posts: 328
    
    6

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

Joined: Jun 25, 2001
Posts: 16145
    
  21

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.


Customer surveys are for companies who didn't pay proper attention to begin with.
Naishadh Parmar
Ranch Hand

Joined: Jun 02, 2011
Posts: 95


Tim,

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

Please advise
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16145
    
  21

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
Ranch Hand

Joined: Feb 12, 2013
Posts: 328
    
    6

Binary jars work fine with Android apps.
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30762
    
156

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.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Ron McLeod
Ranch Hand

Joined: Feb 12, 2013
Posts: 328
    
    6

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

Joined: May 26, 2003
Posts: 30762
    
156

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Eclipse: Add external JARS to Android project....NoClassDefFoundError