Android Studio编译报错:Multiple dex files define Ljava_cup/runtime/ComplexSymbolFactory
Hey there, let's break down what's causing that dex error in your Weka-Android project and fix your build.gradle setup properly.
The com.android.dex.DexException: Multiple dex files define Ljava_cup/runtime/ComplexSymbolFactory error happens because the same Java class is being included multiple times in your project's dependencies.
Specifically, the Maven dependency nz.ac.waikato.cms.weka:weka-stable:3.8.0 already includes classes from the java-cup library (which contains the ComplexSymbolFactory class). However, somewhere in your dependency tree (either directly or via another library), the same java-cup classes are being pulled in again—either in a different version, or as a duplicate inclusion.
Your previous attempts didn't work because:
multiDexEnabled trueis designed to fix the "method count exceeds 65536" issue, not duplicate class definitions.- Cleaning and rebuilding only refreshes build caches, it doesn't resolve underlying dependency conflicts.
Instead of falling back to a local Weka 3.6.6 JAR, you can resolve this while keeping the Maven dependency with either of these two approaches:
Option 1: Exclude the Duplicate java-cup Dependency from Weka
Modify your dependencies block to exclude the java-cup module that's causing the conflict:
implementation ('nz.ac.waikato.cms.weka:weka-stable:3.8.0') { // Try this exclusion first (matches Weka 3.8.0's dependency structure) exclude group: 'net.sf.squirrel-sql.thirdparty-non-maven', module: 'java-cup' // If the above doesn't work, use this more generic rule: // exclude group: 'java-cup', module: 'java-cup' }
This tells Gradle to omit the duplicate java-cup classes from the Weka dependency, ensuring only one copy of the class exists in your build.
Option 2: Force a Single Version of java-cup
If exclusion doesn't resolve the conflict, you can force all dependencies to use the same version of java-cup. Add this block inside your build.gradle file (outside the android block):
configurations.all { // Replace with the exact version used by Weka 3.8.0 (check dependency tree first) resolutionStrategy.force 'java-cup:java-cup:0.11a' }
To find the correct version, run this command in your project's Terminal:
# For macOS/Linux ./gradlew app:dependencies # For Windows gradlew app:dependencies
Look through the output to find the version of java-cup that Weka 3.8.0 depends on, then use that version in the force rule.
The older Weka 3.6.6 JAR likely doesn't include the java-cup classes internally, or its dependency chain doesn't pull in a duplicate copy of the library. While this works, using Maven dependencies is better for version management and automatic updates—so fixing the 3.8.0 dependency is the cleaner long-term solution.
内容的提问来源于stack exchange,提问作者Barbarian772

