如何将外部库包含在多个项目中,而不需要复制多个副本 [英] How to include external libraries in multiple projects without making multiple copies android java

查看:258
本文介绍了如何将外部库包含在多个项目中,而不需要复制多个副本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 afreechart edu.emory.mathcs.jtransforms org.afree.chart 在我为Android编写的许多不同的活动。我有非常多的麻烦,每个新的项目配置,以便我可以


  1. 看从库中的类,当我写代码在Eclipse IDE中。

  2. 查看这些类的附件来源

  3. 查看这些类的附加的javadoc

  4. 当我尝试运行我的活动时,这些类的.class文件。

我现在做的是制作一个.jar的副本,我使用的每个库的doc src和.properties。我将这四个东西复制到我正在创建的每个项目(每个活动一个)的本地lib文件夹中。



我一定很想念一些简单的东西。似乎很明显,我应该在一个位置使用这些库,其文件,src和.properties,并且我应该能够在使用这些库的每个项目中引用该位置。但我还没有弄清楚如何做到这一点。请告诉我怎么样



如果有一种方法来定义一组jar和相关的src&文档将自动并始终被任何新的eclipse项目搜索,这将会更好。



编辑添加后关于外部Libs的答案:



所以我确实把jtransforms-2.4.jar放在外部目录下。在同一个目录中,我将src和doc用于包,并且文件jtransforms-2.4.properties与doc和src条目指向本地文件与doc和src。我将其添加到我的构建路径作为外部jar。



1)Eclipse IDE肯定会发现.jar中的类并没有标记为缺失。
2)doc和src显示为未附加。 如何将doc和src信息与库一起放置,以便它们自动显示每个地方将jar链接为外部jar?
3)当我尝试运行应用程序时,我得到NoClassDefFound错误的jar中的所有类,也就是说,Eclipse IDE可以在外部lib中找到这些类,但是它们没有进入应用程序的类路径。



摘要:所以外部jar技术在三个重要方面有两个失败:
1)没有src和doc显示为附加在eclipse IDE
2)不要提供更新的类路径到应用程序。



FINALLY:当我将jar,.properties,doc和src复制到我的项目的lib文件夹,并删除了外部所以我重申了我原来的问题,给出的答案(添加到外部jar的构建路径)没有解决任何问题。

解决方案

他们的关键字是 ernal 。您可以将库添加到您的项目中,而不需要在本地的lib文件夹中。你只需要添加它们作为外部档案。



您还可以通过项目的属性添加和更改它们。

虽然我不认为这是一个好主意,为您创建的每个项目添加一些库,可能有一个脚本,但是它不是一个内置函数。

编辑:

那么它不是真的内置的,它看起来对我来说很吃惊,但是这个线程可能对您有用。它包含一种可以通过属性文件解决问题的方法。您只需要创建一次该文件并将其添加到您的项目中。所以这是半自动的;)

但我认为这个解决方案比将它添加到每个项目更好,因为你不应该每次都需要所有这些库...

编辑2:(之后)
关于第一点:

可能有不同的原因,为什么库不能用作外部库:


  1. 您在该文件夹中没有正确的权限

  2. 您的类路径中有多个版本的库。删除那些其他引用,然后再试一次

正如我已经提到的,只需将其添加到类路径就不会自动提供链接的srcs和文档。我建议您查看上面的链接。使用这种技术,您可以半自动实现您的目标。

但是,如果您的谷歌关于外部罐子和日食,你会遇到很多人有与您有相同的问题。所以通常应该开箱即用。你会介意更新你的日食吗?
另一个编辑:
我完全忘记了您正在编写Android应用程序的事实。那是一个很特别的例子。有关这个问题的许多主题。也许你也应该查看这一个。使用ADT17将库放入库中会触发自动导入。这就是为什么它为你这样工作(当你放在/ libs)


I am using afreechart and edu.emory.mathcs.jtransforms and org.afree.chart in numerous different activities I am writing for Android. I have a tremendous amount of trouble getting each new project configured so that I can

  1. "See" the classes from the library as I write code in Eclipse IDE.
  2. See attached source for these classes
  3. See attached javadoc for these classes
  4. See the .class files for these classes when I try to run my Activity.

What I do now is make a copy of the .jar, doc src and .properties for each library I use. I copy those four things into the local lib folder for each project (one per activity) I am creating.

I must be missing something simple. It seems obvious that I should have these libraries, with their doc, src, and .properties, in one location, and that I should be able to reference that location in each project that uses those libraries. But I haven't figure out yet how to do that. Please tell me how.

If there is a way to define a set of jars and associated src & doc that will automatically and always be searched by any new eclipse project I do, that would be even better.

EDIT ADDED After answers about external Libs:

So indeed I did put jtransforms-2.4.jar in an external directory. In that same directory I put the src and doc for the package, and a file jtransforms-2.4.properties with a doc and src entry pointing at the local files with doc and src. I added them to my build path as external jar.

1) Eclipse IDE certainly found the classes in the .jar and flagged nothing as missing. 2) The doc and the src were shown as not attached. How do I put doc and src info along with the library so they automatically show up every place I link the jar as an external jar? 3) When I attempted to run the app, I got "NoClassDefFound errors for all the classes in the jar. That is, Eclipse IDE could find these classes in the external lib, but they did not make it in to the class path of the app.

SUMMARY: so the "external jar" technique has failed in two out of three important respects: 1) No src and doc show up as attached in eclipse IDE 2) do not provide updated class paths to the App.

FINALLY: when I copied the jar, .properties, doc and src in to the lib folder of my project, and removed the external jar reference, everything worked perfectly.

SO I reiterate my original question, the answers given (add to build path as external jar) did not solve anything.

解决方案

They keyword here is external. You can add library to your project without having them in your local lib folder. You just need to add them as "External Archives".

You can also add and alter them via your project's properties.
Although I dont think it's a good idea, to add some librarys to every project you create, there may be a script for that, but imo it's not a build-in function.
EDIT:
Well, it's not really built-in and it looks quit hackish to me, but this thread may be useful to you. It contains a way you can adress your problems via a properties file. You just need to create this file once and add it to your projects. So this is semi-automatic ;)
But I think this solution is better than adding it to every project since you shouldn't need all those libraries every single time...
EDIT 2: (after yours) As to the 1st point:
There may be different reasons why the library doesnt work as an external library:

  1. You don't have the right permissions in that folder
  2. You have multiple versions of your library in your classpath. Remove those other references and then try again

As I already mentioned, just adding it to the class path doesn't automatically provides linked srcs and docs. I recommend checking out the link above. With that technique you might achieve your goal semi-automatically.
But if you google about external jars and eclipse you encounter many people having the same issue as you have. So normally it should work out of the box. Would you mind updating your eclipse? Another edit: I totally forgot about the fact you are writing an Android app. Well, that's quite a special case. There many topics about this issue. Maybe you should also check out this one. With ADT17 placing you library in libs is triggering an automatic import. That's why it works this way for you.(When you place it in /libs)

这篇关于如何将外部库包含在多个项目中,而不需要复制多个副本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