gradle-plugin-robospock icon indicating copy to clipboard operation
gradle-plugin-robospock copied to clipboard

java.lang.NoClassDefFoundError when instantiating classes from android project

Open AndrewReitz opened this issue 11 years ago • 15 comments

When running the tests I am getting java.lang.NoClassDefFoundError when trying to instantiate a class in the main Android Project.

I have my project setup in the exact same fashion as the sample project. My only though on this is that since I can't seem access library files inside of the specifications, is that libraries aren't being loaded into the second project, and the class I am trying to use throws this exception when the dependency can not be loaded.

Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.

AndrewReitz avatar Dec 30 '14 22:12 AndrewReitz

Can you print out the full stack-trace and perhaps the file & repository where the error comes from?

Centril avatar Dec 30 '14 22:12 Centril

Here is that stacktrace

com.andrewreitz.tiles.StateTileStateManagerSpec > get default state FAILED java.lang.NoClassDefFoundError at StateTileStateManagerSpec.groovy:8 Caused by: java.lang.ClassNotFoundException at StateTileStateManagerSpec.groovy:8

The line it's failing on looks like def a = new StateTileStateManager() It is package protected but when I change that to public it doesn't seem to change anything. The constructor has @javax.inject.Inject on it for use with dagger. In the specificiation I can't access @Inject which is what is leading me to believe that the dependencies are not being loaded into this project correctly.

I can however create other simple classes that do not require any library dependencies.

Unfortunately I can't link the repo. I will try to setup a test one in a bit.

AndrewReitz avatar Dec 30 '14 22:12 AndrewReitz

Seems like this is actually being caused by rxandroid

Here is the application I created that causes the error. https://github.com/pieces029/robospock-test

AndrewReitz avatar Dec 30 '14 23:12 AndrewReitz

Ok, never used that framework before, so I don't know how much I can help.

Centril avatar Dec 31 '14 00:12 Centril

I'm guessing it's not the specific library and probably something it does. I will keep playing with it and report back the findings.

AndrewReitz avatar Dec 31 '14 00:12 AndrewReitz

I think it could be due to the provided dependency to android in the project.

I tried a few other libraries that do the same thing, and get the same results.

https://github.com/ReactiveX/RxAndroid/blob/0.x/build.gradle for reference.

AndrewReitz avatar Dec 31 '14 04:12 AndrewReitz

Ah, that explains it... Only compile dependencies are added currently... I'm thinking that since test projects usually are not exposed as artifacts, maybe I can just add the provided dependencies as compile to the tester project.

What do you think?

Centril avatar Dec 31 '14 05:12 Centril

The provided dependency is android, so I'm not sure how that should be pulled in. I'm guessing if it's switched to compile it would cause more issues.

I'll keep playing with it and see what else I can find.

AndrewReitz avatar Dec 31 '14 06:12 AndrewReitz

I know =)

Checkout: https://github.com/Centril/gradle-plugin-robospock/blob/master/plugin/src/main/groovy/se/centril/robospock/RoboSpockConfigurator.groovy

As you can see from the code, the project dependencies of the android project is turned into a .jar and then added to the tester project as a testCompile dependency...

Centril avatar Dec 31 '14 06:12 Centril

Does that grab all the dependencies of other dependencies? I can seem to find the output jar to inspect the class files.

AndrewReitz avatar Dec 31 '14 06:12 AndrewReitz

It grabs transitive project dependencies - but only project dependencies.

Centril avatar Dec 31 '14 06:12 Centril

So if a library has a dependency it never gets those. I'm guessing that there in lies the problem.

Are you applying the groovy plugin somewhere? If so is there a way to just copy over all the dependencies from the app project into the dependencies section of the test app? I'll pull down the code and actually start learning what's going on sometime today.

AndrewReitz avatar Dec 31 '14 16:12 AndrewReitz

Ok, I found the issue. Only aars are being pulled in, so any time you reference library code you will get this exception. I am attempting to fix this now.

AndrewReitz avatar Dec 31 '14 23:12 AndrewReitz

Just updated to include my work around in the project. https://github.com/pieces029/robospock-test

If you remove the groovy plugin and the dependency section you will see the exception again. Still trying to figure out a way to have this be fixed in the plugin.

For whatever reason both the groovy and java plugins don't pick up any of the dependencies if you do compile project(':app')

AndrewReitz avatar Jan 01 '15 00:01 AndrewReitz

Just realized Timber also has transitive dependencies so that wasn't really a good test in that sample app.

AndrewReitz avatar Jan 01 '15 18:01 AndrewReitz