Search code examples
androiditextflying-saucer

transform.TransformException in Android


I get an error when I try to use iText to generate a PDF at run time. The error comes when the Application is run in device or geny Motion.

Error:Execution failed for task ':app:transformClassesWithJarMergingForDebug'.
com.android.build.api.transform.TransformException: java.util.zip.ZipException: duplicate entry: com/lowagie/bc/asn1/ASN1Encodable.class.

My Gradle Code is :

apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.1"
    useLibrary 'org.apache.http.legacy'
    defaultConfig {
        applicationId "com.visioneering.tfd"
        minSdkVersion 15
        targetSdkVersion 24
        versionCode 5
        versionName "1.5"
        multiDexEnabled true

    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    dexOptions {
        preDexLibraries = false
        javaMaxHeapSize "4g"
    }
    android {
        lintOptions {
            checkReleaseBuilds false
        }
    }
    packagingOptions {
        exclude 'META-INF/DEPENDENCIES.txt'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/notice.txt'
        exclude 'META-INF/license.txt'
        exclude 'META-INF/dependencies.txt'
        exclude 'META-INF/LGPL2.1'
    }

}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.android.support:appcompat-v7:25.2.0'
    compile 'com.android.support:design:25.2.0'
    compile 'com.google.firebase:firebase-core:10.2.1'
    compile 'com.google.firebase:firebase-messaging:10.2.1'
    compile 'com.github.florent37:materialtextfield:1.0.5'
    compile 'com.android.support:cardview-v7:25.2.0'
    compile 'com.android.support:support-v4:25.2.0'
    compile 'com.google.android.gms:play-services:10.2.1'
    compile 'com.fasterxml.jackson.core:jackson-databind:2.5.3'
    compile 'com.googlecode.json-simple:json-simple:1.1'
    compile 'com.weiwangcn.betterspinner:library-material:1.1.0'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    compile 'com.itextpdf:itext7-core:7.0.3'
    compile 'com.itextpdf:itext-pdfa:5.5.11'
    compile 'itext:itext:1.3.1'
    compile 'org.xhtmlrenderer:flying-saucer-pdf-itext5:9.1.6'

}

// ADD THIS AT THE BOTTOM
apply plugin: 'com.google.gms.google-services'

Please help me to solve this problem.


Solution

  • duplicate entry: com/lowagie/bc/asn1/ASN1Encodable.class - that points to iText 2.1.7 or older, which is not known to be compatible with Android.

    There are a couple of things wrong in your Gradle file:

    • compile 'com.itextpdf:itext-pdfa:5.5.11' - that points to the PDF/A add-on of iText 5, version 5.5.11, which is compatible with Android, but you still need the Android port of iText 5, which is called iTextG. So you need to add compile 'com.itextpdf:itextg:5.5.10'. Yes, 5.5.10, not 5.5.11, because there is no itextg:5.5.11. See http://repo1.maven.org/maven2/com/itextpdf/itextg/
    • I am guessing that you think the "a" in "pdfa" means Android, but it doesn't. It stands for PDF/A: the PDF spec for archiving (ISO 19005), see https://en.wikipedia.org/wiki/PDF/A. If you don't need archiving (you probably don't, in a typical Android app), so you probably need to remove compile 'com.itextpdf:itext-pdfa:5.5.11'
    • compile 'com.itextpdf:itext7-core:7.0.3' - this is iText 7, which is not compatible with Android. Remove that line.
    • compile 'itext:itext:1.3.1' - is an ANCIENT version of iText, and is probably what is causing your error. Remove that line.
    • compile 'org.xhtmlrenderer:flying-saucer-pdf-itext5:9.1.6' - this is something special, which I don't know enough about. It may or may not pull in another dependency of iText, which may or may not be the wrong version. Tag your question with flying-saucer to attract extra attention.