1
votes

When building an APK for Android, the DX tool fails in case you attempt to add the same library twice with an error such as this:

UNEXPECTED TOP-LEVEL EXCEPTION: java.lang.IllegalArgumentException: already added: Lcom/google/ads/AdRequest$Gender; at com.android.dx.dex.file.ClassDefsSection.add(ClassDefsSection.java:122) at com.android.dx.dex.file.DexFile.add(DexFile.java:161) at com.android.dx.command.dexer.Main.processClass(Main.java:685) at com.android.dx.command.dexer.Main.processFileBytes(Main.java:634) at com.android.dx.command.dexer.Main.access$600(Main.java:78) at com.android.dx.command.dexer.Main$1.processFileBytes(Main.java:572) at com.android.dx.cf.direct.ClassPathOpener.processArchive(ClassPathOpener.java:284) at com.android.dx.cf.direct.ClassPathOpener.processOne(ClassPathOpener.java:166) at com.android.dx.cf.direct.ClassPathOpener.processDirectory(ClassPathOpener.java:229) at com.android.dx.cf.direct.ClassPathOpener.processOne(ClassPathOpener.java:158) at com.android.dx.cf.direct.ClassPathOpener.process(ClassPathOpener.java:144) at com.android.dx.command.dexer.Main.processOne(Main.java:596) at com.android.dx.command.dexer.Main.processAllFiles(Main.java:498) at com.android.dx.command.dexer.Main.runMonoDex(Main.java:264) at com.android.dx.command.dexer.Main.run(Main.java:230) at com.android.dx.command.dexer.Main.main(Main.java:199) at com.android.dx.command.Main.main(Main.java:103) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:483) at SDKMain.main(SDKMain.java:129) 1 error; aborting

What if i have 2 different libraries (jars) that contain classes with the same package name and name ?

How can i add these into my APK? Is there any way to do this ?

1

1 Answers

0
votes

First please note that the "already added" error may pop up even if the two jars are not in the same project. For example: linking your project to a library project containing a jar with same classes.

Secondly: if you do have two jars with the same class file the build system will simply use the first one that it sees. So, if you know which of them you want your runtime to use, the solution is simple: just place it earlier in the classpath. Or visually speaking - place it higher in the Order and Export list:

enter image description here


Thirdly: if you really have no other way, you can always open a jar file (e.e. using WinRAR) and delete some of its class files.