尝试运行gradlew test
或gradlew testDebugUnitTest
时发生机器人崩溃
出现此错误
> Task :app:testDebugUnitTest
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.robolectric.util.ReflectionHelpers$7 (file:/C:/Users/a.yasser/.gradle/caches/modules-2/files-2.1/org.robolectric/shadowapi/3.8/d638f001e81ef737bb35db2964312360cc996a94/shadowapi-3.8.jar)
to method java.lang.ClassLoader.getPackage(java.lang.String)
WARNING: Please consider reporting this to the maintainers of org.robolectric.util.ReflectionHelpers$7
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
ash.gaseelydriver.ui.activities.MainActivityTest > shouldNotBeNull FAILED
java.lang.NoClassDefFoundError
Caused by: java.lang.ClassNotFoundException
Caused by: java.lang.RuntimeException
Caused by: java.lang.IllegalArgumentException
仅当我从命令行运行时,我尝试了多种解决方案,但没有一个解决方案,包括
这是gradle文件
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "ash.gaseelydriver"
minSdkVersion 17
targetSdkVersion 28
versionCode 2
versionName "1.1"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
signingConfigs {
release {
storeFile file('ASH_Mobile.jks')
storePassword "Ahmedsh125"
keyAlias "ash_mobile"
keyPassword "Ahmedsh125"
}
}
buildTypes {
release {
zipAlignEnabled true
signingConfig signingConfigs.release
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
testOptions {
unitTests {
includeAndroidResources = true
}
}
buildToolsVersion '28.0.2'
}
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support:design:28.0.0'
implementation 'com.android.support:recyclerview-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.android.support:support-v4:28.0.0'
implementation 'com.android.support:cardview-v7:28.0.0'
testImplementation 'junit:junit:4.12'
implementation 'com.google.android.gms:play-services-location:16.0.0'
implementation 'com.google.android.gms:play-services-places:16.0.0'
implementation 'com.wang.avi:library:2.1.3'
implementation 'com.squareup.retrofit2:retrofit:2.3.0'
implementation 'com.squareup.retrofit2:converter-gson:2.3.0'
implementation 'com.squareup.retrofit2:adapter-rxjava2:2.3.0'
implementation 'io.reactivex.rxjava2:rxandroid:2.0.1'
implementation 'com.jakewharton:butterknife:8.5.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.5.1'
implementation 'com.squareup.picasso:picasso:2.5.2'
implementation 'com.intuit.sdp:sdp-android:1.0.4'
implementation 'com.intuit.ssp:ssp-android:1.0.5'
implementation 'com.nineoldandroids:library:2.4.0'
implementation 'me.dm7.barcodescanner:zxing:1.9.8'
testImplementation "org.robolectric:robolectric:3.8"
testImplementation 'org.mockito:mockito-core:2.7.22'
androidTestImplementation 'org.mockito:mockito-android:2.7.22'
implementation 'com.google.firebase:firebase-database:16.0.3'
implementation 'com.google.firebase:firebase-core:16.0.4'
testImplementation 'com.openpojo:openpojo:0.8.10'
}
apply plugin: 'com.google.gms.google-services'
这是一个示例测试类
@RunWith(RobolectricTestRunner.class)
@Config(constants = BuildConfig.class, sdk = 27)
public class MainActivityTest {
private MainActivity activity;
@Before
public void setUp() {
activity = Robolectric.buildActivity(MainActivity.class).create()
.resume()
.get();
}
@Test
public void shouldNotBeNull() throws Exception {
assertNotNull(activity);
}
答案 0 :(得分:2)
使用CLI时,gradle将使用计算机上安装的Java版本。就我而言,我机器上安装的Java版本是版本10,Android尚不支持该版本。 您的测试在Android Studio上运行,因为它使用了Android支持的嵌入式JDK。 现在要解决您的问题,请首先通过运行以下命令检查计算机上安装的Java版本:
java -version
如果版本为1.10.xxx,则应该:
通过在bash_profile中添加以下行(在Mac上),将环境变量JAVA_HOME指向安装的JDK 8的新路径:
export JAVA_HOME =“ $(/ usr / libexec / java_home -v 1.8)”
通过新的终端窗口重新运行测试