2

I'm new to android programming. I'm trying to include code from a non-android project in an android app. This contains shared code used by a lot of my other non-android apps.

I was wondering what the best way to do this is from a code maintenance perspective. The shared code that I want to include does get modified from time to time and I want to keep the process of updating any apps I write as simple as possible (automatic if at all possible).

Am I better to build a .jar file containing the shared code and copy this to my app (eg: using an ant script) or is there a more streamlined approach.

I specifically want to avoid turning my shared code project into any kind of android project.

Philip Couling
  • 13,581
  • 5
  • 53
  • 85
  • 1
    What, if anything, does your shared code use for build management? Any, Maven? – mikołak Apr 18 '12 at 22:45
  • Nothing of that sort. For the purposes of this question it can be thought of as Eclipse java project with an added ant-script builder to produce a jar. My other projects suck the jar file in along with the deployment (custom set of scripts). The problem I face is that as a newby it seems to me that ADT is packaging everything up through a black-box process. Telling it to suck in a .jar file from outside the project doesnt appear to be an option – Philip Couling Apr 18 '12 at 23:34
  • Jar distribution is industry standard, this is how API developer use to provide common java library and also the reason make Android one of the most popular SDK. there are hundreds of thousands of jars out there, for instance android.jar, junit.jar and etc. – yorkw Apr 18 '12 at 23:41
  • couling: First of all: "Any"->"Ant". Finger slip, sorry :). Secondly, if you're not really using any of those tools, then I believe *toto2* has your answer. If you have any other problems with that, may I suggest a symlink to the JAR? This is also possible on Windows, search for "junction". – mikołak Apr 19 '12 at 18:01

3 Answers3

1

Once you have your .jar, you simply have to add it to your project (or update with the new .jar file if this library gets updated).

Once it is in you project (let's say under the /lib folder, right click on the lib folder -> select build -> add to build path.

Unfortunately, I'm pretty sure there's no way to make an automatic update.

Erwald
  • 2,118
  • 2
  • 14
  • 20
  • There is a way if you use a build management tool (if "automatic" means rebuilding the dependencies). Hence my comment. – mikołak Apr 18 '12 at 22:54
1

Make a jar and add it to the android project's build path. Whenever there is an update to jar, you would need to update it in your project and update (increase) version of your app to automatically allow users to download and update the update (android market would take care of that for you).

Its the most widely and maintainable way.

Hope this helps.

Hassan
  • 1,002
  • 15
  • 21
0

In the projects' properties go to "Project References". It will should you the other open projects and you can click whichever you want.

I haven't tried it, but if you change the non-Android code and run the Android project, I would expect the non-Android project to be recompiled automatically.

toto2
  • 5,306
  • 21
  • 24
  • Compiling isnt the problem. You've described what I normally do for my other projects (you have to add the build path also). The problem is that the ADT (android) plugin packages everything up as well, and the shared/referenced project doesnt get packaged into the android app as I need it to be. – Philip Couling Apr 18 '12 at 23:41
  • Look at [this](http://stackoverflow.com/questions/3217643/how-to-add-external-jar-libraries-to-an-android-project-from-the-command-line). It looks like you have to add a line in the ant configuration file. – toto2 Apr 19 '12 at 02:24