android - 在 Room 持久性上构建版本时出现错误 [DuplicatePlatformClasses] 类冲突

标签 android gradle android-gradle-plugin android-manifest android-room

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

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

当我构建调试版本并部署到手机时,一切正常。

当我构建发布签名的 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]

我的应用程序.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"
}

我的项目.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 native 模块冲突,因为在我的其他依赖项中我找不到这个。 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'
    })

我还注意到,在我解决了问题之后,错误日志中提示我存在冲突的模块,但即使我阅读了一百遍,我之前也没有注意到: enter image description here

所以也许谷歌或 Intellij 可以改进这个错误的编写......

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

关于android - 在 Room 持久性上构建版本时出现错误 [DuplicatePlatformClasses] 类冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47337218/

相关文章:

当项目位于 Github 的子文件夹中时,Android Studio 构建错误

android - 将 firestore、androidx 库和 google-services 插件添加到最新 (4.1.0) 后无法构建项目

java - Sqlite插入和更新其他列的值

maven - Gradle Maven 插件为自定义配置生成不正确的 POM 依赖项

Android画中画比例 View

java - 如何将 Eclipse 项目文件夹结构更改为 Maven/Gradle 结构

java - 修改现有的 gradle 任务

javascript - 找不到 play-services-tasks-license.aar (com.google.android.gms :play-services-tasks-license:11. 8.0)

android - Flutter - 将 #(哈希)和 @(提及)符号添加到 native 键盘

android - 找不到 Gradle DSL 方法 : 'androidTestImplementation()'