在 Room 持久性上构建版本时发生错误 [DuplicatePlatformClasses] class 冲突

Error [DuplicatePlatformClasses] class conflict when building release on Room persistence

我已使用本指南在我的 Android 应用程序中使用 Room 建立持久性: https://developer.android.com/training/data-storage/room/index.html

并添加了如下所示的依赖项: https://developer.android.com/topic/libraries/architecture/adding-components.html

当我构建调试版本并提交给 phone 时,一切正常。

当我构建发布签名的 APK 时,我收到了这条错误消息:

Error:Error: json defines classes that conflict with classes now provided by Android. Solutions include finding newer versions or alternative libraries that don't have the same problem (for example, for httpclient use HttpUrlConnection or okhttp instead), or repackaging the library using something like jarjar. [DuplicatePlatformClasses]

我的app.gradle:

apply plugin: 'com.android.application'

android {
    signingConfigs {
        /* TODO(developer): Configure to sign app with a release key for testing.
        release {
            storeFile file('path/to/release/signing/key')
            keyAlias 'release_key_alias'
            keyPassword "${password}"
            storePassword "${password}"
        }*/
    }
    compileSdkVersion 26
    buildToolsVersion '26.0.2'

    defaultConfig {
        applicationId "myappid"
        minSdkVersion 14
        targetSdkVersion 23
        versionCode 10
        versionName "1.8"
    }
    buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            // TODO(developer): uncomment below once config above is complete and uncommented.
            //signingConfig signingConfigs.release

        }
    }
}
configurations {
    all {
        exclude module: 'httpclient'
    }
}
dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:26.1.0'
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    compile 'com.android.volley:volley:1.0.0'
    compile 'com.github.nkzawa:socket.io-client:0.3.0'
    compile 'io.socket:socket.io-client:0.8.3'
    compile 'com.android.support:design:26.1.0'
    compile 'android.arch.persistence.room:runtime:1.0.0'
    implementation "android.arch.persistence.room:runtime:1.0.0"
    annotationProcessor "android.arch.persistence.room:compiler:1.0.0"
}

我的project.gradle

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.0'
        //classpath 'io.socket:socket.io-client:0.8.3'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}
ext{
    roomVersion = '1.0.0'
}
allprojects {
    repositories {
        google()
        jcenter()
        maven { url 'https://maven.google.com' }
    }
}

有人可以帮忙或给我线索吗?

我终于发现问题是 JSON 子模块:

compile 'com.github.nkzawa:socket.io-client:0.3.0'

这个库有一个子模块:

org.json:json

现在与 android 本机模块冲突,因为在我的其他依赖项中我找不到这个。 10天前它工作正常。 我也不得不杀了这个:

compile 'io.socket:socket.io-client:0.8.3'

最终的解决方案是为模块添加一个排除项并像这样更改行:

    implementation ('com.github.nkzawa:socket.io-client:0.3.0',{
         exclude group:'org.json', module:'json'
    })

我也注意到在我解决了这个问题之后,在错误日志中它提示我有冲突的模块,但即使我读了一百遍我之前也没有注意到:

所以也许 google 或 Intellij 可以改进此错误的编写...

为了发现这个 class 重复冲突错误模块,我发现最好的方法是创建一个新项目并粘贴到应用程序 build.gradle 中的依赖项,然后一一检查或"dividi et impera",也许这对某人来说是一个明显的建议,但我希望早点得到它。

我有同样的问题,我通过 gradle 依赖树搜索冲突:

gradlew app:dependencies

然后我排除了冲突库的 json 模块:

implementation ('<conflicting-library>',{
     exclude group:'org.json', module:'json'
})

如何找到重复的库。

打开gradle运行window和运行这样的命令:

gradle module-name:dependencies

"module-name" 应该是您的应用程序模块的名称,对我来说,它是 "osmunda-demo"。

然后用Ctrl+F搜索"commons-logging",就可以找到了。

@Romeo 提出了一个非常好的观点。几个小时都无法调试代码。问题出在 build.gradle 中导入的依赖项中。这可能是您自己的习惯 sdk/artifact。我自己使用 jjwt 的库有问题。我在我的 sdk 中添加了排除项,但无论何时使用 sdk 都必须再次添加它。确保在您的工件实现中添加 exclude group: 'org.json', module: 'json'