2

I was trying to add the loopj .jar library into my project, in Eclipse v3.7.2

First I added the .jar into the "lib" directory, right clicked on it and picked "add to build path". It compiles fine but when executing I get an error "Could not find class 'com.loopj.android.http.AsyncHttpClient'.

So I remove the .jar from the build path, and move it into the "libs" directory.

No need to add the the build path when it's in the "libs" directory, this time it compiles fine and executes fine too.

So what is the subtile difference between the "lib" directory & "add to build path" vs "libs" directory?

jptsetung
  • 9,064
  • 3
  • 43
  • 55
  • Compile system? Eclipse? Ant? Maven? IntelliJ IDEA? – Leandros Mar 29 '13 at 16:17
  • If you use Eclipse for compilation, you have to use the 'libs' folder, because it is included in the build path. The lib folder isn't recognized by eclipse and could be renamed to everything you want. Though, you must've made a mistake by adding the library to the build path, because normally this should compile fine, too. – Leandros Mar 29 '13 at 16:21
  • @Leandros Close, but not quite. As CommonsWare explains below, the behavior jptsetung describes is exactly as expected when adding to lib/ and spelling everything right: it builds w/o error but fails at runtime because the Jar was not included in the APK. – Matt J. Aug 18 '13 at 20:32

2 Answers2

7

So what is the subtile difference between the "lib" directory & "add to build path" vs "libs" directory?

Android's current build tools (Eclipse and command-line) expect that JARs are in a libs/ directory. It will automatically add those JARs to your compile-time build path. More importantly, it will put the contents of the JARs into your APK file, so they will be part of your run-time build path.

Using lib/ and mucking with your build path manually will solve the compile-time problem, but then your JAR contents are not in your APK and therefore will not be available to you at runtime.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Looks like a good answer, but what annoys me is that I'm pretty sure I used once the lib/ folder and it worked. It was a long time ago, so maybe a few things have changed in Eclipse/Android. – jptsetung Mar 29 '13 at 16:34
1

Citing: http://tools.android.com/recent/dealingwithdependenciesinandroidprojects

If you are still referencing jar libraries manually instead of putting them under libs/ be aware of the following:

  • If the project is a Library project, these jar libraries will not be automatically visible to application projects. You should really move these to libs/
  • If the project is an application, this can work but you must make sure to mark the jar files as exported.
C.d.
  • 9,932
  • 6
  • 41
  • 51