Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android gradle androidTestApi & testApi configuration obsoleted

I got 2 modules, module A and module B. Module B depends on module A, module A shares dependency libraries to module B by using api configuration.

When setting up test environment, inside module A, I also use testApi & androidTestApi to make module B using shared test libraries. However, after running gradle sync, I got warning message: WARNING: Configuration 'testApi' is obsolete and has been replaced with 'testImplementation'.

Read the provided link and it said that other modules can't depend on androidTest, you get the following warning if you use the androidTestApi configuration. Therefore, I must define test libraries in module B in my example for skipping this warning.

I have some questions on this situation:

  1. Why one module should not depends on testing dependencies of other module, although it can depend on normal dependencies defined as api?
  2. Do we have anyway to force module B depends on test libraries of module A without defined these libraries again in module B?

Many thanks

like image 994
Vinh Nguyen Avatar asked Oct 01 '18 08:10

Vinh Nguyen


People also ask

What is testImplementation in Gradle?

For example the testImplementation configuration extends the implementation configuration. The configuration hierarchy has a practical purpose: compiling tests requires the dependencies of the source code under test on top of the dependencies needed write the test class.

How do I add a dependency in Gradle?

To add a dependency to your project, specify a dependency configuration such as implementation in the dependencies block of your module's build.gradle file.


1 Answers

The way I did this was by creating a custom configuration. In your case inside the build.gradle file module A add:

configurations {
    yourTestDependencies.extendsFrom testImplementation
}

dependencies {
    // example test dependency
    testImplementation "junit:junit:4.12"
    // .. other testImplementation dependencies here
}

And in the build.gradle of module B add:

dependencies {
    testImplementation project(path: ':moduleA', configuration: 'yourTestDependencies')
}

The above will include all testImplementation dependencies declared in module A to module B.

like image 164
Chris Margonis Avatar answered Sep 20 '22 22:09

Chris Margonis