Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NoClassDefFoundError when running unit test with Gradle task

I have a project using dynamic feature module, and I want to run my unit test in feature module via gradle task (for my CI purpose):

./gradlew :feature_product:test

But it always gives me NoClassDefFoundError for tests that have dependencies on classes from the base module:

com.example.android.feature.product.ProductViewTest > on vote change to negative FAILED
java.lang.NoClassDefFoundError: app.BaseView

ProductView class from the feature module extends BaseView from the base module.

Oddly, it succeeds when run in Android Studio, it works fine.

android studio

Then I notice something different in the logs, when I run via command line and when I run Android Studio. The first line in the Android Studio is generateDebugSources, something which absent when I run ./gradlew test

Executing tasks: [:lib_ui:generateDebugSources, ...]

How do I fix this? Does Android Studio has different command with the provided command ./gradlew test when I press Ctrl+Shift+R ?

like image 603
Fadel Trivandi Dipantara Avatar asked Jun 26 '19 06:06

Fadel Trivandi Dipantara


People also ask

How do you fix No Class Def Found error?

lang. NoClassDefFoundError, which means the Class Loader file responsible for dynamically loading classes can not find the . class file. So to remove this error, you should set your classpath to the location where your Class Loader is present.

How do I run all unit tests in gradle?

Run Gradle testsIn your Gradle project, in the editor, create or select a test to run. From the context menu, select Run <test name>. icon in the left gutter. If you selected the Choose per test option, IntelliJ IDEA displays both Gradle and JUnit test runners for each test in the editor.

What is classpath in gradle?

Gradle dependencies are grouped into sets called configurations. Different configurations are used for building classpath for the major two tasks — compile classpath is used for compilation and runtime classpath is used for running the application.


1 Answers

After searching further about this issue, I found it also being reported in the android-test and app-bundle-samples projects and there is also an issue in the issue tracker.

It turns out this issue fixed in the Android Gradle Plugin 4.1.0 as per comment in the issue tracker.

If you don't want to update AGP to 4.1.0 which is still in alpha, adding this to the feature module's build.gradle fixed the issue for me, as per this comment:

testRuntimeOnly(files("$projectDir/../b_app/build/intermediates/app_classes/debug/classes.jar"))
like image 104
Gustavo Avatar answered Oct 19 '22 10:10

Gustavo