Android rebuild project, package org.junit does no

2019-07-27 08:37发布

问题:

Error:(3, 17) error: package org.junit does not exist
Error:(5, 24) error: package org.junit does not exist
Error:(11, 6) error: cannot find symbol class Test
Error:(13, 9) error: cannot find symbol method assertEquals(int,int)
:app:compileDebugUnitTestJavaWithJavac FAILED
Error:Execution failed for task ':app:compileDebugUnitTestJavaWithJavac'

.

Compilation failed; see the compiler error output for details.

When i run rebuild, errors are those. But i can run my appwithout rebuild.

I dont use test, i dont know how it came to my folders.

The code is:

import org.junit.Test;

import static org.junit.Assert.*;

/**
 * To work on unit tests, switch the Test Artifact in the Build Variants view.
 */
public class ExampleUnitTest {
    @Test
    public void addition_isCorrect() throws Exception {
        assertEquals(4, 2 + 2);
    }
}

回答1:

Add this to your build.gradle

dependencies {
    ...
    testCompile 'junit:junit:4.12'
}


回答2:

This error happened to me when I was copy/pasting files from another project.

Closing Android Studio and reopening solved the problem.