2016-10-14 82 views
2

我的項目成功運行想到的是,當我清理和重建項目,我 收到此錯誤獲得在ExampleInstrumentedTest.java錯誤文件

Information:Gradle tasks [clean, :app:generateDebugSources, :app:generateDebugAndroidTestSources, :app:mockableAndroidJar, :app:prepareDebugUnitTestDependencies, :app:compileDebugSources, :app:compileDebugAndroidTestSources, :app:compileDebugUnitTestSources] 
D:\Gonna Projects\DoctorApps\app\src\androidTest\java\in\doctormobileapps\doctorapps\ExampleInstrumentedTest.java 
Error:(4, 28) error: package android.support.test does not exist 
Error:(5, 35) error: package android.support.test.runner does not exist 
Error:(7, 17) error: package org.junit does not exist 
Error:(8, 24) error: package org.junit.runner does not exist 
Error:(11, 24) error: package org.junit does not exist 
Error:(18, 2) error: cannot find symbol class RunWith 
Error:(20, 6) error: incompatible types: Test cannot be converted to Annotation 
Error:(23, 30) error: cannot find symbol variable InstrumentationRegistry 
Error:Execution failed for task ':app:compileDebugAndroidTestJavaWithJavac'. 
> Compilation failed; see the compiler error output for details. 
Information:BUILD FAILED 
Information:Total time: 4.834 secs 
Information:9 errors 
Information:0 warnings 
Information:See complete output in console 

有沒有誰可以告訴我什麼是錯的,我沒有?

回答

4

看起來您並未將Junit軟件包添加到模塊的 build.gradle文件中。剛剛在依賴塊添加這一點,它應該工作:

androidTestCompile 'junit:junit:4.12' 
testCompile 'junit:junit:4.12' 
+0

感謝回答,但它並沒有解決這個問題。 – Champandorid

+0

然後請提供您用於Testclass和您的build.gradle文件的代碼。 – Chris

+0

解決了我的問題!出於某種原因,Android Studio在生成build.gradle文件時未包含androidTestCompile行... – Chriszuma

4

當我從頭開始創建一個新的模塊,Android的工作室將產生下面的測試相關的依賴默認gradle這個文件:

androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { 
    exclude group: 'com.android.support', module: 'support-annotations' 
}) 

這似乎可以解決您的構建日誌中的至少一些依賴問題。由於還缺少org.junit參考,因此您可能還需要將junit作爲androidTestCompile依賴項添加,正如@Chris已經回答的那樣;或者它可能被espresso隱含包含在內。

0

有可能會丟失在您的gradle這個編譯依賴這些添加一行:

testImplementation 'junit:junit:4.12' 
androidTestImplementation 'com.android.support.test:runner:0.5' 
androidTestImplementation 'com.android.support.test.espresso:espresso-core:2.2.2'