Search code examples
androidunit-testingkotlinrobolectricandroidx-test

java.lang.NoClassDefFoundError: Test Module could not resolve classes of other Feature Module, Unit Testing - Kotlin - Android


I have the following Project Structure:

Main App
 --app
 --featureModule1
 --featureModule2
 --TestKit(Library module)

where Testkit has all dependencies including app and featureModules. The Testkit included all the unit tests related to the features in app and featuremodules.

When i run unit tests from Android studio(Right click-> Run Test in Testkit), they run fine. However whenever i try running it from gradle command: ./gradlew TestKit:testInternalDebugUnitTest, it throws NoClassDefFoundError for all the dependencies of other modules(for both app and feature modules).

Also i have already added implementation and testImplementation dependencies of all the modules in test module.

Please suggest:

  1. Do i need to add path of classes generated in other modules, if yes pls guide where.

  2. Also running Test with Coverage fails with

    java.lang.VerifyError: Bad return type

Please suggest what am i doing wrong here.

Note: I have created a Testkit because of the multiple open issues of Roboelectric in feature modules for ex: https://github.com/robolectric/robolectric/issues/5428

Versions AS: 4.0.1 gradle: 4.0.1 gradle-wrapper: 6.5 Robolectric: 4.3.1


Solution

  • I was able to solve the problem using the following runtime test dependencies:

    testRuntimeOnly(files("${projectDir}/../app/build/intermediates/app_classes/internalDebug/classes.jar"))
    testRuntimeOnly(files("${projectDir}/../featureModule1/build/intermediates/app_classes/internalDebug/classes.jar"))    
    testRuntimeOnly(files("${projectDir}/../featureModule2/build/intermediates/app_classes/internalDebug/classes.jar"))
    

    inside build.gradle of Testkit.

    Can refer to the following link for more details:https://github.com/android/app-bundle-samples/issues/11#issuecomment-675725610