我试图运行gradle任务“ compileUnitTestJava”,但我总是收到“找不到符号”的错误,并且我不明白为什么。
我只能向您显示一个与真实项目具有相同错误的Testproject,因为它的正常工作和真实代码无法发布。
MainActivity:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public int test(){
return 5;
}
}
测试类:
package com.example.testapplication;
import org.junit.Test;
import static org.junit.Assert.*;
public class MainActivityTest {
@Test
public void test1() {
MainActivity ma = new MainActivity();
assertEquals(5, ma.test());
}
}
build.gradle:
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.example.testapplication"
minSdkVersion 15
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
lintOptions {
abortOnError false
}
}
sourceSets {
unitTest {
java {
srcDirs = ['src/test/java']
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.android.support:design:28.0.0'
testImplementation 'junit:junit:4.12'
unitTestImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
task abcdefg(type:Test, dependsOn: 'assemble') {
testClassesDirs = project.sourceSets.unitTest.output.classesDirs
classpath = project.sourceSets.unitTest.runtimeClasspath
}
这些错误总是在发生。
> Task :app:compileUnitTestJava FAILED
C:\Users\nne\Downloads\TestApplication\app\src\test\java\com\example\testapplication\MainActivityTest.java:11: error: cannot find symbol
MainActivity ma = new MainActivity();
^
symbol: class MainActivity
location: class MainActivityTest
C:\Users\nne\Downloads\TestApplication\app\src\test\java\com\example\testapplication\MainActivityTest.java:11: error: cannot find symbol
MainActivity ma = new MainActivity();
^
symbol: class MainActivity
location: class MainActivityTest
2 errors