8

I'm using Gradle in my Android application an I would like to use the JScience library dependency. I have added the library this way:

dependencies {
    compile fileTree(dir: 'libs', include: '*.jar')

    compile 'org.jscience:jscience:4.3.1'

    testCompile group: 'junit', name: 'junit', version: '4.11'
}

But in the compile time I get the error:

UNEXPECTED TOP-LEVEL EXCEPTION:
com.android.dex.DexException: Multiple dex files define Ljavax/realtime/MemoryArea;
    at com.android.dx.merge.DexMerger.readSortableTypes(DexMerger.java:594)
    at com.android.dx.merge.DexMerger.getSortedTypes(DexMerger.java:552)
    at com.android.dx.merge.DexMerger.mergeClassDefs(DexMerger.java:533)
    at com.android.dx.merge.DexMerger.mergeDexes(DexMerger.java:170)
    at com.android.dx.merge.DexMerger.merge(DexMerger.java:188)
    at com.android.dx.command.dexer.Main.mergeLibraryDexBuffers(Main.java:439)
    at com.android.dx.command.dexer.Main.runMonoDex(Main.java:287)
    at com.android.dx.command.dexer.Main.run(Main.java:230)
    at com.android.dx.command.dexer.Main.main(Main.java:199)
    at com.android.dx.command.Main.main(Main.java:103)

This is caused because of duplicating javax.realtime packages in project, one is a part of JDK, and the second one is in the Jscience library. I have tried to remove this package from Jscience library this way in Gradle:

sourceSets {
    main {
        java {
            exclude 'javax/realtime/**'
        }
    }
}

configurations {
    all*.exclude group: 'javax.realtime'
}

But that didn't help. So, that package is still exists in dependencies. enter image description here

Is there any way how I can exclude a package from jar on compile time?

yyunikov
  • 5,719
  • 2
  • 43
  • 78
  • 2
    "one is a part of Java" -- there is no `javax.realtime` in Android. The fact that a JDK's class library has `javax.realtime` is meaningless, as you are not using a JDK's class library when you are compiling an Android app. – CommonsWare May 23 '14 at 21:17
  • Thanks for the answer, you are 100% correct, my mistake. The problem was in dependency for javolution (which comes from JScience library), because it contains `javax.realtime` package. – yyunikov May 24 '14 at 05:22

1 Answers1

17

In case anybody needed, the problem was in Javolution dependency from JScience library. They both do hava a javax.runtime package. Excluding the Javolution has fixed the issue for me.

compile ('org.jscience:jscience:4.3.1') {
    exclude group: 'org.javolution', module: 'javolution'
}
yyunikov
  • 5,719
  • 2
  • 43
  • 78
  • Thank you for this. I couldn't find the overlapping dependencies per the usual route `gradle -q app:dependencies` -> what command did you use to dig into the application if I can ask? – AllDayAmazing Apr 05 '15 at 05:06
  • I don't remember already but I think that maybe some kind of `gradle dependencies` command or dependency tree tool of Intellij Idea – yyunikov Apr 05 '15 at 14:16
  • Thank you for this. This resolved my issue with "java.exe finished with non-zero exit value 2".. – Ralphilius Apr 18 '15 at 09:34
  • Please note that exclude function [(see ref doc)](https://docs.gradle.org/current/javadoc/org/gradle/api/artifacts/ModuleDependency.html) has appeared in gradle 2.10, you might need to update your graddle wrapper. – Nicolas Bossard Feb 09 '16 at 14:46