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

NoClassDefFoundError: com/android/internal/os/BackgroundThread #2160

Closed
ltn614 opened this issue Dec 1, 2015 · 26 comments
Closed

NoClassDefFoundError: com/android/internal/os/BackgroundThread #2160

ltn614 opened this issue Dec 1, 2015 · 26 comments

Comments

@ltn614
Copy link

ltn614 commented Dec 1, 2015

I download few sample project, none of them is working. When I run test in grade or AndroidStudio,print the following error message:estSomething FAILED
java.lang.NoClassDefFoundError: com/android/internal/os/BackgroundThread
Caused by:
java.lang.ClassNotFoundException: com.android.internal.os.BackgroundThread

@jaredsburrows
Copy link
Contributor

Post your code. What does this mean if we can't reproduce it?

@ltn614
Copy link
Author

ltn614 commented Dec 2, 2015

I just download the sample project form :
https://github.com/robolectric/deckard
BTW, I am use Android studio 1.5 and gradle 2.4 on Mac OS X 10.10.5

@jaredsburrows
Copy link
Contributor

Make sure you use the gradlew that is provided in the project. The project builds fine on the CI server. This is an issue with you. The latest Gradle version is 2.9, the project uses 2.6. You are using 2.4, why?

The Gradle version should be updated along with the Intellij/Android Studio version: http://tools.android.com/tech-docs/new-build-system/version-compatibility.

The easiest way is to keep everything updated. Besides this issue should have gone in deckard. @erd Can we close issues like this please?

@ltn614
Copy link
Author

ltn614 commented Dec 3, 2015

I use the ./gradlew clean test --info, produce the same error information.

@ltn614
Copy link
Author

ltn614 commented Dec 3, 2015

And I upgrade my gradle to 2.9, still got this same problem

@jaredsburrows
Copy link
Contributor

Here, I updated their plugins and I use gradle 2.9:

buildscript {
    repositories {
        jcenter()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:1.5.0'
    }
}

repositories {
    jcenter()
}

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion '23.0.2'

    defaultConfig {
        minSdkVersion 18
        targetSdkVersion 23
        versionCode 2
        versionName "1.0.0-SNAPSHOT"
        applicationId "com.example"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }

    packagingOptions {
        exclude 'LICENSE.txt'
    }
}

dependencies {
    // Support v4
    compile 'com.android.support:support-v4:23.1.1'

    // Espresso
    androidTestCompile 'com.android.support:support-annotations:23.1.1'
    androidTestCompile 'com.android.support.test.espresso:espresso-contrib:2.2.1'

    // Robolectric
    testCompile 'junit:junit:4.12'
    testCompile 'org.hamcrest:hamcrest-library:1.3'
    testCompile 'org.apache.maven:maven-ant-tasks:2.1.3' // fixes issue on linux/mac
    testCompile 'org.robolectric:robolectric:3.0'
}

@ltn614
Copy link
Author

ltn614 commented Dec 4, 2015

Thanks for you to solve my problems patiently,but after I use this build.gradle file, I still failed.
I'm hopeless, but I just paste my error message generated I use griddlew cmd with --info:

Executing task ':testDebugUnitTest' (up-to-date check took 0.008 secs) due to:
No history is available.
Starting process 'Gradle Test Executor 1'. Working directory: /Users/user/work_code/deckard Command: /Library/Java/JavaVirtualMachines/jdk1.8.0.jdk/Contents/Home/bin/java -Djava.awt.headless=true -Djava.security.manager=jarjar.org.gradle.process.internal.child.BootstrapSecurityManager -Dfile.encoding=UTF-8 -Duser.country=CN -Duser.language=zh -Duser.variant -ea -cp /Users/user/.gradle/caches/2.6/workerMain/gradle-worker.jar jarjar.org.gradle.process.internal.launcher.GradleWorkerMain 'Gradle Test Executor 1'
Successfully started process 'Gradle Test Executor 1'
Gradle Test Executor 1 started executing tests.
Gradle Test Executor 1 finished executing tests.

com.example.activity.DeckardActivityTest > testSomething FAILED
java.lang.NoClassDefFoundError: com/android/internal/os/BackgroundThread

    Caused by:
    java.lang.ClassNotFoundException: com.android.internal.os.BackgroundThread

