6

I have used this guide to build persistence with Room in my Android App: https://developer.android.com/training/data-storage/room/index.html

and added dependances like shown here: https://developer.android.com/topic/libraries/architecture/adding-components.html

when i build the debug version and deply to phone, everithing works fine.

When i build the release signed APK i got this error message:

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]

my 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"
}

my 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' }
    }
}

Somebody can help or give me clues?

Romeo
  • 1,135
  • 13
  • 26

4 Answers4

20

I finally found the problem was a JSON sub-module:

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

this library has a submodule:

org.json:json

that is now conflicting with android native module, because in my other dependancies i can't find this one. It was working fine 10 days ago. I also had to kill this:

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

the final solution was to add an exclude for the module and change the line like this:

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

I also have noticed AFTER i solved the problem that in the error log it was suggesting me the module that was in conflict but even if i read it a hundred times i didn't noticed before: enter image description here

so maybe google or Intellij could improve the writing of this errors...

To spot this class duplicate conflict error module i found the best way to proceed is to create a new project and paste in the dependancies in app build.gradle, and check them one by one or with "dividi et impera", maybe this is an obvious suggestion for someone but i would have like to have it sooner.

Romeo
  • 1,135
  • 13
  • 26
  • How did you find that the JSON-submodule was causing the problem? – Sergio Magoo Quintero Feb 23 '18 at 19:55
  • Was just facing the same problem, to find the "problematic" library, you can run ./gradlew app:dependencies, after that this solution worked for me – rotemitz Mar 01 '18 at 15:06
  • 1
    @SergioMagooQuintero "json" was the name of the submodule (in my case) the error was suggesting it but i did not notice because it says "...Error: json defines..." instead of '...Error: "json" defines...' – Romeo Mar 12 '18 at 16:35
  • Thanks 4 the answer, really useful because I was in same situation. Did you find any "gradlew" command, or similar, whose gives you all "ClassName" conflicts? (I was trying with "gradlew dependencies" but it just gives you 10k dependencies without conflicts and dependencies class names). Thanks you LOL Anyway sometimes Android makes me really miss times of back-end / front-end development only. I mean, man... So many errors for nothing and so many changes on every android release :/ (and sometimes really low information on build debugging) – Z3R0 Jan 27 '20 at 14:16
4

I had the same problem and I searched for the conflict via the gradle dependency tree:

gradlew app:dependencies

Then I excluded the the json module for the conflicting library:

implementation ('<conflicting-library>',{
     exclude group:'org.json', module:'json'
})
Manuel Schmitzberger
  • 5,162
  • 3
  • 36
  • 45
0

How to find the duplicate library.

open gradle run window and run such command:

gradle module-name:dependencies

the "module-name" should be your app module's name, for me, it's "osmunda-demo".

Screenshot

then use Ctrl+F to search "commons-logging", you'll find it.

enter image description here

Sun Jiaojiao
  • 321
  • 2
  • 10
0

@Romeo has suggested a really great point. Was unable to debug the code for hours. The problem lies within the dependencies that are imported in build.gradle. It may be your own custom sdk/artifact. I had issues with my own library which uses jjwt. I added the exclusion in my sdk but you will have to add it again whenever using the sdk. Make sure to add exclude group: 'org.json', module: 'json' in your artifact implementation.

  • 1
    It doesn't answer the question so it should not be posted as a question. I understand that you are complementing the answer with useful information but it is just o complement so it should be added as a comment in the accepted answer. Please [see this to learn more on how to write a good answer](https://stackoverflow.com/help/how-to-answer). – Iogui May 17 '21 at 17:19