I have some jar libraries on which my app depends.
When I copy and paste the jars in the project/libs folder, it builds, compiles and executes correctly.
However, if I right click my project -> Build path -> Configure build path -> Libraries tab -> Add external JARs, and add exactly the same jars, it still gives me no error, it allows me to build and compile, but when the app is executed it crashes.
Why?
If you put jars in libs folder 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.
Adding jar to build path with lib 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.
Don't use add external jar this will add as Absolute path instead of this use the relative path by adding jar, with this you must put all the jar files into the libs directory.
If you using Add External Jar it will search for that jar file based on Absolute path so on device that path was not find and you getting Force Close.
Try with adding same jar file by using Add Jar and Add External Jar option.
check the difference here https://stackoverflow.com/a/10679344/760489
Related
I have setup an android test project for my application in ECLIPSE. My test project depends on some external libraries for Mock behavior.
I can add dependencies to my test project in following ways:
If I put all the dependency jar files to my 'application\libs' folder, but they will be bundled with my application APK, irrespective of that my source does not depend on the these jars.
If I put all the dependency jar files to my 'testProject\libs' folder, run test from ECLIPSE and success but logcat keeps givieng me warning of not finding classes. and i have read on SO that ant ingnores 'test\libs'? so is that the same case with ADT also.
So is it good practice to put external dependencies of test project in 'test\libs' only?
NOTE: In both the cases my test get run successfully But I am curious to get the right way.
If you are adding external dependencies in android libs folder is most recommended. Just follow this following steps:
1.Create a folder called libs in your project's root folder.
2.Copy your JAR files to the libs folder.
3.Now right click on the Jar file and then select Build Path > Add to Build Path, which will create a folder called 'Referenced Libraries' within your project.
Third step gives reference to your libraries file.
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?
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.
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.
I want to use a svg-android library to work with *.svg files in Android. For the same, I've installed the apache ant, created a folder in workspace called: SVGAndroid and then added
android.library.reference.1=C:\Android\workspace\SVGAndroid
to project.properties file.
There were no documentation for functions in ReadMe, so I'm wondering if the installation correct or not, how to reinstall an external lib in the second case and where are the examples of using svg-android?
For your including of the project, just include the project into your workspace and in your respective application under the properties of that project just add a dependency on a library that references svgandroid. Make SURE to include the files and ensure that the compile class files are present in the final output of the apk. This typically means that you add the project to your build path/order.
Another approach is to just take the entire project and compile it into a jar file and then include that jar in your libs folder and add that dependency on the jar file of that respective project to your core application.
Useful links
How can I use external JARs in an Android project?
I would rather suggest to not add the path to the project manually, but from Project Properties -> Android and you will have a list with all the library projects in the workspace from which you can choose the desired project.
Also I suggest adding in project.properties the following line:
manifestmerger.enabled=true
in order to have all the AndroidManifest.xml files of the referenced library projects merged into the main project's manifest.
If you want to include a library file in to your project follow the below steps;
Download a jar file and save it in any folder.
open your project.
Right click--> Build path--> Add External Archives---> Add your external jar file to your project
I am using a thirdparty jar file in my android project. When I add the jar to the build paths, via eclipse project -> properties -> java build path -> Libraries -> add external jar.
The project compiles fine until that line of code runs which uses any class from that jar and I get class not found exception at runtime and the program crashes.
I solved it by copying the third party jar file to the libs folder of my project. The class not found exception goes away.
Question:
Can someone please shed some light why this is required and why does it not just work with a reference from the project properties despite the fact it compiles fine.. Why do I have to explicitly copy it to libs folder ?
When I add the jar to the build paths, via eclipse project -> properties -> java build path -> Libraries -> add external jar.
That is not the proper procedure for use with the Android build tools.
I solved it by copying the third party jar file to the libs folder of my project. The class not found exception goes away.
Correct.
Can someone please shed some light why this is required and why does it not just work with a reference from the project properties despite the fact it compiles fine.. Why do I have to explicitly copy it to libs folder ?
Because only the JARs in libs/ are added to the APK file. The Java build path is pure a compile-time thing, not tied into Android's packaging of APK files.
All the libraries in the folder “libs” are found by the Android Eclipse plugin and are added to the “Android Dependencies” item of the project. A better illustration here
:http://android.foxykeep.com/dev/how-to-fix-the-classdefnotfounderror-with-adt-17
I tried everything I found posted on Stack Overflow and yet I'm still getting the annoying NoClassDefFoundException when I try to test on the emulator:
I copied the jar to the "libs" directory of the android project
I added the jar to the build path using Add Jar NOT add external jar
I ticked the jar in the export tab
I even tried to compile the jar once with JDK 1.6; still the same annoying exception. The jar I'm referring to just contains a few entity objects I created for another project, and would like to re-use with my Android project.
By the way, I also tried to add the jar as an external jar, but didn't work either.
You need to add it to the assets/ directory along with adding it to build path in that location for it to work. Do remember to pick the same jar when adding external library.
See this thread
NoClassDefFound Error comes in java, when some class is not available on runtime, while it was available at compile time, please check what is source of this error, you can use following blog to get assistance:
http://javarevisited.blogspot.in/2011/06/noclassdeffounderror-exception-in.html
Do the following ::
Go to the project properties >> Java Build Path add jar file as external jar.
Select order and export option >> select the libraries and jars of the project.
If not working change the order of jar make sure its above android dependencies .