Android Studio - Program type already present: org.hamcrest.CoreMatchers
Asked Answered
C

2

10

I have no idea why this error exists:

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)}

My code in in the scope if dependency of build.gradle (Module: app) is:

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
testImplementation ('junit:junit:4.12'){
    exclude group: 'org.hamcrest', module: 'hamcrest-core'
}
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
implementation 'com.monkeylearn:monkeylearn-java:0.1.4'

}

MainActivity:

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.monkeylearn.MonkeyLearn;
import com.monkeylearn.MonkeyLearnException;
import com.monkeylearn.MonkeyLearnResponse;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        try {
            MonkeyLearn ml = new MonkeyLearn("*******************************");
            String moduleId = "*********";
            String[] textList = {"This is a text to test your classifier", "This is some more text"};
            MonkeyLearnResponse res = ml.classifiers.classify(moduleId, textList, true);
            System.out.println( res.arrayResult );
        } catch (MonkeyLearnException e) {
            e.printStackTrace();
        }
    }
}

Do you have any ideas?

Cogan answered 15/4, 2018 at 12:3 Comment(3)
Are you seeing this error when you are trying to run tests are run your app?Semiology
This error exists when I try to build the project. Then, it cannot be built successfully.Cogan
Try to comment out the androidTestImplementation and testImplementation dependencies and build your project again.Semiology
G
1

There is a similar problem with simple-json in another question that I answered here. I suggest you do the same for monkeylearn-java and junit or any other non-google dependencies one by one, I mean download their jar files and put them in the libs folder one by one, to find out which one is the problem, and leave it be in libs folder.

I think this is a bug either in Android Studio or in Gradle.

Gott answered 27/7, 2018 at 5:25 Comment(0)
B
1

I had a similar problem with simple-json. For that the solution was to exclude the JUnit dependency.

I am not an expert on Android programming but I find it strange that you exclude hamcrest-core from the JUnit testImplementation. I would rather suggest to exclude transitive dependencies from external libs.

For simple-json this was my solution:

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

Maybe you can do the same for monkeylearn-java?

Beryllium answered 19/12, 2018 at 0:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.