1

Okay so currently i am making an Android app that utilizes the Google Sheets API version 3.0 and Drive API Client Library for Java, i need the app to read a spreadhsheet from a users drive account and edit it. Following the documentation i have included the following jars in my /lib folder(Android Studio 1.1).
dependencies { compile fileTree(include: ['*.jar'], dir: 'libs') compile 'com.android.support:appcompat-v7:21.0.3' compile 'com.google.android.gms:play-services:6.5.87' compile files('libs/google-api-client-1.19.1.jar') compile files('libs/google-api-client-android-1.19.1.jar') compile files('libs/google-api-services-drive-v2-rev158-1.19.1.jar') compile files('libs/google-http-client-1.19.0.jar') compile files('libs/google-http-client-android-1.19.0.jar') compile files('libs/google-http-client-gson-1.19.0.jar') compile files('libs/google-oauth-client-1.19.0.jar') compile files('libs/gson-2.1.jar') compile files('libs/jackson-core-2.1.3.jar') compile files('libs/jackson-core-asl-1.9.11.jar') compile files('libs/jsr305-1.3.9.jar') compile files('libs/protobuf-java-2.4.1.jar') compile files('libs/mail.jar') compile files('libs/jsr305.jar') compile files('libs/guava-11.0.2.jar') compile files('libs/gdata-spreadsheet-meta-3.0.jar') compile files('libs/gdata-spreadsheet-3.0.jar') compile files('libs/gdata-docs-meta-3.0.jar') compile files('libs/gdata-docs-3.0.jar') compile files('libs/gdata-core-1.0.jar') compile files('libs/gdata-client-meta-1.0.jar') compile files('libs/gdata-client-1.0.jar') compile files('libs/additionnal.jar') compile files('libs/activation.jar') }

When im typing my code everything works just fine, everything is imported appropiately but when i run my app i get this result.

`What went wrong:
Execution failed for task ':app:dexDebug'.
> com.android.ide.common.internal.LoggedErrorException: Failed to run command:
    C:\Users\Ricardo\AppData\Local\Android\sdk\build-tools\21.1.2\dx.bat --dex --output C:\Users\Ricardo\Google Drive\Development\Android\Updated\Private\Google_Fiasco\MyApplication\app\build\intermediates\dex\debug --input-list=C:\Users\Ricardo\Google Drive\Development\Android\Updated\Private\Google_Fiasco\MyApplication\app\build\intermediates\tmp\dex\debug\inputList.txt
Error Code:
    2
Output:

    UNEXPECTED TOP-LEVEL EXCEPTION:
    com.android.dex.DexException: Multiple dex files define Ljavax/annotation/CheckForNull;
        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)
e`<br>

Now a couple things to note, when i make two separate projects ,one implementing the Spreadsheet API and the other the Drive API no errors occur. The error only occurs when i implement both API's in the same project i.e combining the jars from both libraries. After tinkering with the jars i found that the error only happens when both jsr305.jar & jsr305-1.3.9.jar are included. Looking at these two jars i see that both have CheckForNull.java files, the file referenced in the error message Multiple dex files define Ljavax/annotation/CheckForNull.
So my question: How do i resolve this error? I think i have to exclude it in my dependencies? Precise instructions would be appreciated, and perhaps an explanation on what the solution does. Also one last thing What difference does it make between simply putting jar files in my /lib folder and adding it to my dependencies in build.gradle? It doesn't seem to affect anything. The result of deleting either of the jsr.jat files is the following:
UNEXPECTED TOP-LEVEL EXCEPTION: com.android.dex.DexIndexOverflowException: method ID not in [0, 0xffff]: 65536 at com.android.dx.merge.DexMerger$6.updateIndex(DexMerger.java:502) at com.android.dx.merge.DexMerger$IdMerger.mergeSorted(DexMerger.java:277) at com.android.dx.merge.DexMerger.mergeMethodIds(DexMerger.java:491) at com.android.dx.merge.DexMerger.mergeDexes(DexMerger.java:168) 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)

Liquid Ink
  • 521
  • 1
  • 5
  • 16
  • Do you really need two `jsr` jars? Aren't they the same library, just in different versions? – Lawrence Choy Feb 05 '15 at 05:21
  • I'm just following the documentation and anyway when ever I remove jar.jar I get a different error cause it seems Spreadsheet API needs that jar. – Liquid Ink Feb 05 '15 at 11:20
  • Remove one of the copies, not both of them. – Scott Barta Feb 05 '15 at 17:57
  • i posted the result of deleted either of the jar files in my edit – Liquid Ink Feb 05 '15 at 23:23
  • @LiquidInk This error is caused by having too many method referenced in your project. [You can read more here](https://developer.android.com/tools/building/multidex.html). Generally it means that you need to remove unused libraries or use proguard even on debug build to reduce the number of method referenced. You may also go with the multi-dex approached mentioned in the article but it makes your project more complicated. – Lawrence Choy Feb 06 '15 at 02:06
  • @Lawrence Choy You sir are amazing! I was about to just start from scratch but this worked. Thankyou so much! – Liquid Ink Feb 06 '15 at 02:25
  • @Lawrence if you post your suggestion as an answer i would be more than happy to award you the question. – Liquid Ink Feb 23 '15 at 23:37

3 Answers3

2

Just clean the project and build again... I knew my code had nothing to do with the bug as soon as I saw the word 'dex' in the error text

Naeem A. Malik
  • 995
  • 4
  • 19
1

This error is caused by having too many method referenced in your project. You can read more here.

Generally it means that you need to remove unused libraries or use proguard even on debug build to reduce the number of method referenced. You may also go with the multi-dex approached mentioned in the article but it makes your project more complicated.

Lawrence Choy
  • 6,088
  • 1
  • 20
  • 30
0

I had react-native project with app's build.gradle:

def enableProguardInReleaseBuilds = false

...

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

Setting enableProguardInReleaseBuilds to true worked for me:

def enableProguardInReleaseBuilds = true
Alexander Danilov
  • 3,038
  • 1
  • 30
  • 35