error while creating PDF

2019-05-23 23:34发布

问题:

I am trying to generate a PDF using Android Studio. I am using the iText library, but when I add it into Android studio, I get the following error:

Error:Execution failed for task ':app:dexDebug'. com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command 'C:\Program Files\Java\jdk1.7.0_79\bin\java.exe'' finished with non-zero exit value 2

When I remove the library, my project successfully compiles. It works as expected when using Eclipse instead of Android Studio.

EDIT

Here is my build.gradle file

apply plugin: 'com.android.application'

android {
    compileSdkVersion 22
    buildToolsVersion "22.0.1"

    defaultConfig {
        applicationId "com.abc"
        minSdkVersion 9
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }
}

dependencies {

    compile project(':Wheel')
    compile project(':facebook')
    compile files('libs/mint-4.0.8.jar')
    compile 'com.android.support:support-v4:22.1.0'
    compile 'com.google.android.gms:play-services:7.0.0'
    compile 'com.android.support:appcompat-v7:22.1.0'
    compile 'com.google.code.gson:gson:2.3.1'
    compile 'com.android.support:recyclerview-v7:22.1.0'
    compile 'com.squareup.picasso:picasso:2.5.2'
    compile files('libs/itextpdf-5.5.6.jar')
}

And error list instead of line posted

UNEXPECTED TOP-LEVEL EXCEPTION:
com.android.dex.DexIndexOverflowException: Cannot merge new index 66843 into a non-jumbo instruction!
    at com.android.dx.merge.InstructionTransformer.jumboCheck(InstructionTransformer.java:109)
    at com.android.dx.merge.InstructionTransformer.access$800(InstructionTransformer.java:26)
    at com.android.dx.merge.InstructionTransformer$StringVisitor.visit(InstructionTransformer.java:72)
    at com.android.dx.io.CodeReader.callVisit(CodeReader.java:114)
    at com.android.dx.io.CodeReader.visitAll(CodeReader.java:89)
    at com.android.dx.merge.InstructionTransformer.transform(InstructionTransformer.java:49)
    at com.android.dx.merge.DexMerger.transformCode(DexMerger.java:842)
    at com.android.dx.merge.DexMerger.transformMethods(DexMerger.java:813)
    at com.android.dx.merge.DexMerger.transformClassData(DexMerger.java:785)
    at com.android.dx.merge.DexMerger.transformClassDef(DexMerger.java:682)
    at com.android.dx.merge.DexMerger.mergeClassDefs(DexMerger.java:542)
    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)
Error:Execution failed for task ':app:dexDebug'.
> com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command 'C:\Program Files\Java\jdk1.7.0_79\bin\java.exe'' finished with non-zero exit value 2

回答1:

I faced a similar problem yesterday. Just add this to your build.gradle file and clean/invalidate your project =)

android {

    ....

    dexOptions {

        jumboMode = true
    }
}


回答2:

I used iTextG library as suggesed by "Bruno Lowagie" in comment and Remove unused libraries.

The com.android.dex.DexIndexOverflowException: exception is arrived because

As per android developer site

The Dalvik Executable specification limits the total number of methods that can be referenced within a single DEX file to 65,536, including Android framework methods, library methods, and methods in your own code. 

https://developer.android.com/tools/building/multidex.html

But with this method we got out of memory issue. To fix this we have to add in android closure.

dexOptions { 
   incremental true 
   javaMaxHeapSize "4g" 
}

http://www.mutualmobile.com/posts/dex-64k-limit-not-problem-anymore-almost