Using Espresso: Hamcrest toplevel and NoClassDefFound - android

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:
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'
}
Copy the hamcrest jars from here
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
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.

Related

Multiple dex files define in Google Play Services

I am having the following dependencies in the build.gradle file. Note that I am using Ionic/Cordova mobile app.
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
// SUB-PROJECT DEPENDENCIES START
debugCompile project(path: "CordovaLib", configuration: "debug")
releaseCompile project(path: "CordovaLib", configuration: "release")
compile "com.google.android.gms:play-services-analytics:+"
compile "com.onesignal:OneSignal:3.3.0#aar"
compile "com.google.android.gms:play-services-gcm:+"
compile "com.google.android.gms:play-services-location:+"
compile "com.google.android.gms:play-services-ads:8.4+"
// SUB-PROJECT DEPENDENCIES END
}
I get the below error during the build process.
Error: cmd: Command failed with exit code 1 Error output:
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Dex: Error converting bytecode to dex:
Cause: com.android.dex.DexException: Multiple dex files define Lcom/google/android/gms/internal/zzah;
UNEXPECTED TOP-LEVEL EXCEPTION:
com.android.dex.DexException: Multiple dex files define Lcom/google/android/gms/internal/zzah;
at com.android.dx.merge.DexMerger.readSortableTypes(DexMerger.java:591)
at com.android.dx.merge.DexMerger.getSortedTypes(DexMerger.java:546)
at com.android.dx.merge.DexMerger.mergeClassDefs(DexMerger.java:528)
at com.android.dx.merge.DexMerger.mergeDexes(DexMerger.java:164)
at com.android.dx.merge.DexMerger.merge(DexMerger.java:188)
at com.android.dx.command.dexer.Main.mergeLibraryDexBuffers(Main.java:504)
at com.android.dx.command.dexer.Main.runMonoDex(Main.java:334)
at com.android.dx.command.dexer.Main.run(Main.java:277)
at com.android.dx.command.dexer.Main.main(Main.java:245)
at com.android.dx.command.Main.main(Main.java:106)
FAILURE: Build failed with an exception.
I read the below SO links. It says about app level and module level gradle files. I am not sure which 2 files are those.
Version conflict updating to 8.4.0
Multiple dex files define Lcom/google/android/gms/internal/zzim;
Multiple dex files define Lcom/google/android/gms/internal/zzau
That error occurs when you have the same library/directory included more than once in your build.gradle's dependencies. This looks also to be conflict with another plugin your using as stated in this thread.
You can check these links:
UNEXPECTED TOP-LEVEL EXCEPTION: com.android.dex.DexException: Multiple dex files define
Check all your dependencies in your build.gradle file. Are you referencing a dependency that also includes a dependency you have already included? For example, if your including appcompat-v7 there is no need to include appcompat-v4 since v7 includes all features from v4.
DexException: Multiple dex files
This error is caused by having too many method referenced in your project. You can read more here. Generally it means that you need to remove unused libraries or use proguard even on debug build to reduce the number of method referenced. You may also go with the multi-dex approached mentioned in the article but it makes your project more complicated.
I am not sure about the reason, but adding Google Admob library plugin the build was successful.
NOTE: I am using cordova plugin for Admob.

Gradle Build FAILED - bad class file magic (cafebabe) or version (0034.0000)

