Android gradle androidTestApi & testApi configuration obsoleted
This is Chris Margonis (Kudos!) answer in Kotlin-DSL:
// "base" module/project
configurations {
create("testDependencies"){
extendsFrom(configurations.testImplementation.get())
}
}
dependencies {
// example test dependency
testImplementation "junit:junit:4.12"
// .. other testImplementation dependencies here
}
//another module
dependencies {
testImplementation(project(path = ":base", configuration = "testDependencies"))
}
To complement the accepted answer, in your base module :core
add a configuration closure:
// build.gradle (core module)
configurations {
testDependencies.extendsFrom testImplementation
testRuntimeOnlyDependencies.extendsFrom testRuntimeOnly
}
dependencies {
def junit5_version = "5.6.0"
// dependencies
testImplementation "org.junit.jupiter:junit-jupiter-api:$junit5_version"
testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:$junit5_version"
testImplementation "org.junit.jupiter:junit-jupiter-params:$junit5_version"
testRuntimeOnly "org.junit.vintage:junit-vintage-engine:$junit5_version"
}
Then in your feature
module add:
dependencies {
implementation project(':core') {
testImplementation configurations.getByName('testDependencies').allDependencies
testRuntimeOnly configurations.getByName('testRuntimeOnlyDependencies').allDependencies
}
}
Now your testing dependencies are shared across modules.
Note: Modules core
and feature
are fictitious, please substitute accordingly.
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.