Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 703
makaron Avatar asked Feb 06 '12 17:02

makaron


2 Answers

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.

like image 200
Ricardo Gladwell Avatar answered Oct 01 '22 10:10

Ricardo Gladwell


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

If I'm not wrong, when you go to eclipse's Run->Run Configurations and create an Android Application to launch your project, you basically ask eclipse (Android SDK) to build the .apk with Ant for 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 .pom files, Ant can't build the project and gives the errors of this kind.

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

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.

like image 22
makaron Avatar answered Oct 01 '22 10:10

makaron