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

makaron picture makaron · Feb 6, 2012 · Viewed 19.5k times · Source

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:

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

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).

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.

Any idea would be appreciated.

Answer

Ricardo Gladwell picture Ricardo Gladwell · Feb 7, 2012

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 AvailabilityRequest class in your Maven dependencies.

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).

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.

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