Issue
I have a project for which I am using Room for storage, and am currently trying to set up a test for a simple migration.
I followed the steps described at https://medium.com/androiddevelopers/testing-room-migrations-be93cdb0d975 and have a test that looks like this:
@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)
}
}
However, when running the test on a physical device, I get the following exception:
java.lang.NoClassDefFoundError: Failed resolution of: Lcom/google/gson/GsonBuilder;
The MigrationTestHelper itself seems fine, and if I try to create a Gson or GsonBuilder object within the test, I get the same error.
In my dependencies, I also added these lines (using Kotlin DSL):
dependencies {
...
androidTestImplementation("android.arch.persistence.room:testing:1.1.1")
androidTestImplementation("com.google.code.gson:gson:2.8.0")
}
Running the app itself is fine, storage works as expected.
What is the proper way to include Gson and avoid this error? Not being able to easily test transactions is an issue for me.
Solution
Okay I figured it out. It was just a proguard issue. Whenever a class isn't found, you just need to make sure it isn't stripped out.
In my case, I added this line to my proguard file
-keep class com.google.gson.** { *; }
And then keep adding more instructions whenever it complains.
Answered By - Rémi Vansteelandt
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.