I'm trying to compile a simple Android app composed by the app and a module with multiple dependencies (lambdj, org.json, gson, okhttp ).
But I have this error on compiling and I cannot figure out how to solve it (and where is the problem)
:app:preDexDebug
UNEXPECTED TOP-LEVEL EXCEPTION:
java.lang.RuntimeException: Exception parsing classes
at com.android.dx.command.dexer.Main.processClass(Main.java:752)
at com.android.dx.command.dexer.Main.processFileBytes(Main.java:718)
at com.android.dx.command.dexer.Main.access$1200(Main.java:85)
at com.android.dx.command.dexer.Main$FileBytesConsumer.processFileBytes(Main.java:1645)
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:672)
at com.android.dx.command.dexer.Main.processAllFiles(Main.java:574)
at com.android.dx.command.dexer.Main.runMonoDex(Main.java:311)
at com.android.dx.command.dexer.Main.run(Main.java:277)
at com.android.dx.command.dexer.Main.main(Main.java:245)
at com.android.dx.command.Main.main(Main.java:106)
Caused by: com.android.dx.cf.iface.ParseException: bad class file magic (cafebabe) or version (0034.0000)
at com.android.dx.cf.direct.DirectClassFile.parse0(DirectClassFile.java:472)
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.parseClass(Main.java:764)
at com.android.dx.command.dexer.Main.access$1500(Main.java:85)
at com.android.dx.command.dexer.Main$ClassParserTask.call(Main.java:1684)
at com.android.dx.command.dexer.Main.processClass(Main.java:749)
... 12 more
1 error; aborting
Error:Execution failed for task ':app:preDexDebug'.
> com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command 'C:\Program Files\Java\jdk1.8.0_31\bin\java.exe'' finished with non-zero exit value 1
Information:BUILD FAILED
The stacktrace wasn't very clear for me...after some searches I found this thread (Using lambdaj in android).
The only one answer posted suggested to check the hamcrest library ( a dependency of lambdaj , version 1.1), so I decide to add to my gradle build configuration the latest hemcrest lib available (1.3 version).
compile 'org.hamcrest:hamcrest-all:1.3'
And the project build successfully!
Hope it helps!
UPDATE:
I had some problem with lambdaj on my project, this library is not totally compatible. I moved to retrolambda, check it out!
This is usually caused by using Java 8 libraries on non-Java 8 Projects or vice-versa.
For me the fix was to just use retrolambda on my project and the libraries I used:
in your main build.gradle file:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.5.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
classpath 'me.tatarka:gradle-retrolambda:3.2.4'
}
}
then on top of your app/library build.gradle file:
apply plugin: 'me.tatarka.retrolambda'
Also make sure to set it up for Java 8:
android {
...
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
retrolambda plugin act at compile time and apply Java8 lambda support to your code, see https://github.com/evant/gradle-retrolambda for more informations on what it does.

Gradle fails with 'IllegalArgumentException: already added' on library inclusion

I'm trying to include a library in my new, bare bones Android Studio project.
My module's build.gradle contains the following:
repositories {
mavenCentral()
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.3'
compile 'org.jmrtd:jmrtd:0.5.0-RELEASE'
}
Build-and-run fails at the preDexDebug-step with the following stacktrace:
UNEXPECTED TOP-LEVEL EXCEPTION:
java.lang.IllegalArgumentException: already added: Lnet/sf/scuba/data/Country;
at com.android.dx.dex.file.ClassDefsSection.add(ClassDefsSection.java:122)
at com.android.dx.dex.file.DexFile.add(DexFile.java:161)
at com.android.dx.command.dexer.Main.processClass(Main.java:732)
at com.android.dx.command.dexer.Main.processFileBytes(Main.java:673)
at com.android.dx.command.dexer.Main.access$300(Main.java:83)
at com.android.dx.command.dexer.Main$1.processFileBytes(Main.java:602)
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:632)
at com.android.dx.command.dexer.Main.processAllFiles(Main.java:510)
at com.android.dx.command.dexer.Main.runMonoDex(Main.java:280)
at com.android.dx.command.dexer.Main.run(Main.java:246)
at com.android.dx.command.dexer.Main.main(Main.java:215)
at com.android.dx.command.Main.main(Main.java:106)
1 error; aborting
Now, I've looked for all the obvious problems which could cause this (multiple library inclusion) but can't seem to find the culprit. The library is included only once, and is the only dependency except for the Android support library.
The errorenous inclusion seems to stem from a transitive dependency within JMRTD. Excluding or including this library (scuba) seems to make no difference.
I've also tried to add the library manually in my libs folder but am having the same issue.
Interestingly, if I exclude the transitive dependency completely like so...
configurations {
all*.exclude group: 'net.sf.scuba', module: 'scuba-smartcards'
}
... the preDexDebug step fails on a different file (which is natively part of the library and not included from a dependency):
UNEXPECTED TOP-LEVEL EXCEPTION:
java.lang.IllegalArgumentException: already added: Lorg/jmrtd/BACDeniedException;
at com.android.dx.dex.file.ClassDefsSection.add(ClassDefsSection.java:122)
at com.android.dx.dex.file.DexFile.add(DexFile.java:161)
Side note: the files failing are the first files listed if you view the jar contents in alphabetical order, indicating that all class files would fail in the same way.
Is there some sort of internal duplication going on that I'm not aware of? The artifact I'm including seems fine on inspection but could there be an issue with the format of the files fetched from the repo?
Thanks!
Disclaimer: I'm new to Android and Gradle. I've got extensive experience with Maven and Java though.
For those who might be having similar issues: This turned out to be caused by a bad ant build for both of the libraries in question (jmrtd + scuba) which included each class file twice. This was hard to detect as some file managers will not list duplicate content of archives.
I eventually confirmed it by running 'tar tf ' to discover the diplicates and fixed it by building the libs myself with the ant jar-task attribute 'duplicate="preserve"'.
In general though, the root cause is the terrible default behaviour of ant to add two of the same file instead of failing/overwriting combined with the pickiness of Gradle when dealing with duplicates.

Trouble with building android project with Gradle (Multiple dex files)

