0

i've been struggling with this issue for a couple of days now...

ill explain my problem in a nutshell....

i have a simple java project (not android library or something more complex) which i want to use as my model..

lets assum it is called mymodel and it has a simple class Model.

i also have an android application myandroidapp and in the main activity i want to create an instance of the class Model.

I can add the mymodel as a project to the build path and compile but when i run this application it throws class not found exception. how can i make it work?

please help.

thanks

Gur
  • 111
  • 8

2 Answers2

0

Step #1: Create a JAR based on your "simple java project"

Step #2: Put the JAR in the libs/ directory of your Android project

And you're done.

Never mess with the build path of an Android Eclipse project manually.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • thanks for the help. yet i didt get how to reference my jar file code and how exactly to add it into /libs (do you mean copy paste?) – Gur Mar 18 '13 at 01:06
0

In Eclipse, you can follow the following steps:

Creating JAR file

  1. Right-click on the root of the mymodel project and click on 'Export...'
  2. In the window that appears, expand the 'Java' category/folder, choose 'JAR file' and click 'Next'
  3. In the next window, choose the options you desire, especially pay attention to where the 'export destination' is. When you're done, click on 'Finish'. The JAR file will now be located at the 'export destination'.

Adding JAR to Android project

  1. In your android project (myandroidapp), copy the JAR file into the libs/ folder. You can do this by simply copy and pasting or dragging and dropping etc.
  2. Then, right-click on the JAR file that is now in the libs folder, go on 'Build Path' and from the menu that appears, click on 'Add to Build Path'

You should now be able to use this JAR file in your Android project.

s16h
  • 4,647
  • 1
  • 21
  • 33
  • thanks. i followed your steps and now i get the ClassDefNotFoundError instead... i guess its a progress but i cant figure out what im doing wrong... when i create the jar i check only the "export all output..." then i use drag-drop to add the .jar file to /libs and then add to build path as you mentioned... any ideas? – Gur Mar 18 '13 at 02:07
  • Does the `Model` class work OK in it's own project? Is it imported correctly? Maybe the import statement is importing another `Model` class. Perhaps show us the import statement code and the snippet of code from where you are using the class. – s16h Mar 18 '13 at 10:35
  • everything works fine when im running a java application on my machine... it crushes only on android for some reason anyway the import statement is import MyProject.Core.Model – Gur Mar 18 '13 at 12:59