You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何排除外部库Java文件?解决Dex重复定义异常方案咨询

Hey fellow Android dev! Let's break down your two build questions step by step—they're both super common issues, so let's get you sorted.

1. How to exclude Java files from external libraries?

First off, the approach depends on whether you're dealing with a binary library (like an AAR/JAR from Maven) or a source-based library (like a local module or Git dependency):

  • For binary libraries (AAR/JAR): You can't exclude individual source files during compilation, but you can skip specific class files when packaging your APK. Use packagingOptions in your app-level build.gradle:
android {
    packagingOptions {
        // Exclude the main class
        exclude 'android/support/v7/widget/DefaultItemAnimator.class'
        // Don't forget inner classes if they exist (like the $1 in your error)
        exclude 'android/support/v7/widget/DefaultItemAnimator$1.class'
    }
}
  • For source-based libraries: If you're pulling in a library's source code directly, you can exclude specific Java files via sourceSets:
android {
    sourceSets {
        main {
            java {
                exclude 'path/to/the/external/library/UnwantedClass.java'
            }
        }
    }
}
2. Fixing the "Multiple dex files define DefaultItemAnimator$1" error

First, to answer your question: You can't directly exclude a single class from a Gradle dependency—Gradle's exclude logic only works for entire modules/libraries, not individual classes. But don't worry, there are way better fixes for this duplicate dex issue:

Option 1: Force all support libraries to use the same version

The root cause here is that com.google.android.support:wearable:1.0.0 is pulling in an older version of the support libraries, conflicting with your explicit recyclerview-v7:26.1.0 dependency. Force a unified version across all dependencies by adding this to your root-level build.gradle:

allprojects {
    repositories {
        // Your existing repo config (Google, Maven Central, etc.)
    }
    configurations.all {
        resolutionStrategy {
            // Force the versions you need
            force 'com.android.support:recyclerview-v7:26.1.0'
            force 'com.android.support:support-core-ui:26.1.0' // This is where DefaultItemAnimator lives
        }
    }
}

This tells Gradle to ignore any older versions of these libraries and use your specified version instead.

Option 2: Exclude conflicting support dependencies from wearable

Another approach is to block the wearable library from pulling in its own support libraries, since you're already adding the newer version manually. Update your app-level build.gradle dependencies:

dependencies {
    implementation('com.google.android.support:wearable:1.0.0') {
        // Exclude the conflicting support modules
        exclude group: 'com.android.support', module: 'recyclerview-v7'
        exclude group: 'com.android.support', module: 'support-core-ui'
    }
    implementation 'com.android.support:recyclerview-v7:26.1.0'
}

This way, only your explicitly declared recyclerview-v7 version is included in the build.

Option 3: Use pickFirst as a last resort

If the above options don't work for some reason, you can tell Gradle to pick the first instance of the conflicting class it finds (instead of throwing an error). Add this to your app-level build.gradle:

android {
    packagingOptions {
        pickFirst 'android/support/v7/widget/DefaultItemAnimator.class'
        pickFirst 'android/support/v7/widget/DefaultItemAnimator$1.class'
    }
}

Note: This is a band-aid solution—it doesn't fix the root version conflict, so use it only if the first two options aren't feasible.


内容的提问来源于stack exchange,提问作者Sudipta Seal

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 08:23:45