Android - How to share dependencies between testImplementation and androidTestImplementation

39
September 21, 2021, at 03:40 AM

I'm still fairly new to Android and Gradle, coming from a webdev/maven background. I have the MockK library which I want to use in both "normal" unit tests as well as instrumented androidTest tests. I currently have it defined as a testImplementation, however then I cannot import it into my androidTest files. I could add it twice, as a testImplementation and an androidTestImplementation:

testImplementation 'io.mockk:mockk:1.12.0'
androidTestImplementation 'io.mockk:mockk:1.12.0'

But that just feels wrong. On top of that, when I run MockKAnnotations.init() in my androidTest, I get this error:

Caused by: java.lang.ClassNotFoundException: Didn't find class "io.mockk.proxy.android.AndroidMockKAgentFactory"

I'm trying to understand how to properly configure my dependencies for this scenario. Help would be appreciated. Thanks.

EDIT

Ok, so I got that scenario working. I needed to use mockk for testImplementation, and mockk-android for androidTestImplementation:

testImplementation 'io.mockk:mockk:1.12.0'
androidTestImplementation 'io.mockk:mockk-android:1.12.0'

Next problem: coroutines. I've added kotlin-coroutines-test to try and help me with coroutine testing, because I get the Main dispatcher error when I try running without it.

androidTestImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:1.5.2"

The problem is now I get this really weird error:

Execution failed for task ':app:mergeDebugAndroidTestJavaResource'.
> A failure occurred while executing com.android.build.gradle.internal.tasks.MergeJavaResWorkAction
   > 2 files found with path 'META-INF/AL2.0' from inputs:
      - /home/me/.gradle/caches/modules-2/files-2.1/net.java.dev.jna/jna-platform/5.5.0/af38e7c4d0fc73c23ecd785443705bfdee5b90bf/jna-platform-5.5.0.jar
      - /home/me/.gradle/caches/modules-2/files-2.1/net.java.dev.jna/jna/5.5.0/e0845217c4907822403912ad6828d8e0b256208/jna-5.5.0.jar
     Adding a packagingOptions block may help, please refer to
     https://google.github.io/android-gradle-dsl/current/com.android.build.gradle.internal.dsl.PackagingOptions.html
     for more information

Still trying to work my way through this, would appreciate the help.

READ ALSO
Rinvex laravel-addresses

Rinvex laravel-addresses

As a beginner Laravel, as using it for few weeks, I find it very frustrating and lost with Rinvex Laravel address and their instruction is a bit lost for beginner people like me, I have lookup google and seems not many details or induction on Rinvex...

55
Can't import from absolute paths using Typescript and NodeJS

Can't import from absolute paths using Typescript and NodeJS

I've tried many things to solve this issueI'm trying to import from absolute paths defined on my tsconfig

62
How to store data from user into Array or ArrayList? [closed]

How to store data from user into Array or ArrayList? [closed]

Want to improve this question? Add details and clarify the problem by editing this post

71
not rendering content until needed

not rendering content until needed

I am trying to make my webpage load quicker and be more responsiveI am loading in upto 4k images that a user can scroll down through, apply filters to, and sort

25