Including iTextG in an Android Project using Andro

2020-05-09 18:51发布

问题:

I'm getting the following error when including iTextG in Android Studio,

com.android.dex.DexException: Multiple dex files define Lcom/itextpdf/awt/geom/AffineTransform;
    at com.android.dx.merge.DexMerger.readSortableTypes(DexMerger.java:596)
    at com.android.dx.merge.DexMerger.getSortedTypes(DexMerger.java:554)
    at com.android.dx.merge.DexMerger.mergeClassDefs(DexMerger.java:535)
    at com.android.dx.merge.DexMerger.mergeDexes(DexMerger.java:171)
    at com.android.dx.merge.DexMerger.merge(DexMerger.java:189)
    at com.android.dx.command.dexer.Main.mergeLibraryDexBuffers(Main.java:454)
    at com.android.dx.command.dexer.Main.runMonoDex(Main.java:303)
    at com.android.dx.command.dexer.Main.run(Main.java:246)
    at com.android.dx.command.dexer.Main.main(Main.java:215)
    at com.android.dx.command.Main.main(Main.java:106)

I don't know why but seems that there is awt in this library too. This is how i'm including it:

  1. Download itextg from http://sourceforge.net/projects/itextg/
  2. Extract the jar file
  3. Copy itextg-5.5.4.jar to the libs folder in the app directory
  4. Gradle clean

Why is the awt still in the library ?

回答1:

Please read the exception carefully. It talks about com.itextpdf.awt. That is not the same as the forbidden java.awt.

Your allegation that awt is still in the library is wrong. The com.itextpdf.awt package is there to replace the forbidden AWT classes. Those replacement classes as such are perfectly valid and can be used on Android.

The problem you are experiencing is explained in the error message: the class com.itextpdf.awt.geom.AffineTransform is defined in different DEX files. This means that you are using more than just iTextG 5.5.x. You have more than one iTextG on your device. Remove them all and make sure you add only a single instance of iTextG.