ahsing / android-test-kit

Automatically exported from code.google.com/p/android-test-kit
0 stars 0 forks source link

NoClassDefFoundError in project which uses Dagger #111

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?

Project uses these dependencies for tests:

Instructions used for build configuration: 
https://code.google.com/p/android-test-kit/wiki/EspressoSetupInstructions

dependencies {
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.0') {
        exclude group: 'com.squareup.dagger'
    }
    androidTestCompile 'com.android.support.test:testing-support-lib:0.1'
}

Also I tried without excluding 'com.squareup.dagger'

After I run tests i get `java.lang.NoClassDefFoundError` and it seems like 
Dagger didn't generate needed classes:

 java.lang.NoClassDefFoundError: com/*/*/a/*AppModule$$ModuleAdapter$ProvideApplicationProvidesAdapter
            at com.*.*.a.*AppModule$$ModuleAdapter.getBindings(*AppModule$$ModuleAdapter.java:28)
            at com.*.*.a.*AppModule$$ModuleAdapter.getBindings(*AppModule$$ModuleAdapter.java:13)
            at dagger.ObjectGraph$DaggerObjectGraph.makeGraph(ObjectGraph.java:185)
            at dagger.ObjectGraph$DaggerObjectGraph.access$000(ObjectGraph.java:138)
            at dagger.ObjectGraph.create(ObjectGraph.java:129)
            at com.*.*.a.*Application.buildObjectGraphAndInject(*Application.java:30)
            at com.*.*.a.*Application.onCreate(*Application.java:26)

What is the expected output? What do you see instead?
Tests run corretly.

What version of the product are you using? On what operating system?

'com.android.support.test.espresso:espresso-core:2.0'
'com.android.support.test:testing-support-lib:0.1'

Please provide any additional information below.

Tests run corretly if I use these dependencies instead:

    androidTestCompile 'com.android.support.test:testing-support-lib:0.1'
    androidTestCompile 'org.hamcrest:hamcrest-integration:1.1'
    androidTestCompile 'org.hamcrest:hamcrest-core:1.1'
    androidTestCompile 'org.hamcrest:hamcrest-library:1.1'
    androidTestCompile 'com.google.guava:guava:18.0'
    androidTestCompile files('testLibs/espresso-core-2.0.jar')
    androidTestCompile files('testLibs/espresso-contrib-2.0.jar')

Original issue reported on code.google.com by froger.mcs on 22 Dec 2014 at 9:33

GoogleCodeExporter commented 9 years ago
Exclude group: 'com.squareup.dagger'" should not be there (the dagger 
dependency is shaded, so it should not conflict with your app).

NoClassDefFoundError is also common with proguard. If you're proguarding, 
please make sure that the required classes are not being stripped out.

If you could repro this with a simple project (e.g. add dagger to 
https://github.com/googlesamples/android-testing/blob/master/espresso/BasicSampl
e/app/build.gradle), that would help us with the investigation.

Original comment by vale...@google.com on 22 Dec 2014 at 9:18

GoogleCodeExporter commented 9 years ago
I'm also running into this error and trying to minimally reproduce it. In my 
bare-bones project, I discovered that updating the support dependency from

    compile 'com.android.support:support-v4:20.0.0'

to 

    compile 'com.android.support:support-v4:21.0.3'

seems to fix it. However, that solution is inadequate for my real project. I'm 
going to see if I figure anything else out.

Original comment by bria...@gmail.com on 23 Dec 2014 at 1:32

GoogleCodeExporter commented 9 years ago
This project is currently producing this error for me.

Original comment by bria...@gmail.com on 23 Dec 2014 at 1:51

Attachments:

GoogleCodeExporter commented 9 years ago
Can you verify the basic setup works with building one of the samples on 
https://github.com/googlesamples/android-testing/ and then try to reproduce the 
it with one of those? Would make it easier for us.

Thanks. 

Original comment by slinz...@google.com on 23 Dec 2014 at 10:06

GoogleCodeExporter commented 9 years ago
I just had a quick look at your build.gradle file and is there any particular 
reason you are using different versions of v4 and v7? v7 depends on v4 and that 
could be an issue in itself if you use 20.0.0 and 21.0.3 You should really 
avoid using two different versions if possible and update them accordingly. Can 
you try if that fixes the problem?

Original comment by slinz...@google.com on 23 Dec 2014 at 10:13

GoogleCodeExporter commented 9 years ago
There's no particular reason for the different versions aside from admitted 
cargo-cult gradle programming on my part. I'll try to fix that in my project.

Here's a patch against BasicSample that reproduces the issue for me.
https://github.com/brianhv/android-testing/commit/84afc8f490869f6dabc59f8aaa223f
b71b2efb44

Original comment by bria...@gmail.com on 23 Dec 2014 at 5:46

GoogleCodeExporter commented 9 years ago
Having exactly same issues: tests dont run on pre-L devices, but compile and 
run fine on 5.0.

Original comment by magedark...@gmail.com on 25 Dec 2014 at 12:38

GoogleCodeExporter commented 9 years ago
Also able to reproduce this. Emulator and physical devices work on 5.0+. Fails 
for the stated reason on my 4.0 tests.

Original comment by w...@therusty.org on 27 Dec 2014 at 11:05

GoogleCodeExporter commented 9 years ago
Probably I've found bug reason. In my project I had the same problem when I 
reached 64k methods limit. After I add `multiDexEnabled = true` property, but 
forgot to extend `MultiDexApplication` I had the same issue. 

In Android 5.0 it works without any problems because Lollipop natively supports 
loading multiple dex files. Probably it's something similar in project tests.

Also I'm not sure about testInstrumentationRunner. Should we use 
MultiDexTestRunner or AndroidJUnitRunner? I'll try the first one as soon as I 
have some free time.

Original comment by froger.mcs on 29 Dec 2014 at 6:56

GoogleCodeExporter commented 9 years ago
My issue was the javax.inject dependency being included by espresso and dagger.

exclude group: 'javax.inject'

Original comment by w...@therusty.org on 31 Dec 2014 at 6:41

GoogleCodeExporter commented 9 years ago
w...@therusty.org
thank you, now it is working!

Original comment by ultrao...@gmail.com on 31 Dec 2014 at 2:02

GoogleCodeExporter commented 9 years ago
Excluding javax.inject fixes it for me as well.

Original comment by bria...@gmail.com on 1 Jan 2015 at 5:32

GoogleCodeExporter commented 9 years ago
Confirmed, excluding javax.inject fixes the problem. Thanks "#10 
w...@therusty.org"

Original comment by magedark...@gmail.com on 3 Jan 2015 at 12:23

GoogleCodeExporter commented 9 years ago
For people who encounter similar issues in the future, if you are running via 
emulator you get some helpful messages in the logs for diagnosing. E.g.:

(Ldagger/internal/Binding; had used a different Ljavax/inject/Provider; during 
pre-verification) 

or

DexOpt: not resolving ambiguous class 'Ljunit/framework/TestSuite;'

Then it is just a matter of finding out what dependencies are including these 
classes.

Original comment by w...@therusty.org on 3 Jan 2015 at 5:59