java.lang.ClassNotFoundException for class in JUnit test in Android app module

Viewed 418

I'm working on a modular Android app where I have a base :app module and multiple dependent modules.

enter image description here

The :app module contains some shared business logic and models, including a DisplayItem model that gets displayed differently in each module (different sections of the UI).

All of this works fine, until I run my Unit tests in my module via the command line. When I run my tests via the Gradle wrapper, the tests in my modules fail with a ClassNotFoundException when trying to load the DisplayItem class in my tests.

com.me.news.NewsViewModelTest > initializationError FAILED
    java.lang.NoClassDefFoundError
        Caused by: java.lang.ClassNotFoundException

What I can't understand is why these tests run fine in Android Studio, and everything is compiling and resolving as expected, but then they fail when run from the command line. I suspect Android Studio is doing something to get these classes at runtime that isn't happening with the Gradle wrapper alone, but I can't figure out what.

Any thoughts about what is causing this error, what Android Studio may be doing, and how I could troubleshoot my Gradle build/project to fix these issues?

1 Answers

Add this line to your app build.gradle class. It's working fine for me

testRuntimeOnly(files("${project.rootDir}/FEATURE_MODULE_NAME/intermediates/app_classes/nonProdDebug/classes.jar"))

Related