gpt4 book ai didi

android - 在仪器测试中找不到 Room.inMemoryDatabaseBuilder()

转载 作者:行者123 更新时间:2023-11-29 02:34:40 25 4
gpt4 key购买 nike

我正在尝试为我的应用程序中的 Room DB 创建一个仪器测试,但我不断收到此错误:

java.lang.NoSuchMethodError: No static method inMemoryDatabaseBuilder(Landroid/content/Context;Ljava/lang/Class;)Landroid/arch/persistence/room/RoomDatabase$Builder; in class Landroid/arch/persistence/room/Room; or its super classes (declaration of 'android.arch.persistence.room.Room' appears in /data/app/com.example.android-nhsbta60PW8T0UpasgtR0B==/base.apk)
at com.example.android.FooDaoTest.<init>(FooDaoTest.kt:40)
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:334)
at org.junit.runners.BlockJUnit4ClassRunner.createTest(BlockJUnit4ClassRunner.java:217)
at org.junit.runners.BlockJUnit4ClassRunner$1.runReflectiveCall(BlockJUnit4ClassRunner.java:266)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:58)
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:375)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:2074)

这是我的 Gradle 构建配置:

项目:

buildscript {
ext {
kotlinVersion = '1.2.0'

supportLibVersion = '26.1.0'
archLibVersion = '1.0.0'

...

// Test libs
androidTestLibVersion = '1.0.1'
espressoVersion = '3.0.1'
powerMockVersion = '1.7.1'
}

repositories {
google()
jcenter()
}

dependencies {
classpath 'com.android.tools.build:gradle:3.0.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
}
}

allprojects {
repositories {
google()
jcenter()
}
}

应用:

android {
compileSdkVersion 26
buildToolsVersion '26.0.3'

defaultConfig {
applicationId "com.example.android"

minSdkVersion 17
targetSdkVersion 26

versionCode 1
versionName "0.1"

...

multiDexEnabled true

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}

buildTypes {
debug {
minifyEnabled true
useProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
release {
minifyEnabled true
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])

implementation 'com.android.support:multidex:1.0.2'

implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlinVersion"

...

implementation "android.arch.lifecycle:extensions:$archLibVersion"
implementation "android.arch.persistence.room:runtime:$archLibVersion"
kapt "android.arch.lifecycle:compiler:$archLibVersion"
kapt "android.arch.persistence.room:compiler:$archLibVersion"

...

// Test libs

testImplementation 'junit:junit:4.12'
testImplementation 'org.mockito:mockito-core:2.8.47'
testImplementation "org.powermock:powermock-module-junit4:$powerMockVersion"
testImplementation "org.powermock:powermock-api-mockito2:$powerMockVersion"
testImplementation 'org.hamcrest:hamcrest-library:1.3'
testImplementation "android.arch.core:core-testing:$archLibVersion"
testImplementation "com.squareup.okhttp3:mockwebserver:3.9.1"

androidTestImplementation "com.android.support.test:runner:$androidTestLibVersion"
androidTestImplementation "com.android.support.test:rules:$androidTestLibVersion"
androidTestImplementation("com.android.support.test.espresso:espresso-core:$espressoVersion", {
exclude group: 'com.android.support', module: 'support-annotations'
})
androidTestImplementation "com.android.support.test.espresso:espresso-contrib:$espressoVersion"
androidTestImplementation "com.android.support.test.espresso:espresso-intents:$espressoVersion"
androidTestImplementation "com.android.support.test.espresso:espresso-idling-resource:$espressoVersion"
androidTestImplementation 'com.android.support.test.uiautomator:uiautomator-v18:2.1.3'

androidTestUtil "com.android.support.test:orchestrator:$androidTestLibVersion"
}

这是我的仪器测试:

@RunWith(AndroidJUnit4::class)
class FooDaoTest {

private lateinit var db: AppDatabase

private lateinit var fooDao: FooDao


@Before
fun setUp() {
db = Room.inMemoryDatabaseBuilder(InstrumentationRegistry.getContext(), AppDatabase::class.java).build()

fooDao = db.fooDao()
}

@After
fun closeDb() {
// db.close()
}

@Test
fun save() {
...
}
}

我猜这个错误与 multidex 有关,但我不明白为什么。有谁知道为什么会发生这种情况以及我如何才能使这个测试起作用?

最佳答案

似乎这是我遇到的一些构建问题,我发布了为我修复它的内容以防它帮助其他人:

我多次尝试从 Android Studio 清理项目,但问题仍然存在,但是,当我使用以下命令从命令行清理项目时:

./gradlew clean

然后再次运行测试它成功了。

更新:

经过更多测试后,我发现真正的问题出在我的 Proguard 配置上。我在调试版本中使用 Proguard,它删除了所有未使用的类/方法(Room.inMemoryDatabaseBuilder() 是其中之一)。一旦我为调试版本禁用 Proguard 并按照上面指定的方式清理项目,它就开始工作了。总结一下,解决方案是:

  1. 为调试版本禁用 Proguard(或向调试版本添加测试 Proguard 配置):

选项 1:

buildTypes {
// debug {
// minifyEnabled true
// useProguard false
// proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
// }
...
}

选项 2:

buildTypes {
debug {
minifyEnabled true
useProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'

testProguardFiles 'proguard-rules-test.pro'
}
...
}

proguard-rules-test.pro 的内容将取决于您的应用需要什么。

  1. 从命令行清理项目:

    ./gradlew clean

关于android - 在仪器测试中找不到 Room.inMemoryDatabaseBuilder(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47763453/

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