Since the release of 1.0, I'm finally moving over to AS.. I don't have too much experience with Gradle, so bear with me. I've found a couple of answers that seem to be putting me on the right track, however, I've yet to find a generic answer that will help me. (Most of the answers are just "replace your jar with this.."). It seems as though I have a dependency linked twice somewhere, however, I'm not quite sure where...
Here is the error I am getting
Output:
UNEXPECTED TOP-LEVEL EXCEPTION:
com.android.dex.DexException: Multiple dex files define Landroid/support/v4/view/PagerAdapter;
at com.android.dx.merge.DexMerger.readSortableTypes(DexMerger.java:594)
at com.android.dx.merge.DexMerger.getSortedTypes(DexMerger.java:552)
at com.android.dx.merge.DexMerger.mergeClassDefs(DexMerger.java:533)
at com.android.dx.merge.DexMerger.mergeDexes(DexMerger.java:170)
at com.android.dx.merge.DexMerger.merge(DexMerger.java:188)
at com.android.dx.command.dexer.Main.mergeLibraryDexBuffers(Main.java:439)
at com.android.dx.command.dexer.Main.runMonoDex(Main.java:287)
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)
And here are my dependencies on my main project:
dependencies {
compile project(':swipeBackLibrary')
compile 'com.android.support:appcompat-v7:19.1.0'
compile files('libs/android-support-v13.jar')
compile files('libs/libGoogleAnalyticsServices.jar')
}
I'm guessing I have a collision somewhere. How can I check where PagerAdapter is getting its source(s)? Thanks guys.
The appcompat library includes the support library, which is where the duplication comes from. Since you've included the support library as a jarfile instead of referring to it via Maven coordinates, the build system can't disambiguate the multiple copies of the library and prevent the error. You can fix it by moving to a coordinate-based spec for the support library:
dependencies {
compile project(':swipeBackLibrary')
compile 'com.android.support:appcompat-v7:19.1.0'
compile 'com.android.support:support-v13:19.1.0'
compile files('libs/libGoogleAnalyticsServices.jar')
}

UNEXPECTED TOP-LEVEL EXCEPTION when testing using Gradle

I'm pretty new to Gradle. My project is compiling file when I do a
./gradlew assembleDebug
But it doesn't when I try to run the tests:
./gradlew connectedCheck
I get (a couple of them):
UNEXPECTED TOP-LEVEL EXCEPTION:
java.lang.IllegalArgumentException: already added: Lcom/facebook/AccessToken$SerializationProxyV1;
at com.android.dx.dex.file.ClassDefsSection.add(ClassDefsSection.java:123)
at com.android.dx.dex.file.DexFile.add(DexFile.java:163)
at com.android.dx.command.dexer.Main.processClass(Main.java:490)
at com.android.dx.command.dexer.Main.processFileBytes(Main.java:459)
at com.android.dx.command.dexer.Main.access$400(Main.java:67)
at com.android.dx.command.dexer.Main$1.processFileBytes(Main.java:398)
at com.android.dx.cf.direct.ClassPathOpener.processArchive(ClassPathOpener.java:245)
at com.android.dx.cf.direct.ClassPathOpener.processOne(ClassPathOpener.java:131)
at com.android.dx.cf.direct.ClassPathOpener.process(ClassPathOpener.java:109)
at com.android.dx.command.dexer.Main.processOne(Main.java:422)
at com.android.dx.command.dexer.Main.processAllFiles(Main.java:333)
at com.android.dx.command.dexer.Main.run(Main.java:209)
at com.android.dx.command.dexer.Main.main(Main.java:174)
at com.android.dx.command.Main.main(Main.java:91)
here is the structure of my project:
main app depends on:
compile 'com.android.support:support-v4:18.0.+'
compile 'com.android.support:gridlayout-v7:18.0.+'
compile 'com.google.code.gson:gson:1.7.2'
compile 'com.jakewharton:butterknife:2.0.1'
compile 'com.squareup.retrofit:retrofit:1.1.1'
compile 'com.squareup:otto:1.3.4'
compile 'com.squareup.okhttp:okhttp:1.2.0'
compile project(':Library:mylib')
mylib depends on:
compile 'com.android.support:support-v4:18.0.+'
compile files('libs/facebooksdk.jar')
compile files('libs/libGoogleAnalyticsV2.jar')
the exception is coming for each jars from mylib, after this line:
:Library:mylib:dexTest
any idea?
The already added error occurs when a library is referenced more than once. What I'd do is set the compile project as the first compile element in your app and delete the repeated support-library:
app depends on:
compile project(':Library:mylib')
compile 'com.google.code.gson:gson:1.7.2'
compile 'com.jakewharton:butterknife:2.0.1'
compile 'com.squareup.retrofit:retrofit:1.1.1'
compile 'com.squareup:otto:1.3.4'
compile 'com.squareup.okhttp:okhttp:1.2.0'
lib depends on:
compile 'com.android.support:support-v4:18.0.+'
compile files('libs/facebooksdk.jar')
compile files('libs/libGoogleAnalyticsV2.jar')
I encountered the same error but the message behind already added: is slightly different. It's about duplicate support-v4. What I did to tackle is:
compile('de.keyboardsurfer.android.widget:crouton:1.8.1') {
exclude(group: 'com.google.android', module: 'support-v4')
}
Hope it show you a new direction to fix your issue.
UPDATE:
Sorry, I didn't read your question carefully. Turns out you got problem with the connectedCheck task. Mine is when running gradle build. Right now, when I run gradle connectedCheck, I just faced the same error. I even tried with the IDE but none of them worked.
There are actually a discussion about it here https://code.google.com/p/android/issues/detail?id=61429 too. Due to the comment #8, if you move to the app folder and run above task, you may manage. Assume that the app folder is app-project/app, the libs are put in app-projecct/libraries/my-lib-1. Previously, I run in app-project and failed.
Hope it fix.

Categories

Resources