Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

java.lang.RuntimeException: java.lang.ClassNotFoundException: android.app.Application #4

Closed
vanniktech opened this issue Feb 25, 2015 · 18 comments

Comments

@vanniktech
Copy link

When I try to run your library example and want to execute the RobolectricCoreTest I get this exception:

java.lang.RuntimeException: java.lang.ClassNotFoundException: android.app.Application
at org.robolectric.annotation.Config$Implementation.parseApplication(Config.java:123)
at org.robolectric.annotation.Config$Implementation.fromProperties(Config.java:92)
at com.example.core.CustomRobolectricRunner.overwriteConfig(CustomRobolectricRunner.java:37)
at com.example.core.CustomRobolectricRunner.getAppManifest(CustomRobolectricRunner.java:29)
at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:182)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:158)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:74)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:211)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:67)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
Caused by: java.lang.ClassNotFoundException: android.app.Application
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at org.robolectric.annotation.Config$Implementation.parseApplication(Config.java:120)
... 23 more

Is there any way that I can fix that one? Basically every Unit Test class works except for RobolectricCoreTest

Adding android.sourceSets.test.java.srcDirs += "build/generated/source/r/debug" worked for the R. issue.

@nenick
Copy link
Owner

nenick commented Feb 25, 2015

I'm definitely had a working example but now i see the same issue. I will check what i can do.

Also saw a different error

java.lang.ClassCastException: java.lang.NoClassDefFoundError cannot be cast to java.lang.RuntimeException
    at org.robolectric.internal.ReflectionHelpers.callInstanceMethodReflectively(ReflectionHelpers.java:68)
    at org.robolectric.util.ActivityController$1.run(ActivityController.java:115)
    at org.robolectric.shadows.ShadowLooper.runPaused(ShadowLooper.java:268)
    at org.robolectric.util.ActivityController.create(ActivityController.java:111)
    at org.robolectric.util.ActivityController.create(ActivityController.java:122)
    at org.robolectric.util.ActivityController.setup(ActivityController.java:202)
    at org.robolectric.Robolectric.setupActivity(Robolectric.java:1388)
    at com.example.core.RobolectricCoreTest.testIt(RobolectricCoreTest.java:16)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:236)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:158)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:74)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:211)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:67)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)

@vanniktech
Copy link
Author

Alright thank you very much. I've also tried it at home and I've got the same error on Mac as on Linux.

@nenick
Copy link
Owner

nenick commented Feb 26, 2015

When you have the time then try to get a working library example with the plugin from robolectric team. I have no idea when i will find the time

@nenick
Copy link
Owner

nenick commented Feb 26, 2015

Our issue could be related to https://code.google.com/p/android/issues/detail?id=156027

@nenick
Copy link
Owner

nenick commented Feb 26, 2015

./gradlew core:testDebug does not compile the main classes so we must call self ./gradlew core:assembleDebug. This does not fix our issue...

I tried a simple line at different position and I'm confused about the result. int action_bar = android.support.v7.appcompat.R.id.action_bar;
This line at the MainActivity in the app module does work like expected. But using this line in the core module will not compile anymore.

@nenick
Copy link
Owner

nenick commented Feb 26, 2015

Missing R looks like a different issue which can be resolved. http://stackoverflow.com/questions/24842927/appcompat-v7-package-does-not-exist

But when i get the error java.lang.NoClassDefFoundError cannot be cast to java.lang.RuntimeException The root cause is java.lang.NoClassDefFoundError: android/support/v7/appcompat/R$styleable

@nenick
Copy link
Owner

nenick commented Feb 26, 2015

Replace the ActionBarActivity with a simple FragmentActivity and it works for me. But this is not acceptable when we like to use the appcompat library.

public class CoreActivity extends FragmentActivity {

Perhaps this could be done with robolectric 3.0 because there is much more appcompat support.

@marcoRS
Copy link

marcoRS commented Feb 28, 2015

Robolectric 3.0-SNAPSHOT seems to be compatible with Activities extending ActionBarActivity. I had a simple example working in AS although it would fail when running in command line

@vanniktech
Copy link
Author

@marcoRS
And why did it fail if you've tried to run it via the command line?

@nenick
Copy link
Owner

nenick commented Mar 1, 2015

The issue with ActionBarActivity can definitively avoided with robolectric 3.0-SNAPSHOT

With robolectric 2.4 this works only for application modules or if you switching back to simple FragmentActivity and avoid appcompat. Or someone find a proper solution.

Some additional discussion about this topic http://stackoverflow.com/questions/28767817/android-studio-1-1-0-robolectric-2-4-causes-java-lang-annotation-annotationfor/28825095?noredirect=1#comment45936404_28825095

@nenick
Copy link
Owner

nenick commented Mar 14, 2015

@vanniktech I have updated the library example. Would be nice if you could try if it works for you too.

For Android Studio you must run first an unit test from the main module and then you can run tests from library module or you will get the issue #9

@vanniktech
Copy link
Author

When I run clean check --continue from the Terminal everything seems to be working except that in RobolectricCoreTest.testIt I get this exception:

java.lang.RuntimeException: java.io.FileNotFoundException: ~/tmp/AndroidStudioAndRobolectric/core/build/intermediates/bundles/debug/bundles/debug/AndroidManifest.xml (No such file or directory)

However that's not a real issue for me since I would test Android Activity related stuff only in the app module and there the test works perfectly fine.

What's weird though when I try to run the tests in Android Studio, I get this error in core/build.gradle:

Error:(45, 0) Gradle: Execution failed for task ':core:assembleDebug'.
core/build/intermediates/exploded-aar/com.android.support/appcompat-v7/21.0.3/R.txt (No such file or directory)

I've double checked and the file is present: core/build/intermediates/exploded-aar/com.android.support/appcompat-v7/21.0.3/R.txt.

@nenick
Copy link
Owner

nenick commented Mar 18, 2015

Thank you for reporting. I will take a look later.

First issue should be easy to fix because the path looks wrong ...bundles/debug/bundles/debug/...

Second issue could be relay on different execution root. Could you get the absolut path?

@vanniktech
Copy link
Author

I was able to fix the second error:

def rTextFile = new File("core/build/intermediates/exploded-aar/com.android.support/appcompat-v7/21.0.3/R.txt")

needs to be replaced with:

def rTextFile = new File("$projectDir/build/intermediates/exploded-aar/com.android.support/appcompat-v7/21.0.3/R.txt")

Afterwards you can execute the tests in Android Studio and from the Terminal. If you want I can create a PR (in the library branch) for that.

@nenick
Copy link
Owner

nenick commented Mar 18, 2015

I would be thankful for each help

@vanniktech
Copy link
Author

There you go #10

@nenick
Copy link
Owner

nenick commented Mar 18, 2015

Thank you for your PR.

Both issues results from different paths on command line and Android Studio. Please try again ;)

@vanniktech
Copy link
Author

Great changes! Now every test works and an AssertionError is thrown. Seems like we're finally able to test in Android Studio.

Seems like this one can be closed.

@nenick nenick closed this as completed Mar 21, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants