Including a Jar in Android App - java

I want to include a jar into my Android package.
Unfortunately, I can't get it to work. I followed several explanations, such as this one, but I still get NoClassDefFoundErrors - at runtime, building, compiling, installing the project worked without errors.
Most answers seem to be outdated. How do I solve this issue using the current Eclipse, ADT and Android versions? Adding them to the Java Build Path like in plain Java projects didn't help.
All help appreciated.
What I have tried
Putting them into a folder and including into the project [Screenshot]

Put the jar in a folder entitled 'libs' (should be in the root of your project. Then add it to the java build path.
If that doesn't work try this:
Your Project -> right click -> Import -> File System -> yourjar.jar
Your Project -> right click -> Properties -> Java Build Path -> Libraries -> Add Jar -> yourjar.ja

Add your jars to libs folder. Automatically add's 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.

You two things and the error will be fixed
Right click on Project-->Select Properties-->Go to Java Build Path--> Select 3rd tab from top i.e "Libraries" -->Click Add Jar--> Select your Jar
Right click on Project-->Select Properties-->Go to Java Build Path--> Select 4th tab from top i.e "Order and Export" --> Select(chekbox) your Jar.
For more help check this link

Related

Using jar file in another project

I have one jar file, named pwd.jar and I want to use that in another project named sample.
How can I do that? What are all the steps I should perform?
Note: I am using Eclipse IDE
Right click your project-> properties->Java Build Path -> Libraries tab -> Add External jar, and browse your jar
Cheers

why this eclipse error showing and what should be the solution for it

Missing library: xdoclet-1.2.1.jar. Select the home directory for XDoclet. 1.2.1
why this eclipse error showing and what should be the solution for it alz
It is causing possibly because that jar is not added to your project build path. Follow this steps:
Right click on project(in the package explorer)
-> Buid Path
-> Configure Build Path
-> Java Build Path
-> Libraries tab
-> Add JARs button
Then locate the folder where the jar resides. You can filter your search by typing *.jar in the search field. And then click ok.
I downloaded the xdoclet-lib-1.2.3.zip file from:
http://sourceforge.net/projects/xdoclet/files/xdoclet/1.2.3/
unziped it to its folder name, xdoclet-1.2.3, and then from the Eclipse (I was using 4.5) preferences JavaEE -> XDoclet tab selected the Version 1.2.3 and then the Browse button to the xdoclet-1.2.3 folder. Apply and OK and Eclipse error goes away.
Saying all that, I suspect you don't really need it unless you know that you are using xdoclet. If that's the case and it was intentionally left out then the error message should be fixed in Eclipse. I only went around fixing it because of the message.
If have maven version of the project, can also try by updating the project. I also had this XDoclet. 1.2.1.jar complaint, which got resolved once I update the project.
Right Click Project > Maven > Update Project ... > Check the projects > OK.
Please add maven dependency in your project . Steps are -
1- Right Click on Project
2- select configure build path
3- select library tab
4- add library and select Maven Dependencies
5- apply and Apply and Close .

android import jar encounter NoClassDefFound

I encountered NoClassDefFound when my app is running, as I have imported sth form a external jar file. The file is located in /libs and has been added to the java build path. I use Eclipse with android SDK. I have tried clean for many times.
try this.
1. Your Project -> right click -> Properties -> Java Build Path -> Libraries -> Add Jar -> yourjar.jar
2. Your Project -> right click -> Properties -> Java Build Path -> Order & Export --> Select All --> OK
Try adding lib to build path. Right click your lib in eclipse and add to build path.
Or
go to projects properties in eclipse and in java - > java build path -> order and export ,just tick check box on your lib.
Update :: now just uninstall your app from device and clean and build project and run. (clean all projects if you have imported others)..... also build any other library project in eclipse if you have used it in your project.

Android import library

In my project, I need to use external library. That library consists of .class files. I have added folder with this library to my project using properties -> Java Build Path -> Libraries -> Add class folder. I can compile project without any problem, but when I run it, I get failed resolving xy and debugger stops on line where I create instance of class from imported files.
I guess the external library I use isnt linked to my .apk file. How can I fix this? I have tried to check this library on "Order and Export" tab assuming that, it will export the library with my application. But it didnt work.
How can I link this library to my project .apk? Do I need to somehow install this library on device first? Do I need to copy this library in application folder?
PS: I'm using Eclipse Juno, android SDK target 10.
Thank you
UPDATE
I have this library copied in libs folder. External library path is:
my_project/libs/sk/aicit/leg/libraries*.class
I have added ./libs folder in Libraries settings of project.
I do not have .jar version of this library, only .class files.
I have also souce code of this library, but I didnt want to include its classes in my project, it has its own dependencies, I wanted to just "link" it like library. Its an external library I havent written.
UPDATE 2
I have copied all the source files from library to my projects "src/" dir. I have removed Library from Java build path. I can stil compile my project, but when I run it Iget
FATAL EXCEPTION:main
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{
com.example/myvideorecord/com.example.myvideorecord.Robot}: java.langNullPointerException
...
What can I do, how to debug this error? :(
steps
Copy the library file into libs folder
select jar file -> right click -> Build Path -> Add to Build Path
clean the project Project-> Clean -> select your project -> ok
Android Studio
For importing a Android library into Android Studio, use the following steps:
1) (Optional) I like having my main Android project contain everything it needs to compile correctly, so I move external libraries into the libs/ directory. Generally, you only need to keep the following directories:
res
src
AndroidManifest.xml
*.iml (This will typically be the name of the library you're importing.)
2) Go to your Project Properties.
3) Go to Modules.
4) Import a New Module (Command + N and then Import Module).
5) Navigate to the library directory you want to import.
6) Create module from existing sources.
7) Click Next three times to add the necessary files.
8) Click Finish to complete the module additional.
9) Click on your project in the module list and go to the Dependencies tab.
10) Click the + button at the bottom and click Module Dependency....
11) Make sure your library you're importing is selected and click OK.
12) Click OK in the Project Properties window.
And you should be good to go.
Copy that library file into libs folder
if u have the source code of the library you can import it in eclipse .
Right click on your project -> properties->android -> reference-> add
and add the library project which u have imported. This will include only .class files of the library in ur project

Setting the classpath in java using Eclipse IDE

I have an Eclipse project with an external folder containing a lot of JAR files (legacy libraries) . instead of adding all the jars in Eclipse ("add external Jar"), I would prefer to add a ref to this external folder. In the "configure build bath", I'm adding a "External Class Folder". If I'm doing that instead of adding the Jar, my program is not compiling anymore. Am I missing something on the way I should use this option ?
Thanks
Try this:
Project -> Properties -> Java Build Path -> Add Class Folder.
If it doesnt work, please be specific in what way your compilation fails, specifically post the error messages Eclipse returns, and i will know what to do about it.
You can create new User library,
On
"Configure Build Paths" page -> Add Library -> User Library (on list) -> User Libraries Button (rigth side of page)
and create your library and (add Jars buttons) include your specific Jars.
I hope this can help you.
Just had the same issue, for those having the same one it may be that you put the library on the modulepath rather than the classpath while adding it to your project

Categories