Sharing Java Files between Android Projects

I have a "main" Android Java project, with a significant number of files in it. I would like to create a second project so that I can hammer away at one particular class for debugging and robustness purposes, but I want to keep the debugging code out of my main project. But I want to have it available to run anytime in the future. I also want to be able to create similar Apps to my main App (but with a slightly different look and feel) that have almost all the java code in common. At the same time, to keep development simple, I don't want duplicated source files of shared code on my machine and then have to manage modifications between them.

A quick scan of the web shows I don't appear to be the only one wanting to share files - or java packages between Android (or non Android) Eclipse projects. The standard suggestions of sharing code in jar files, or through cvs or subversion are not practical - I really want to be able to alter a file in one project, and have it appear instantly altered in all projects while testing, and only commit to the version control system when I'm really ready to commit, not simply to exchange code between projects.

So after many hours of frustration, here is the simple how to.

1) Assume you have a "main" project which has some source that could be of use to other projects. For example purposes, we will call it "ProjectMain" here.

2) Make a second Android Project as you normally would, and make sure it can generate and run the typical "Hello World" App. We will call this "SecondaryProject".

3) Now we give SecondaryProject access to the src files of MainProject: Right-Click on "SecondaryProject" in the Eclipse Package Explorer (you must click on the project name, not any of of its contents). Move the Mouse to "Build Path" on the popup menu, and then select "Link Source" on the top. You now get a dialog box, where you can browse to select "Linked folder location". This should be the location on your hard disk of the "src" folder of "ProjectMain". Once you have selected that, you have to fill in the field "Folder name:", which is the name that source folder will appear as in the "SecondaryProject" project. I called mine "src_import". Click on finish and the "src_import" folder will appear in your "SecondaryProject".

4) Now some errors may appear in your "SecondaryProject" in the src_import folder. This will most likely be in the "main.java" file of "ProjectMain" that is now included in the sources of "SecondaryProject". So deep down in the src_import folder, find the offending file(s), and right-click in the file name (i.e. main.java) and select "Build Path"->"Exclude". If you have more files that access resources in "ProjectMain" you will have to exclude them.

5) Now "SecondaryProject" should be error free, and you should be able to run it and access all the java files and packages you shared from "ProjectMain".

6) Remember that for the shared files, only one copy exists between all projects. So if you change the contents of a shared file in one project, it will change in all of them. So only share code that is generic enough to not require App specific alterations.

posted @ 2012-02-08 16:43  songsiyao  阅读(185)  评论(0编辑  收藏  举报