سؤال

I am trying to add Espresso to test our app.

When I do not have the hamcrest-1.1.jar in the build.gradle, I receive the following error:

com.espresso.PracticeTest > testClickOnActionBar[Nexus 10 - 4.3] FAILED 
java.lang.NoClassDefFoundError: org.hamcrest.Matchers
at com.google.android.apps.common.testing.ui.espresso.matcher.ViewMatchers.withId(ViewMatchers.java:274)
:Example:connectedInstrumentTest FAILED

When I do have the hamcrest-1.1.jar in the build.gradle, I receive the following error:

 UNEXPECTED TOP-LEVEL EXCEPTION:
com.android.dx.cf.iface.ParseException: class name (org/hamcrest/beans/HasProperty) does not match path (hamcrest-library-1.1/org/hamcrest/beans/HasProperty.class)
at com.android.dx.cf.direct.DirectClassFile.parse0(DirectClassFile.java:520)
at com.android.dx.cf.direct.DirectClassFile.parse(DirectClassFile.java:406)
at com.android.dx.cf.direct.DirectClassFile.parseToInterfacesIfNecessary(DirectClassFile.java:388)
at com.android.dx.cf.direct.DirectClassFile.getMagic(DirectClassFile.java:251)
at com.android.dx.command.dexer.Main.processClass(Main.java:665)
at com.android.dx.command.dexer.Main.processFileBytes(Main.java:634)
at com.android.dx.command.dexer.Main.access$600(Main.java:78)
at com.android.dx.command.dexer.Main$1.processFileBytes(Main.java:572)
at com.android.dx.cf.direct.ClassPathOpener.processArchive(ClassPathOpener.java:284)
at com.android.dx.cf.direct.ClassPathOpener.processOne(ClassPathOpener.java:166)
at com.android.dx.cf.direct.ClassPathOpener.process(ClassPathOpener.java:144)
at com.android.dx.command.dexer.Main.processOne(Main.java:596)
at com.android.dx.command.dexer.Main.processAllFiles(Main.java:498)
at com.android.dx.command.dexer.Main.runMonoDex(Main.java:264)
at com.android.dx.command.dexer.Main.run(Main.java:230)
at com.android.dx.command.dexer.Main.main(Main.java:199)
at com.android.dx.command.Main.main(Main.java:103)
...while parsing hamcrest-library-1.1/org/hamcrest/beans/HasProperty.class

This is the build.gradle

dependencies {
instrumentTestCompile files('libs/dagger-1.1.0.jar', 'libs/dagger-compiler-1.1.0.jar',
        'libs/hamcrest-library-1.1.jar',
        'libs/espresso-1.0-SNAPSHOT.jar', 'libs/guava-14.0.1.jar', 'libs/javawriter-2.1.1.jar',
        'libs/javax.annotation-api-1.2.jar','libs/javax.inject-1.jar',
        'libs/jsr305-1.3.9.jar', 'libs/testrunner-1.0-SNAPSHOT.jar',
        'libs/testrunner-runtime-1.0-SNAPSHOT.jar')
 }

I do a ./gradlew clean build then ./gradlew example:connectedCheck to run.

Does anyone have a work-around to this?

هل كانت مفيدة؟

المحلول

The answer here is likely to serve you better than my original one

Took quite awhile but I finally got it working. I had to do the following:

  1. Declare my dependencies like so:

    dependencies {
      compile 'com.android.support:appcompat-v7:+'
      compile 'com.squareup.dagger:dagger-compiler:1.1.0'
      compile 'com.squareup.dagger:dagger:1.1.0'
    
      instrumentTestCompile files('libs/espresso-1.0-SNAPSHOT.jar','libs/testrunner-1.0-SNAPSHOT.jar','libs/testrunner-runtime-1.0-SNAPSHOT.jar')
      instrumentTestCompile files('libs/hamcrest-core-1.1.jar', 'libs/hamcrest-library-1.1.jar', 'libs/hamcrest-integration-1.1.jar')
      instrumentTestCompile 'com.google.guava:guava:14.0.1'
    }
    
  2. Copy the hamcrest jars from here

  3. Remove the license files from the jars like this (or else you'll get an error about duplicate LICENSE.txt files)

    zip -d hamcrest-core-1.1.jar LICENSE.txt
    zip -d hamcrest-library-1.1.jar LICENSE.txt
    
  4. Run gradle connectedCheck

A few things to note:
- Hamcrest 1.3 didn't work for me, got an error about a matcher was missing - Crazy how many hoops I had to jump through to get here.
- Good luck getting this to play well with android studio.

نصائح أخرى

I haven't used gradle yet but I had similar issues which only went away when I added all of the hamcrest jars to my classpath.

Adding hamcrest-all.jar didn't work for me only the individuals (lib, core and integration).

That said, it did complain about the license.txt inside of the jars.

This was not a problem I had to time to figure out what the "proper" fix was but removing the license files from the jars and it all started working.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top