1 test completed, 1 failed
Finished generating test XML results (0.042 secs) into: /Users/user/work_code/deckard/build/test-results/debug
Generating HTML test report...
Finished generating test html results (0.146 secs) into: /Users/user/work_code/deckard/build/reports/tests/debug
:testDebugUnitTest FAILED
:testDebugUnitTest (Thread[Task worker Thread 3,5,main]) completed. Took 3.893 secs.

@jaredsburrows
Copy link
Contributor

@ltn614 It shows you are using gradle 2.6. Make sure you are using the latest JDK 8 version.

@jaredsburrows
Copy link
Contributor

What is the update on this?

@ltn614
Copy link
Author

ltn614 commented Dec 17, 2015

still no any progress, I try this on computer at home, still same problem, it's a windows 10 desktop, I am sure this is not working for me.

@jaredsburrows
Copy link
Contributor

@ltn614 I am thinking it still has to do with your setup. I cannot reproduce your problem.

@erd
Copy link
Member

erd commented Jan 22, 2016

Errors like these are usually caused by having mis-matched Robolectric jars on your system. Try running gradle --refresh-dependencies. If that doesn't work, try looking at robolectric/deckard or robolectric/robolectric-samples for examples of working configurations. You are also welcome to post on the mailing list (robolectric@googlegroups.com) to see if anyone else might be able to help.

@erd erd closed this as completed Jan 22, 2016
@cesar1000
Copy link
Contributor

I'm repro'ing this issue consistently. I'm using SDK 21 for most of my tests. Tests that are configured to run on SDK 18 fail with this exception, since the reset of the shadows can't find BackgroundThread. It looks like the shadow reset is not accounting for old SDKs. Here's the full exception:

java.lang.NoClassDefFoundError: com/android/internal/os/BackgroundThread
    at org.robolectric.shadows.ShadowBackgroundThread.reset(ShadowBackgroundThread.java:19)
    at org.robolectric.Shadows.reset(Shadows.java:1607)
    at org.robolectric.Robolectric.reset(Robolectric.java:22)
    at org.robolectric.internal.ParallelUniverse.resetStaticState(ParallelUniverse.java:43)
    at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:233)
    at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:188)
    at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:54)
    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:152)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.runTestClass(JUnitTestClassExecuter.java:105)
    at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.execute(JUnitTestClassExecuter.java:56)
    at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassProcessor.processTestClass(JUnitTestClassProcessor.java:64)
    at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.processTestClass(SuiteTestClassProcessor.java:50)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
    at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
    at org.gradle.messaging.dispatch.ContextClassLoaderDispatch.dispatch(ContextClassLoaderDispatch.java:32)
    at org.gradle.messaging.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:93)
    at com.sun.proxy.$Proxy2.processTestClass(Unknown Source)
    at org.gradle.api.internal.tasks.testing.worker.TestWorker.processTestClass(TestWorker.java:106)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
    at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
    at org.gradle.messaging.remote.internal.hub.MessageHub$Handler.run(MessageHub.java:360)
    at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:54)
    at org.gradle.internal.concurrent.StoppableExecutorImpl$1.run(StoppableExecutorImpl.java:40)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.ClassNotFoundException: com.android.internal.os.BackgroundThread
    at org.robolectric.internal.bytecode.InstrumentingClassLoader.getByteCode(InstrumentingClassLoader.java:168)
    at org.robolectric.internal.bytecode.InstrumentingClassLoader.findClass(InstrumentingClassLoader.java:123)
    at org.robolectric.internal.bytecode.InstrumentingClassLoader.loadClass(InstrumentingClassLoader.java:95)
    ... 40 more

@guysmoilov
Copy link

I can report getting the exact same error as @cesar1000, consistently.

@leonardok
Copy link

I am also getting this using 3.0. I get this even running the deckard sample project tests. When I try to import com.android.internal.os.BackgroundThread it does not resolve.

@cesar1000
Copy link
Contributor

I just switched all my tests to SDK 21 and the issue is gone. It's definitely a bug in Robolectric though.

@leonardok
Copy link

