简体   繁体   中英

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.

安卓工作室

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 ?

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"))

If it is a missing task that you believe is necessary then calling it first like below should do the trick:

./gradlew :lib_ui:generateDebugSources :feature_product:test

I would even go full on and assemble the dependencies if necessary though that might take more time:

./gradlew :lib_ui:assemble :feature_product:assemble :feature_product:test

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM