Eclipse Android项目,如何在工作区中引用库?

2022-01-20 00:00:00 ide android eclipse

我按照我在这里找到的一些步骤(现在找不到 URL,抱歉)将我在 Eclipse 中的 Android 项目转换为我的 99.9% 的代码在库项目中的布局,然后我还有另外 2 个同一工作区下的 shell 项目,大部分只是 AndroidManifest.xml 文件和一些资源文件.这样做是为了让我可以支持同一项目的 2 个版本,两个版本之间只有一些小的文本/图标更改.应用程序名称也不同,因此我可以同时在 Android Market 上发布这两个版本.

I followed some steps I found here (can't find the URL right now, sorry) to convert my Android project in Eclipse to a layout where 99.9% of my code is in a library project, and then I have 2 other shell projects under the same workspace that are mostly just the AndroidManifest.xml files, and a few resource files. This was done so I can support 2 builds of the same project, with just some minor text/icon changes between the 2. The application name is also different so I can publish both on the Android Market at the same time.

自从我这样做以来,我大约每编译 10 次,也许每天或两天一次,我得到dalvik error 1"和一些关于Access 已经存在"的信息(Access 是我的第一个 Java 单元的名称图书馆项目).

Ever since I did this, about every 10 times I compile, maybe once every day or two, I get "dalvik error 1" and something about "Access already exists" (Access being the name of the first Java unit in my library project).

为了解决"这个问题,我进入了我正在尝试构建的存根项目的 Java 构建路径,并从库选项卡中的主库中删除了 JAR 文件.然后我可以构建而不会出现错误.

To "work around" the issue I go in to the Java Build path for my stub-project that I am trying to build, and remove the JAR file from my main library from the libraries tab. Then I can build without the error.

过了一会儿(可能是 1 或 2 天),当我编译我的存根项目(不是我的库)时,我会收到一个关于缺少类的错误.所以我会回到 Java Build 路径并将对 JAR 文件的引用放回原处,1 或 2 天一切正常,然后我又回到了和以前一样的错误.

Then a while later (maybe 1 or 2 days) I'll get an error about missing classes when I compile my stub-project (not my library). So I'll go back to the Java Build path and put the reference to the JAR file back in, and all is good again for 1 or 2 days, then I'm back to the same error as before.

这只是一个已知问题和我需要做的事情,还是我可以通过重组我的项目/工作区来解决?目前我有:

Is this just a known issue and something I need to do, or can I resolve by a restructure of my projects/workspaces? Currently I have:

Lib 项目 - 构建路径上只有 2 个库:Android 2.1 和 com.android.ide.eclipse.adt.LIBRARIES

Lib Project - only has 2 libs on build path: Android 2.1 and com.android.ide.eclipse.adt.LIBRARIES

使用上述库的第一个存根项目 - 具有与上述项目相同的 2 个库,另外有时我使用添加 JAR"来包含上述项目的 bin 文件夹中的 JAR.

First stub project that uses above lib - has the same 2 libs as above project, plus sometimes I use "Add JAR" to include the JAR from the above project's bin folder.

第二个存根项目 - 与第一个存根项目相同的库

2nd stub project - same libs as first stub project

我是否应该使用构建路径选项下的其他选项卡之一从我的lib 项目"中引用 JAR?也许是项目"选项卡,或者源"选项卡?我目前在其他任何领域都没有它.

Should I reference the JAR from my "lib project" using one of the other tabs under build path options? Maybe the "Project" tab instead, or the "Source" tab? I don't currently have it under any of those other areas.

当我进入奇怪的状态时,做一个清理项目"也无济于事,我已经尝试了几次,并在清理之间打开/关闭 IDE,但没有解决.

When I get in to the weird state, doing a "Clean project" also doesn't help, I've tried that several times and open/close the IDE between cleaning, to no resolve.

此时我们处于最后的测试阶段,所以我正常的日常任务是:

At this point we are in the final testing stages, so my normal daily task is:

  1. 在 LIB 项目中进行次要更新(错误修复)

  1. Make a minor update (bug fix) in the LIB project

使用发布向导导出两个项目并更新 Android Market 和我们保存 APK 文件的其他地方

Use the publish wizard to export both projects and update Android Market and other places we keep the APK files

因此,我希望这些步骤保持简单,而不必打开/关闭多个工作区或在可能的情况下执行大量构建步骤.

So I'd like those steps to stay simple, without having to open/close multiple workspaces or go through a lot of build steps if possible.

推荐答案

经典的Eclipse/Java方式

添加(工作区)库

  • 右键单击要插入的项目.
  • 点击属性.
  • 选择 Java 构建路径.
  • 选择标签.
  • 现在,这取决于您如何编译库.所以要么执行Add JARs...Add Class Folder.选择 external 变体以使用外部源.就是这样.

    Now, it depends how you compile your library. So either do an Add JARs... or an Add Class Folder. Choose the external variant to use an external source. That's it.

    • 右键单击要插入的项目.
    • 点击属性.
    • 选择 Java 构建路径.
    • 选择项目标签.

    点击右侧的Add...即可完成.

    需要两个步骤:

    • 将您要使用的项目标记为库项目
    • 引用标记的项目

    右键单击您的项目并选择Properties.选择左侧的 Android 并勾选复选框 IsLibrary.完成.

    Right click your project and select Properties. Select Android on the left and tick the checkbox IsLibrary. Done.

    右键单击您的项目并选择Properties.选择左侧的 AndroidAdd... 您标记的项目.它将被添加到列表中并可以使用.现在您可以从引用的标记项目中访问所有类和资源(例如可绘制对象、字符串).太棒了,嗯?:)

    Right click your project and select Properties. Select Android on the left and Add... your marked project. It will be added to the list and is ready to use. Now you are able to access all classes and ressources (e.g. drawables, strings) from the referenced, marked project. Awesome, eh? :)

相关文章