gpt4 book ai didi

android - 运行 gradlew 测试时出现 Robolectric 错误

转载 作者:搜寻专家 更新时间:2023-11-01 09:23:17 25 4
gpt4 key购买 nike

尝试运行gradlew testgradlew testDebugUnitTest 时 Robolectric 崩溃有这个错误

> 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 文件
  • 删除.idea\libraries
  • 删除清理/重建项目
  • 降级和升级 robolectric 版本
  • 将我的整个项目编译版本和 taget sdk 升级到 28
  • 将 gradle 的版本从 3.1.2 升级到 3.2.0

这是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);
}

最佳答案

使用 CLI 时,gradle 将使用安装在您机器上的 java 版本。就我而言,我机器上安装的 Java 版本是版本 10,Android 尚不支持该版本。您的测试在 Android Studio 上运行,因为它使用了 Android 支持的嵌入式 JDK。现在要解决您的问题,首先通过运行以下命令检查计算机上安装的 Java 版本:

java -version

如果版本是1.10.xxx,你应该:

  1. 通过此 link 下载并安装 JDK 8 .
  2. 将环境变量 JAVA_HOME 指向新安装的 JDK 8 路径,方法是在 bash_profile 中添加以下行(在 Mac 上):

    export JAVA_HOME = "$(/usr/libexec/java_home -v 1.8)"

  3. 通过新的终端窗口重新运行测试

关于android - 运行 gradlew 测试时出现 Robolectric 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52761004/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com