动态功能模块中的 androidTest espresso 测试用例未能 build/run - 任务 ':dfm:mergeLibDexDebugAndroidTest' 执行失败

androidTest espresso test cases in a Dynamic Feature Module failing to build/run - Execution failed for task ':dfm:mergeLibDexDebugAndroidTest'

我已将 app 模块中的一个功能分离到一个动态功能模块中(名为 dfm )。

该模块在我遵循所有最佳实践和要求的情况下完美运行。但是 androidTest 中使用 AndroidJUnit4 和 Espresso 的测试用例之一无法正常工作,并且在构建任务时失败并中断:mergeLibDexDebugAndroidTest。 该项目包括:

2020-07-15 20:40:42.705:INFO:oejs.ServerConnector:Daemon worker Thread 34: Stopped ServerConnector@eda0043{HTTP/1.1}{localhost:0}
    
    FAILURE: Build failed with an exception.
    
    * What went wrong:
    Execution failed for task ':dfm:**mergeLibDexDebugAndroidTest**'.
    > Could not resolve all files for configuration ':dfm:debugAndroidTestRuntimeClasspath'.
       > Failed to transform classes.jar (project :testlib) to match attributes {artifactType=android-dex, com.android.build.api.attributes.BuildTypeAttr=debug, com.android.build.api.attributes.VariantAttr=debug, dexing-enable-desugaring=true, dexing-incremental-desugaring-v2=false, dexing-is-debuggable=true, dexing-min-sdk=21, org.gradle.libraryelements=jar, org.gradle.usage=java-runtime, org.jetbrains.kotlin.platform.type=androidJvm}.
          > No variants of project :app match the consumer attributes:
              - Configuration ':app:debugRuntimeElements' variant android-navigation-json:
                  - Incompatible attribute:
                      - Required artifactType 'android-classes-jar' and found incompatible value 'android-navigation-json'.
                  - Other attributes:
                      - Found com.android.build.api.attributes.BuildTypeAttr 'debug' but wasn't required.
                      - Found com.android.build.api.attributes.VariantAttr 'debug' but wasn't required.
                      - Required dexing-enable-desugaring 'true' but no value provided.
                      - Required dexing-incremental-desugaring-v2 'false' but no value provided.
                      - Required dexing-is-debuggable 'true' but no value provided.
                      - Required dexing-min-sdk '21' but no value provided.
                      - Found org.gradle.usage 'java-runtime' but wasn't required.
                      - Found org.jetbrains.kotlin.platform.type 'androidJvm' but wasn't required.
              - Configuration ':app:debugRuntimeElements' variant android-packaged-dependencies:
                  - Incompatible attribute:
                      - Required artifactType 'android-classes-jar' and found incompatible value 'android-packaged-dependencies'.
                  - Other attributes:
                      - Found com.android.build.api.attributes.BuildTypeAttr 'debug' but wasn't required.
                      - Found com.android.build.api.attributes.VariantAttr 'debug' but wasn't required.
                      - Required dexing-enable-desugaring 'true' but no value provided.
                      - Required dexing-incremental-desugaring-v2 'false' but no value provided.
                      - Required dexing-is-debuggable 'true' but no value provided.
                      - Required dexing-min-sdk '21' but no value provided.
                      - Found org.gradle.usage 'java-runtime' but wasn't required.
                      - Found org.jetbrains.kotlin.platform.type 'androidJvm' but wasn't required.
              - Configuration ':app:debugRuntimeElements' variant apk:
                  - Incompatible attribute:
                      - Required artifactType 'android-classes-jar' and found incompatible value 'apk'.
                  - Other attributes:
                      - Found com.android.build.api.attributes.BuildTypeAttr 'debug' but wasn't required.
                      - Found com.android.build.api.attributes.VariantAttr 'debug' but wasn't required.
                      - Required dexing-enable-desugaring 'true' but no value provided.
                      - Required dexing-incremental-desugaring-v2 'false' but no value provided.
                      - Required dexing-is-debuggable 'true' but no value provided.
                      - Required dexing-min-sdk '21' but no value provided.
                      - Found org.gradle.usage 'java-runtime' but wasn't required.
                      - Found org.jetbrains.kotlin.platform.type 'androidJvm' but wasn't required.
              - Configuration ':app:debugRuntimeElements' variant bundle-apks:
                  - Incompatible attribute:
                      - Required artifactType 'android-classes-jar' and found incompatible value 'bundle-apks'.
                  - Other attributes:
                      - Found com.android.build.api.attributes.BuildTypeAttr 'debug' but wasn't required.
                      - Found com.android.build.api.attributes.VariantAttr 'debug' but wasn't required.
                      - Required dexing-enable-desugaring 'true' but no value provided.
                      - Required dexing-incremental-desugaring-v2 'false' but no value provided.
                      - Required dexing-is-debuggable 'true' but no value provided.
                      - Required dexing-min-sdk '21' but no value provided.
                      - Found org.gradle.usage 'java-runtime' but wasn't required.
                      - Found org.jetbrains.kotlin.platform.type 'androidJvm' but wasn't required. </pre>

我无法得到具体的错误,也无法理解它是否是 Gradle 文件或我的代码中的问题,因为 没有编译错误 任何地方。请帮助我理解错误。 谢谢

在错误日志中,我们看到以下行:

Failed to transform classes.jar (project :testlib) to match attributes {artifactType=android-dex, com.android.build.api.attributes.BuildTypeAttr=debug, com.android.build.api.attributes.VariantAttr=debug, dexing-enable-desugaring=true, dexing-incremental-desugaring-v2=false, dexing-is-debuggable=true, dexing-min-sdk=21, org.gradle.libraryelements=jar, org.gradle.usage=java-runtime, org.jetbrains.kotlin.platform.type=androidJvm}.

因此,在研究了上述消息并提示它与 testlib 相关之后,我重新检查了包含 androidTestImplementation project(":testlib")dfm/build.gradle。由于目前 testlibandroidTests 没有依赖性,这是不必要的,因此失败了。因此,我删除了使测试构建成功的不必要的 androidTestImplementation project(":testlib")