It doesn't work for me with sdk 21. The test project is using sdk 21
actually. :-(


Leo.

On Sat, Mar 19, 2016 at 3:42 PM, César Puerta notifications@github.com
wrote:

I just switched all my tests to SDK 21 and the issue is gone. It's
definitely a bug in Robolectric though.


You are receiving this because you commented.
Reply to this email directly or view it on GitHub
#2160 (comment)

@drawers
Copy link

drawers commented Apr 19, 2016

Was consistently repro'ing the exact same error with stacktrace. After problems with a difficult proxy, I suspected some .jars were missing or corrupt. Connected through a proxy-less network, went to .m2 directory and deleted 'repository' directory, causing the dependencies to re-download at runtime. It now works as expected.

@alwaystest
Copy link

@drawers yes,I didn't download the jars correctly. After remove ~/.m2/repository and redownload jars, my problem solved. Still don't know how does Robolectric works, and why the jars were downloaded to ~/.m2/repository.
I am new to this. Thanks for your comment.

@drawers
Copy link

drawers commented May 1, 2016

@alwaystest glad it helps. @erd gradlew --refresh-dependencies may not be enough to remove the mismatched jars. you may want to add to the previous note for closing the issue that deleting the .gradle and .m2 directories to cause the cached dependencies to re-download is a possible fix.

@VadivelChinnasamy
Copy link

VadivelChinnasamy commented Jun 28, 2016

My probs is I have implement my code and add library also.it working fine latest model but not support android 4.1,my min sdk file is 15.It maces class not found error
My gradle file
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.google.android.gms:play-services:8.4.0'
compile 'com.android.support:appcompat-v7:23.4.0'
compile 'com.android.support:design:23.2.1'
compile 'com.jakewharton:butterknife:6.1.0'
compile 'com.mcxiaoke.volley:library:1.0.17'
compile 'com.facebook.android:facebook-android-sdk:4.+'
compile 'com.edmodo:rangebar:1.0.0'
compile 'com.wdullaer:materialdatetimepicker:2.3.0'
compile 'com.squareup.picasso:picasso:2.5.2'
compile 'com.loopj.android:android-async-http:1.4.9'

@jhansche
Copy link
Contributor

The root of this problem is that robolectric manually downloads Maven dependencies, instead of letting Gradle handle it: https://github.com/robolectric/robolectric/blob/master/robolectric/src/main/java/org/robolectric/internal/dependency/MavenDependencyResolver.java
https://github.com/robolectric/robolectric/blob/master/robolectric/src/main/java/org/robolectric/internal/InstrumentingClassLoaderFactory.java

So everything may work just fine normally, using Gradle; but fails when robolectric tries to download the same artifacts.

In my particular case, the reason it couldn't find the jar(s) was because it couldn't parse the .pom files. And the reason it couldn't parse the pom files was because those files consisted purely of this:

$ cat ~/.m2/repository/org/robolectric/android-all/5.0.0_r2-robolectric-1/android-all-5.0.0_r2-robolectric-1.jar
<html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx/1.8.1</center>
</body>
</html>

And the reason this was a 301 instead of content, is because my ~/.m2/settings.xml file has a catch-all <mirrorOf>*</mirrorOf> entry to point everything to my company's internal Nexus artifact repository -- and that mirror URL was set a long time ago to http://nexus..., but recently we moved that server and in the process migrated to SSL, so hitting that Nexus URL was redirecting (via 301) to https://nexus.... But apparently whatever Robolectric uses to download those artifacts does not follow redirects.

I updated my m2-settings file, and deleted the bogus 301 files from ~/.m2/repository, and now everything is better...

@cesar1000
Copy link
Contributor

@jhansche I'm not sure this is the root cause of this particular problem - we ran into the same problem at Twitter though and reached the same conclusion as you did. I could still repro the exception after that fix, and it seemed to be caused by an attempt to reset the environment for API 21 with the environment configured for API 18.

@panario
Copy link

panario commented Jan 16, 2017

deleting the .m2 directory (thx @drawers) fixed this for me.

@sampadakadam
Copy link

I'm having the same issue on sdk 21, and build process is gradle. Is there any fix for this?

@chendewan
Copy link

deleting the .m2 directory (thx @drawers) fixed this for me.

thanks!!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests