how to import own jar into android project (ADT 20) - java

If I import a selfmade jar file into my Android project (add it to the libs folder) I get an "Could not find class" error sent by dalvikvm. (I'm using Eclipse)
Obviously it's not an import error, because downloaded jars (like jdome) worke fine.
So I guess it's an export error. The way how I export a jar is: right click on project -> export -> jar -> leave default settings -> finish
I'm sure it's no internal problem, because the jar contains only one class which has only one method for test reasons.
Does anyone know what I did wrong?????

Just copy and paste your .jar file in libs folder of your project.

Yry this, I hope this will work for you:
Go To Your Project
Right Click
Properties
Java Build Path
Libraries
Add External Jar
Select Jar File
OK

Related

How to open a Project in NetBeans

Hi guys I just clean and build my project in Netbeans, copy the folder of my project, put it in a ZIP archive and send it to myself. Now I don't know how to deploy the project, I tried just to copy the classes and run it, but it give me so many error, even if I try to recreate the structure of the packages with the same name it doesn't work. I tried to find information on the internet but is not very clear.
When I click on Clean and Build.
NeatBeans create a folder with the name of the project and inside> build,dist,nbproject,src,web,build
When you clean and build under Netbeans, you should find all you need under the dist folder:
if you a creating a web application you should find a .war file - that is the file to deploy: just copy it to the webapps folder for Tomcat
if you are creating a standalone application or a library, you should find the .jar
Above assume you do not use maven for the project. If you did you would not have a dist folder, but the standard maven target one.
I don't really know what are the errors you get. However check these few things out and see if it works for you:
check if you are importing from an external Library, if so, make sure that you have those libraries within your project.
If you are creating a new project and just copy pasting the classes, then make sure packages are correct and not from the other project you had.
it is pretty simple to open a project in net beans:
after creating your project and building it, zip the whole project folder.
once you unzipped it, use "CTRL+Shift+O" or the third option in netbeans>file to open a project.
a filechooser should open and you can select the project you want to open.
this is pretty much the whole thing you need to do.
you can also find the .war in webapps folder, .jar in dist folder too.
your question is really unclear. We dont know what is your project exactly nor your errors.
Please let me know the exact errors. so that I can help you with better.
I don't understand exactly what is the error your facing but I think that if you use the export and import project features of NetBeans maybe you wouldn't have that error in the first place:
To export project:
go to File -> Export Project -> To ZIP... -> Specify the zip destination directory.
To import project:
go to File -> Import Project -> From ZIP... -> And browse your file system to find the zip you previously created.

eclipse / android / java - cant reference class from jar file in libs

OK, so I made a jar file, using the eclipse export feature, on one of my projects, let's call it X.
So X.jar has a single class in it, M.class.
I took this jar and put it into the libs/ folder of another project.
When I do
M m = new M()
it tells me it can't find M, and offers to import it from the package it's in. When I accept that import, it tells me the import cannot be found.
Now, what's confusing (or at least frustrating) is that if I export the jar with M.java instead of M.class, it works - but I need the jar to not be recompiled in the new project, hence why I was hoping to use the class file and not the source.
Instead of placing the jar file in the lib folder, try adding it to the build path.
In eclipse:
right click on the project -> Build Path -> Configure Build path... -> Add External JARS
In IntelliJ:
Ctrl+Alt+Shift+S -> Libraries -> new(Plus Sign)

Android external Jar

I have some api with java and I need it in my android app.
When I am using it with including all packages and classes it works fine. Now I create JAR file from my java project using export. Now when I add the jar to my android app as a external jar my all errors are disappeared but when I run my app I got an error
java.lang.NoClassDefFoundError.
I have seen that kind of errors early in my apps and it was the problem of Jar. How can I generate JAR file to avoid this situation?
You have probably built that jar file with jdk 1.7. Classes compiled with jdk 1.7 are not included in apk file. Try to change java compiler to 1.6 before you export jar. In eclipse go to Window -> Preferences -> Java -> Compiler.
copy the jar file in the libs folder of your android project.
After that you may want to clean your project and rebuild it. You can even clear manually the bin dir content.
You have to add your jar file to your project's buildpath.
To make sure it's exported in your APK, you also have to check it under the Order and Export tab, in the build path preferences of your project.
A better approach might be the following:
remove the current exported jar from your project
select the project you want to import, right click -> properties -> android -> check 'mark as library'
right click your current project -> properties -> android -> locate the 'add' button in the bottom. Click it and select the library project.
Using this approach any changes in the library project will be reflected in your current project (at least after doing a clean projects).
I have done it !!! Thanks everybody.
So the solution is :
1. change java compiler to 1.6 before you export jar (As #Lecho mentioned)
2. Move the Jar file to lib folder
So it works !!! thanks everybody

Build java library with resources

I am trying to make android java library, but when I build my project in output jar library placed only java compiled classes, but no resources and assets.
Also my project contains native libraries, I need to include it in jar too.
PS: I have connect my library project to another project.
PS2: I have find out that resources from library project (res, libs) compiles into apk file, but assets not. How to make assets compile too?
To export jar with resource file please follow the below steps.
1) Right Click on Project Folder
2) Then Click on Export
3) Click on Java -> Jar file.
4) It opens a Jar Export Dialog . Please ensure that Exported generated class file and
resource check-box is checked.
5) Then provide the export destination .
6) Finally click on finish button.
In export menu for eclipse, you have a check box asking you for the inclusion of source files

How to use your own created jar file in another android application

I created my own jar file using eclipse. Now i am trying to make an android JAR file and then will use the same JAR file in others android apps.
Now though, my application is complete and make a jar file, but how i will use it for other Android Apps?
You should be able to access any publicly exposed methods from your .jar by importing the .jar into your project, then adding
import myjar.myclass;
You can then reference any methods from the class you import.
Hope this helps!
Add the jar to you build path
Right click project
Click "Properties" -> "Java Build Path" -> "Libraries"
Click "Add External Jar"
Import classes like normal
import com.my.package.here.MyClass;

Categories

Resources