Android: Skip Gradle “testClasses” task for a dependency project

 ̄綄美尐妖づ 提交于 2019-12-31 18:57:07

问题


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)


回答1:


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'
    }
}


来源:https://stackoverflow.com/questions/33132996/android-skip-gradle-testclasses-task-for-a-dependency-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!