【问题标题】:Android Studio - Program type already present: org.hamcrest.CoreMatchersAndroid Studio - 程序类型已经存在:org.hamcrest.CoreMatchers
【发布时间】:2018-04-15 12:03:01
【问题描述】:

我不知道为什么会出现这个错误:

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

如果 build.gradle (Module: app) 的依赖项在范围内,我的代码是:

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'

}

主活动:

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

你有什么想法吗?

【问题讨论】:

  • 您在尝试运行测试时是否看到此错误正在运行您的应用程序?
  • 我尝试构建项目时出现此错误。那么就无法构建成功。
  • 尝试注释掉 androidTestImplementation 和 testImplementation 依赖并重新构建你的项目。

标签: android-studio hamcrest monkeylearn


【解决方案1】:

在我回答here 的另一个问题中,simple-json 也有类似的问题。 我建议你对monkeylearn-javajunit或者其他任何非google的依赖一个一个的做同样的事情,我的意思是下载他们的jar文件并把它们一个一个放到libs文件夹中,找出哪个是问题,并将其留在libs 文件夹中。

我认为这是 Android Studio 或 Gradle 中的错误。

【讨论】:

    【解决方案2】:

    simple-json 也有类似的问题。为此,解决方案是排除 JUnit 依赖项。

    我不是 Android 编程方面的专家,但我觉得奇怪的是,您从 JUnit testImplementation 中排除了 hamcrest-core。我宁愿建议从外部库中排除传递依赖。

    对于simple-json,这是我的解决方案:

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

    也许您可以为monkeylearn-java 做同样的事情?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-13
      • 1970-01-01
      • 2019-12-11
      • 2018-06-29
      • 2019-01-21
      • 2019-04-22
      • 1970-01-01
      • 2018-09-23
      相关资源
      最近更新 更多