0

One thing that has continued to frustrate me as I learn Android development (via Eclipse) is getting Android app projects that reference Android library projects to work reliably. Sometimes the app project won't build because of missing class references that are defined in the library, and sometimes it builds but then crashes at runtime with the class defined in the library is referenced, with an error link

java.lang.NoClassDefFoundError: com.dave.customviewtestlib.LibTestClass

shown in by logcat.

I'm using Android Developer Tools Build: v21.0.1-543035

I've created a workspace with the simplest possible Android app project referencing the simplest possible Android library project. How exactly am I supposed to tie them together in Eclipse? They both build fine by themselves. Then I edit the "Java Build Path" of the Android app project. The confusing thing is, there seems to be a lot of ways to reference the library project. Sometimes one way works, then it stops working and another way that didn't work before starts working. Am I supposed to

  1. In the app project's Java Build Path | Projects, "Add" the library project, or
  2. In the app project's Java Build Path | Libraries, "Add Jar" and point to to a .jar containing the library's classes and other resources, or
  3. In the app project's Java Build Path | Libraries, "Add Class Folder" and point to the library project's "bin" folder which contains the classes, or
  4. In the app project's Java Build Path | Source, "Add Folder" and point to the library project's source folder, or
  5. Something else?

Also, there is the mysterious Java Build Path | Order and Export tab. What does that mean?

Also I have a related question: are these so-called Android libraries like static libraries (you reference them at build time and the code gets sucked in, and at runtime it is just part of the app binary), or like dynamic libraries (they can get installed separately on the device)? I understand it's all Java and all classes are loaded at runtime by the class loader, but I think there is still an important distinction. Most tutorials and references are so dumbed-down, getting answers is hopeless.

Any help would really help minimize my irritation :)

David Stone
  • 1,132
  • 7
  • 18
  • 1
    http://stackoverflow.com/questions/5991299/java-eclipse-android-beginner-question-how-to-add-libraries-or-linked-folder – Jesse Jashinsky Jan 18 '13 at 21:02
  • Also see http://developer.android.com/tools/projects/projects-eclipse.html#ReferencingLibraryProject "are these so-called Android libraries like static libraries" -- yes, though they also compile in resources (layouts, drawables, etc.) "Most tutorials and references are so dumbed-down, getting answers is hopeless" -- this is covered in the documentation, in books, and in many answers here on StackOverflow. Heck, a Google search for `adding an android library project` comes up with the answers in the first page of results. – CommonsWare Jan 18 '13 at 21:07
  • Yeah, part of the problem is that Eclipse seems to leave projects in inconsistent/bad state. I was referencing the Android lib the wrong way (via "Java Build Path" | "Libraries" | "Add Class Folder". I was getting the ClassDefNotFoundError at runtime. So I changed the refernce to use the "Android" tab as per the instructions here and on the link you provided. Guess what? I still got the ClassDefNotFoundError. I had to delete the app project's /bin folder to fix it. Was I supposed to magically know to do that? – David Stone Jan 19 '13 at 16:48
  • (And yes, I tried Cleaning and Refreshing and I was still getting the error until I deleted the /bin folder from the file system and rebuilt. I've used Visual Studio and other IDEs for 15-20 years and when you make a change and then rebuild, they always pick up the change and work fine, without extra mystery steps you have to guess) – David Stone Jan 19 '13 at 16:50

1 Answers1

2

In the package explorer view:

Right click on the library project -> project properties -> android tab

Check: Is Library.

Right click on the Android Project -> project properties -> android tab

Click the "add" button, and select your library project from the list.

To answer you "static question" the difference between and Android library project and a jar'd library is that the android tools automatically fixes resource (xml) references for your project (the generated R file, etc) and then it packages the classes into a .jar file and adds it to your project's apk. So it is included statically.

As to your build path and order and export tabs. The build path is where you can add: .jar files, other projects in your work space, and additional source files to your project. The order/export tab from this is where you select what libraries/class files are going to be statically included with your project's jar file (or apk in the case of Android projects).

accordionfolder
  • 573
  • 4
  • 17
  • Thanks, that helped. I swear I have tried that many times, but when I created a fresh workspace and fresh app and library projects, following your steps worked. Part of my confusion is that Eclipse seems to get itself in bad states. For example, I had an Android library that the app was referencing via "Java Build Path" | "libraries" | "add class folder". Then I removed it from there and added the reference to the Android tab as you say above. Upon cleaning, Refreshing, and rebuilding/installing, I got the dreaded ClassDefNoFoundError. I had to delete the /bin folder to fix it. – David Stone Jan 19 '13 at 16:44