提问人:Ichwan Sholihin 提问时间:8/12/2023 更新时间:8/12/2023 访问量:61
java.lang.NoClassDefFoundError:无法解析 Android 插桩测试中的 Lcom/myproject/mymodule/ClassName
java.lang.NoClassDefFoundError: Failed resolution of: Lcom/myproject/mymodule/ClassName in Android Instrumented Tests
问:
谁能帮忙解决这个问题?
以前,我尝试为我的项目创建一个插桩测试。我为单元测试创建了新模块,并且在创建类时模块中发生了错误,然后我生成了类来创建插桩测试(存储在包中),但是为什么当我运行同一包中的类时,它会出现标题中的错误?com.module.unit_test (androidTest)
ResourceComparer
ResourceComparer.kt
:
package com.ichwan.unit_test
import android.content.Context
class ResourceComparer {
fun isEqual(context: Context, resId: Int, compare: String) : Boolean {
return context.getString(resId) == compare
}
}
ResourceComparerTest.kt
:
package com.ichwan.unit_test
import android.content.Context
import androidx.test.core.app.ApplicationProvider
import com.google.common.truth.Truth.assertThat
import org.junit.After
import org.junit.Before
import org.junit.Test
class ResourceComparerTest {
private lateinit var resourceComparer: ResourceComparer
@Before
fun setup() {
resourceComparer = ResourceComparer()
}
@After
fun tearDown(){
}
@Test
fun stringResourceSameAsGivenString_returnsTrue() {
val context = ApplicationProvider.getApplicationContext<Context>()
val result = resourceComparer.isEqual(context, androidx.compose.ui.R.string.selected, "selected")
assertThat(result).isTrue()
}
@Test
fun stringResourceSameAsGivenString_returnsFalse() {
val context = ApplicationProvider.getApplicationContext<Context>()
val result = resourceComparer.isEqual(context, androidx.compose.ui.R.string.selected, "selection")
assertThat(result).isFalse()
}
}
这是 logcat 的结果:
java.lang.NoClassDefFoundError: Failed resolution of: Lcom/ichwan/unit_test/ResourceComparer;
at java.lang.Class.getDeclaredFields(Native Method)
at org.junit.runners.model.TestClass.getSortedDeclaredFields(TestClass.java:77)
at org.junit.runners.model.TestClass.scanAnnotatedMembers(TestClass.java:70)
at org.junit.runners.model.TestClass.<init>(TestClass.java:57)
at org.junit.runners.ParentRunner.createTestClass(ParentRunner.java:111)
at org.junit.runners.ParentRunner.<init>(ParentRunner.java:91)
at org.junit.runners.BlockJUnit4ClassRunner.<init>(BlockJUnit4ClassRunner.java:74)
at androidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner.<init>(AndroidJUnit4ClassRunner.java:43)
at androidx.test.internal.runner.junit4.AndroidJUnit4Builder.runnerForClass(AndroidJUnit4Builder.java:51)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:70)
at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:37)
at androidx.test.internal.runner.AndroidRunnerBuilder.runnerForClass(AndroidRunnerBuilder.java:149)
at androidx.test.internal.runner.ScanningTestLoader.doCreateRunner(ScanningTestLoader.java:50)
at androidx.test.internal.runner.TestLoader.getRunnersFor(TestLoader.java:64)
at androidx.test.internal.runner.TestRequestBuilder.build(TestRequestBuilder.java:835)
at androidx.test.runner.AndroidJUnitRunner.buildRequest(AndroidJUnitRunner.java:650)
at androidx.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:418)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1932)
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.ichwan.unit_test.ResourceComparer" on path: DexPathList[[zip file "/system/framework/android.test.runner.jar", zip file "/data/app/com.ichwan.unit_test.test-2/base.apk", zip file "/data/app/com.ichwan.gigihmodule-2/base.apk", zip file "/data/app/com.ichwan.gigihmodule-2/split_config.en.apk", zip file "/data/app/com.ichwan.gigihmodule-2/split_config.xxhdpi.apk"],nativeLibraryDirectories=[/data/app/com.ichwan.unit_test.test-2/lib/x86, /data/app/com.ichwan.gigihmodule-2/lib/x86, /system/lib, /vendor/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
at java.lang.ClassLoader.loadClass(ClassLoader.java:380)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
... 18 more
Suppressed: java.io.IOException: No original dex files found for dex location /data/app/com.ichwan.gigihmodule-2/split_config.en.apk
at dalvik.system.DexFile.openDexFileNative(Native Method)
at dalvik.system.DexFile.openDexFile(DexFile.java:373)
at dalvik.system.DexFile.<init>(DexFile.java:113)
at dalvik.system.DexFile.<init>(DexFile.java:78)
at dalvik.system.DexPathList.loadDexFile(DexPathList.java:359)
at dalvik.system.DexPathList.makeElements(DexPathList.java:323)
at dalvik.system.DexPathList.makeDexElements(DexPathList.java:263)
at dalvik.system.DexPathList.<init>(DexPathList.java:126)
at dalvik.system.BaseDexClassLoader.<init>(BaseDexClassLoader.java:48)
at dalvik.system.PathClassLoader.<init>(PathClassLoader.java:64)
at com.android.internal.os.PathClassLoaderFactory.createClassLoader(PathClassLoaderFactory.java:43)
at android.app.ApplicationLoaders.getClassLoader(ApplicationLoaders.java:58)
at android.app.LoadedApk.createOrUpdateClassLoaderLocked(LoadedApk.java:520)
at android.app.LoadedApk.getClassLoader(LoadedApk.java:553)
at android.app.ActivityThread.getTopLevelResources(ActivityThread.java:1847)
at android.app.LoadedApk.getResources(LoadedApk.java:766)
at android.app.ContextImpl.<init>(ContextImpl.java:2066)
at android.app.ContextImpl.createAppContext(ContextImpl.java:2011)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5252)
at android.app.ActivityThread.-wrap2(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1528)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
Suppressed: java.io.IOException: No original dex files found for dex location /data/app/com.ichwan.gigihmodule-2/split_config.xxhdpi.apk
at dalvik.system.DexFile.openDexFileNative(Native Method)
at dalvik.system.DexFile.openDexFile(DexFile.java:373)
at dalvik.system.DexFile.<init>(DexFile.java:113)
at dalvik.system.DexFile.<init>(DexFile.java:78)
at dalvik.system.DexPathList.loadDexFile(DexPathList.java:359)
at dalvik.system.DexPathList.makeElements(DexPathList.java:323)
at dalvik.system.DexPathList.makeDexElements(DexPathList.java:263)
at dalvik.system.DexPathList.<init>(DexPathList.java:126)
at dalvik.system.BaseDexClassLoader.<init>(BaseDexClassLoader.java:48)
at dalvik.system.PathClassLoader.<init>(PathClassLoader.java:64)
at com.android.internal.os.PathClassLoaderFactory.createClassLoader(PathClassLoaderFactory.java:43)
at android.app.ApplicationLoaders.getClassLoader(ApplicationLoaders.java:58)
at android.app.LoadedApk.createOrUpdateClassLoaderLocked(LoadedApk.java:520)
at android.app.LoadedApk.getClassLoader(LoadedApk.java:553)
at android.app.ActivityThread.getTopLevelResources(ActivityThread.java:1847)
at android.app.LoadedApk.getResources(LoadedApk.java:766)
at android.app.ContextImpl.<init>(ContextImpl.java:2066)
at android.app.ContextImpl.createAppContext(ContextImpl.java:2011)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5252)
at android.app.ActivityThread.-wrap2(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1528)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
我在这里阅读了几个来源,但其中大多数都有来自 Gradle 的额外依赖项。但是我很困惑,如果错误如logcat中所述,是否有其他库或缺少gradle脚本?
这是我的脚本:build.gradle (module)
plugins {
id 'com.android.dynamic-feature'
id 'org.jetbrains.kotlin.android'
}
android {
namespace 'com.ichwan.unit_test'
compileSdk 34
defaultConfig {
applicationId "com.ichwan.unit_test"
minSdk 24
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables {
useSupportLibrary true
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
kotlinOptions {
jvmTarget = '1.8'
}
buildFeatures {
viewBinding true
dataBinding true
}
}
dependencies {
implementation project(":app")
implementation 'androidx.core:core-ktx:1.10.1'
implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'com.google.android.material:material:1.9.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
testImplementation 'junit:junit:4.13.2'
testImplementation 'androidx.test:core:1.5.0'
testImplementation 'com.google.truth:truth:1.1.4'
androidTestImplementation 'com.google.truth:truth:1.1.4'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
androidTestImplementation 'androidx.annotation:annotation:1.6.0'
}
答: 暂无答案
评论