Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Skip Gradle "testClasses" task for a dependency project

I have followed this guide to create a JUnit test file for my main Android module (let's call it "module-a"), in Android Studio v1.4.

My "module-a" has a dependency on an external library that is provided as a .aar file and for which I had to create a dedicated module.

This dependency causes an error:

When right clicking the test Java file and hitting "Run MyTestName" , it fails with this error

Error:Gradle: 
FAILURE: Build failed with an exception.

* What went wrong:
Task 'testClasses' not found in project ':module-b'.

Removing the dependency on module-b solves the problem.

Excerpt of module-a build.gradle:

compile project(':module-b')

module-b build.gradle:

configurations.create("default")
artifacts.add("default", file('library-b.aar'))

How should I configure Gradle so that it does not try to run the testClasses task on "module-b" ? (this should solve my issue)

like image 272
Sébastien Avatar asked Oct 14 '15 18:10

Sébastien


People also ask

How do I skip a task in Gradle?

To skip any task from the Gradle build, we can use the -x or –exclude-task option. In this case, we'll use “-x test” to skip tests from the build. As a result, the test sources aren't compiled, and therefore, aren't executed.

What is Gradle assemble task?

Assemble is a lifecycle task with no actions attached. Lifecycle tasks often combine other tasks and are useful to run in your development workflow. The purpose of assemble is to combine all tasks which produce a distribution or artifact into a single task.

How do I list tasks in Gradle?

To get an overview of all Gradle tasks in our project, we need to run the tasks task. Since Gradle 5.1, we can use the --group option followed by a group name. Gradle will then show all tasks belonging to the group and not the other tasks in the project.

What does Gradle check do?

Check is a lifecycle task. Lifecycle tasks often combine several other tasks and are helpful to run as part of your development workflow. The purpose of check is to combine all the verification tasks into a single task. If you've used Maven before, you can think of check as similar to the Maven verify phase.


1 Answers

I did not find a way to skip the testClasses task for module-b: it seems that actions started from Android Studio (like running a JUnit test) run Gradle commands that cannot be modified. In my case:

Information:Gradle: Executing tasks:
[:module-a:prepareFree_flavorDebugUnitTestDependencies,
 :module-a:generateFree_flavorDebugSources,
 :module-a:mockableAndroidJar,
 :module-a:assembleFree_flavorDebug,
 :module-a:assembleFree_flavorDebugUnitTest,
 :module-b:testClasses]

I found a workaround for my problem, though:

Add the following code to module-b build.gradle:

task testClasses {
    doLast {
        println 'This is a dummy testClasses task'
    }
}
like image 162
Sébastien Avatar answered Sep 29 '22 20:09

Sébastien