Program type already present: org.hamcrest.CoreMat

2019-06-22 15:49发布

问题:

When i build my app, I got follow error.

Program type already present: org.hamcrest.CoreMatchers

Message{kind=ERROR, text=Program type already present: org.hamcrest.CoreMatchers, sources=[Unknown source file], tool name=Optional.of(D8)}

I have google my problem and fond the similar question here , but can't get the proper answer .

This is my build.gradle file

apply plugin: 'com.android.application'

android {
    compileSdkVersion 27
    useLibrary 'org.apache.http.legacy'
    defaultConfig {
        minSdkVersion 15
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        multiDexEnabled true
        ndk {
            ndk {
                moduleName "exec"
                abiFilters 'armeabi-v7a'
            }
        }
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    sourceSets {
        main {
            jni.srcDirs = []
            jniLibs.srcDirs = ['libs']
            res.srcDirs = ['src/main/res']
            assets.srcDirs = ['src/main/assets']
            manifest.srcFile 'src/main/AndroidManifest.xml'
        }
    }
    configurations.all{
        resolutionStrategy.force 'org.hamcrest:hamcrest-core:1.1'
    }

}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'com.android.support:appcompat-v7:27.1.1'
    implementation 'com.j256.ormlite:ormlite-core:4.41'
    implementation 'com.j256.ormlite:ormlite-android:4.41'
    implementation 'com.googlecode.json-simple:json-simple:1.1.1'
    implementation 'org.mozilla:rhino:1.7.10'
}

Thanks for any help!

回答1:

Download jar file of json-simple 1.1.1 and put it in libs folder.

And remove implementation 'com.googlecode.json-simple:json-simple:1.1.1' from build.gradle.

I don't know why this solves the problem because I don't know why this problem occurs at all.



回答2:

In my case, I encountered a similar error saying like that.

I figured out by syncing my app gradle. It turned out that I modified app gradle code and didn't sync but built the project.



回答3:

I have found that excluding the dependent JUnit from json-simple solves this issue as well:

implementation('com.googlecode.json-simple:json-simple:1.1.1') {
    exclude group: 'junit', module: 'junit'
}

The latest version of simple-json depends on JUnit 4.1, which depends on hamcrest-core:1.1 which in my case conflicted with the existing hamcrest-core:1.3.