eclipse 意外的顶级异常:java.lang.IllegalArgumentException:已添加

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9164005/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 17:40:57  来源:igfitidea点击:

UNEXPECTED TOP-LEVEL EXCEPTION: java.lang.IllegalArgumentException: already added

androideclipsemacosmavendalvik

提问by makaron

I'm new to Android, trying to launch the project, which is being built and deployed well. But every time I make a try to start the app from eclipse, I get this error:

我是 Android 新手,正在尝试启动该项目,该项目正在构建和部署良好。但是每次我尝试从 Eclipse 启动应用程序时,我都会收到此错误:

UNEXPECTED TOP-LEVEL EXCEPTION:
java.lang.IllegalArgumentException: already added: Lcom/.../model/AvailabilityRequest$DAY_TIME_PREFERENCE;
[2012-02-06 17:32:11 - main-app] Dx     at com.android.dx.dex.file.ClassDefsSection.add(ClassDefsSection.java:123)
[2012-02-06 17:32:11 - main-app] Dx     at com.android.dx.dex.file.DexFile.add(DexFile.java:163)
[2012-02-06 17:32:11 - main-app] Dx     at com.android.dx.command.dexer.Main.processClass(Main.java:486)
...
[2012-02-06 17:32:11 - main-app] Dx     at org.eclipse.core.internal.jobs.Worker.run(Worker.java:54)
[2012-02-06 17:32:11 - main-app] Dx 1 error; aborting
[2012-02-06 17:32:11 - main-app] Conversion to Dalvik format failed with error 1

I've killed enormous amount of time for this stupid thing. Obviously, this is known problem, but nothing works for me.
What I have:

我已经为这个愚蠢的事情浪费了大量的时间。显然,这是已知问题,但对我来说没有任何作用。
我拥有的:

Operation system - OS X, which is important I think;
IDE - eclipse 3.7.1
ADT - Version: 15.0.1.v201111031820-219398
Build tool - Maven

操作系统 - OS X,我认为这很重要;
IDE - eclipse 3.7.1
ADT - 版本:15.0.1.v201111031820-219398
构建工具 - Maven

What I've tried so far:
- Properties->Java Build Path->Libraries to remove all the libraries except Android x.y.z, then Maven->Update Project Configuration, also to clean the project;
- Magic with closing and reopening eclipse;
- Dances with deleting and recreating the project and the libraries for it (according to the text of error message, there is some duplication of the sources, which are in one of the 2 libraries, used in the main project).

到目前为止我尝试过的:
- Properties->Java Build Path->Libraries 删除除 Android xyz 之外的所有库,然后 Maven->Update Project Configuration,也清理项目;
- 关闭和重新打开日食的魔法;
- 删除和重新创建项目及其库(根据错误消息的文本,源代码存在一些重复,它们位于主项目中使用的两个库之一中)。

After one of the variants above (usually the 1st one) it starts to work, but I can spend 20 mins to make it work or several hours..., today I can't manage to do it at all.

在上述变体之一(通常是第一个)之后,它开始工作,但我可以花 20 分钟使其工作或几个小时......,今天我根本无法做到。

Any idea would be appreciated.

任何想法将不胜感激。

采纳答案by makaron

Well, as far as I understand, the main thing here was that I was using mavenas a building tool. I could build and deploy project without any problem, but I couldn't start it from the environment (eclipse).

好吧,据我所知,这里的主要事情是我使用maven作为构建工具。我可以毫无问题地构建和部署项目,但我无法从环境(eclipse)启动它。

If I'm not wrong, when you go to eclipse's Run->Run Configurationsand create an Android Applicationto launch your project, you basically ask eclipse (Android SDK) to build the .apk with Antfor you, not with maven. Eclipse builds the project with Ant and pushes the generated .apk to the device/simulator. But since all the settings of the project are in .pomfiles, Ant can't build the project and gives the errors of this kind.

如果我没记错的话,当您转到 eclipse 的Run->Run Configurations并创建一个Android 应用程序来启动您的项目时,您基本上是要求 eclipse(Android SDK)使用Ant为您构建 .apk ,而不是使用 maven。Eclipse 使用 Ant 构建项目并将生成的 .apk 推送到设备/模拟器。但是由于项目的所有设置都在.pom文件中,因此 Ant 无法构建项目并给出此类错误。

Solution:
Start your app right from the device/simulator after you deployed it, not through Runmenu. If you want to debugyour app, use DDMS' debug, not the one eclipse provides. In case you don't know where DDMS is (like me in my case) - in Macit's Window->Open Perspective->Other...choose DDMS from menu; you can put it as a bookmark together with eclipse's Debugand Java. In DDMS you'll find all the cool tools, including debug.

解决方案:
在部署后直接从设备/模拟器启动您的应用程序,而不是通过运行菜单。如果您想调试您的应用程序,请使用 DDMS 的调试,而不是 eclipse 提供的调试。如果您不知道 DDMS 在哪里(就像我的情况一样) - 在Mac 中它是窗口->打开透视图->其他...从菜单中选择 DDMS;您可以将其作为书签与 eclipse 的DebugJava一起放置。在 DDMS 中,您会找到所有很酷的工具,包括调试。

P.S. This is a response from a beginner and for beginners... If you see something where I'm wrong, please, let me know or feel free to edit my answer.

PS 这是初学者和初学者的回应...如果您发现我错的地方,请告诉我或随时编辑我的答案。

回答by Ricardo Gladwell

The ADT will throw an exception like this if your Eclipse classpath contains more than one class of the same name/package. In this case it is encountering more than one instance of the AvailabilityRequestclass in your Maven dependencies.

如果您的 Eclipse 类路径包含多个同名/包的类,ADT 将抛出这样的异常。在这种情况下,它AvailabilityRequest在您的 Maven 依赖项中遇到了多个类的实例。

You can resolve this by finding which classpath dependencies contain the same class files in them (hitting Ctrl-Alt-T and typing in AvailabilityRequest will do this).

您可以通过查找哪些类路径依赖项中包含相同的类文件来解决此问题(按 Ctrl-Alt-T 并键入 AvailabilityRequest 将执行此操作)。

You can then open your POM in the m2e POM editor and go to the Dependency Hierarchy tab. This will allow you to select the extraneous dependency, which you can then exclude by right-clicking and selecting "Exclude Maven Artifact..." which will automatically add an <exclusions>element to your POM. This will remove the duplicate JAR from your Eclipse classpath and allow you to build you project.

然后您可以在 m2e POM 编辑器中打开您的 POM 并转到 Dependency Hierarchy 选项卡。这将允许您选择无关的依赖项,然后您可以通过右键单击并选择“排除 Maven 工件...”来排除该依赖项,这将自动<exclusions>向您的 POM添加一个元素。这将从您的 Eclipse 类路径中删除重复的 JAR,并允许您构建您的项目。

Also, you should be careful about what dependencies you add to your POM.

此外,您应该注意添加到 POM 中的依赖项。