Sharing Java Files between Android Projects

本文介绍了一种在多个Android项目间共享代码的方法。通过链接源文件夹而非复制,确保了源代码的一致性,并能即时反映修改。具体步骤包括创建辅助项目、链接主项目的源文件夹并排除特定文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.

转载于:https://www.cnblogs.com/songsiyao/archive/2012/02/08/2342934.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值