0

I am facing below problem. I have three projects. I call them projects A,B and lib

Project Lib
-> androidTest
   -> utilityfile.kt

Project A
-> androidTest
   -> <use utilityfile.kt here>

Project B
-> androidTest
   -> <use utilityfile.kt here>

Now I have one utility inside lib project which is obviously a library project. I can put my utility file only inside androidTest and not under main or test directory. (This is something I can't change)

Now I want to access that test utility which is inside lib module, into projects A and B. But gradle is giving me errors at runtime.

The error is 'compilation error' when I try to run android test case in project A or B.

I searched so much and found using old gradle version there was a way e.g

https://stackoverflow.com/a/32083131/1166926

testCompile project(':libProject').sourceSets.test.output

and also https://stackoverflow.com/a/5648565/1166926

But these are not working for me now, as newer gradle doesn't support this now.

What can be done to access the androidTest class files into another project's androidTest directory?

Hardik Trivedi
  • 5,677
  • 5
  • 31
  • 51

1 Answers1

1

You should extract that test utility into its own separate library module, and then add that as an androidTestImplementation on projects A, B, and Lib.

For example:

TestUtils Lib
-> src/main/java
   -> UtilityFile.kt

Project A
-> androidTestImplementation project(":testUtils")

Project B
-> androidTestImplementation project(":testUtils")

Project Lib
-> androidTestImplementation project(":testUtils")
Kevin Coppock
  • 133,643
  • 45
  • 263
  • 274
  • In an ideal situation this is the solution, which I am aware of. The constraint for me is I can not put my utility file inside main? Because it has an import to one more lib which only works in androidTest directory only. I have mentioned this in a question. – Hardik Trivedi Dec 18 '18 at 16:05
  • @Kevin Coppock I've did this, but still it doesn't render the customView from the `testUtils` module, any idea? I've openned a question feel free to take a look. – Skizo-ozᴉʞS ツ Jun 16 '21 at 09:44