簡體   English   中英

在運行Android Instrumented測試進行房間遷移時,獲取GSON的ClassNotFoundException

[英]Getting ClassNotFoundException for GSON when running Android Instrumented test for Room migration

我有一個項目,我正在使用Room進行存儲,並且當前正在嘗試建立一個簡單遷移的測試。

我按照https://medium.com/androiddevelopers/testing-room-migrations-be93cdb0d975中描述的步驟進行了測試,如下所示:

@RunWith(AndroidJUnit4::class)
class MigrationsTest {

    private val DB_NAME = "test.db"

    private lateinit var testHelper: MigrationTestHelper

    @Before
    fun setup() {
        testHelper = MigrationTestHelper(
            androidx.test.platform.app.InstrumentationRegistry.getInstrumentation(),
            MyDatabase::class.java.canonicalName,
            FrameworkSQLiteOpenHelperFactory()
        )
    }

    @Test
    fun testMigrate1_2() {
        val db = testHelper.createDatabase(DB_NAME, 1)
        db.close()

        testHelper.runMigrationsAndValidate(DB_NAME, 2, true, MyDatabase.MIGRATION_1_2)
    }

}

但是,在物理設備上運行測試時,出現以下異常:

java.lang.NoClassDefFoundError: Failed resolution of: Lcom/google/gson/GsonBuilder;

MigrationTestHelper本身看起來還不錯,並且如果我嘗試在測試中創建GsonGsonBuilder對象, GsonBuilder收到相同的錯誤。

在我的依賴項中,我還添加了以下幾行(使用Kotlin DSL):

dependencies {
    ... 
    androidTestImplementation("android.arch.persistence.room:testing:1.1.1")
    androidTestImplementation("com.google.code.gson:gson:2.8.0")
}

運行應用程序本身很好,存儲可以按預期工作。

包含Gson並避免此錯誤的正確方法是什么? 對我來說,無法輕松測試交易是一個問題。

好吧,我知道了。 這只是一個保障問題。 每當找不到一個類時,您只需要確保它沒有被剝離即可。

就我而言,我將此行添加到了我的proguard文件中

-keep class com.google.gson.** { *; }

然后在抱怨時繼續添加更多說明。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM