transform.TransformException 在 Android

transform.TransformException in Android

我在 运行 时尝试使用 iText 生成 PDF 时出现错误。 当应用程序在设备或 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.

我的Gradle代码是:

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'

请帮我解决这个问题。

duplicate entry: com/lowagie/bc/asn1/ASN1Encodable.class - 指向 iText 2.1.7 或更早版本,不知道它与 Android.

兼容

您的 Gradle 文件中有几处错误:

  • compile 'com.itextpdf:itext-pdfa:5.5.11' - 指向 iText 5 版本 5.5.11 的 PDF/A 插件,它与 Android 兼容,但您仍然需要 Android iText 5 的端口,称为 iTextG。所以你需要compile 'com.itextpdf:itextg:5.5.10'。是的,5.5.10,不是5.5.11,因为没有itextg:5.5.11。参见 http://repo1.maven.org/maven2/com/itextpdf/itextg/
  • 猜测您认为 "pdfa" 中的 "a" 表示 Android,但事实并非如此。它代表 PDF/A:用于存档的 PDF 规范 (ISO 19005),请参阅 https://en.wikipedia.org/wiki/PDF/A。如果您不需要存档(在典型的 Android 应用程序中,您可能不需要),那么您可能需要 remove compile 'com.itextpdf:itext-pdfa:5.5.11'
  • compile 'com.itextpdf:itext7-core:7.0.3' - 这是 iText 7,与 Android 不兼容。删除该行。
  • compile 'itext:itext:1.3.1' - 是 iText 的旧版本,可能是导致错误的原因。删除该行。
  • compile 'org.xhtmlrenderer:flying-saucer-pdf-itext5:9.1.6' - 这很特别,我还不太了解。它可能会或可能不会引入 iText 的另一个依赖项,这可能是也可能不是错误的版本。用 flying-saucer 标记您的问题以引起更多关注。