Android, Eclipse, Put .jar in “lib”&“add to build path” vs Put .jar in “libs”

百般思念 提交于 2019-12-29 01:28:06

问题


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?


回答1:


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.




回答2:


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.


来源:https://stackoverflow.com/questions/15707024/android-eclipse-put-jar-in-libadd-to-build-path-vs-put-jar-in-libs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!