Java – Gradle searches for IVY services but does not define IVY configuration/repo

Gradle searches for IVY services but does not define IVY configuration/repo… here is a solution to the problem.

Gradle searches for IVY services but does not define IVY configuration/repo

I’m getting the following error from Gradle

Could not resolve all dependencies for configuration ':unitTestCompile'.
> Could not find junit:junit:4.11.

Here is my build.gradle

apply plugin: 'android-library'

buildscript {
    repositories {
        mavenCentral()
    }

dependencies {
        classpath 'com.android.tools.build:gradle:0.8.+'
    }
}

version = '0.6.2'

configurations {
        unitTestCompile.extendsFrom runtime
        unitTestRuntime.extendsFrom unitTestCompile
}

dependencies {
    compile files('libs/android-support-v4.jar')
    unitTestCompile 'junit:junit:4.11'
}

android {
    buildToolsVersion "19.0.1"
    compileSdkVersion 17

sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src', 'bundled-src']
            resources.srcDirs = ['src']
            aidl.srcDirs = ['src']
            renderscript.srcDirs = ['src']
            res.srcDirs = ['res']
            assets.srcDirs = ['assets']
        }
    }

dependencies {
        compile fileTree(dir: './libs', include: 'Amplitude.jar')
    }
}

sourceSets {
        unitTest {
                java.srcDirs = ['tests/src']
                res.srcDirs = ['tests/res']
                assets.srcDirs = ['tests/assets']
                resources.srcDirs = ['tests/res']
        }
}
task doUnitTest(type:Test, dependsOn: assemble) {
        description = "run unit tests"
        testClassesDir = project.sourceSets.unitTest.output.classesDir
        classpath = project.sourceSets.unitTest.runtimeClasspath
}

check.dependsOn doUnitTest

A debug run indicates that it is trying to resolve the dependency with Ivy and not with Maven?


16:21:58.633 [DEBUG] [org.gradle.api.internal.artifacts.ivyservice.resolveengine.DependencyGraphBuilder] Visiting configuration :analytics-android:0.6.2(unitTestCompile).
16:21:58.633 [DEBUG] [org.gradle.api.internal.artifacts.ivyservice.resolveengine.DependencyGraphBuilder] Visiting dependency :analytics-android:0.6.2(unitTestCompile) -> junit:junit:4.11(dependency: junit#junit; 4.11 {unitTestCompile=[default]})
16:21:58.634 [DEBUG] [org.gradle.api.internal.artifacts.ivyservice.resolveengine.DependencyGraphBuilder] Selecting new module version junit:junit:4.11
16:21:58.634 [DEBUG] [org.gradle.api.internal.artifacts.ivyservice.ivyresolve.UserResolverChain] Attempting to resolve module 'junit:junit:4.11' using repositories []
16:21:58.636 [DEBUG] [org.gradle.api.internal.artifacts.ivyservice.resolveengine.DependencyGraphBuilder] Attaching :analytics-android:0.6.2(unitTestCompile) to its parents.
16:21:58.637 [DEBUG] [org.gradle.api.internal.artifacts.ivyservice.resolveengine.oldresult.TransientConfigurationResultsBuilder] Flushing resolved configuration data in Binary store in /private/var/folders/7y/yrtq1vcx3xb901v_xd5v5_100000gn/T/gradle1413382664789288993.bin. Wrote root :analytics-android:0.6.2:unitTestCompile.
16:21:58.638 [DEBUG] [org.gradle.api.internal.tasks.execution.ExecuteAtMostOnceTaskExecuter] Finished executing task ':compileUnitTestJava'
16:21:58.638 [INFO] [org.gradle.execution.taskgraph.AbstractTaskPlanExecutor] :compileUnitTestJava (Thread[main,5,main]) completed. Took 0.017 secs.
16:21:58.639 [DEBUG] [org.gradle.execution.taskgraph.AbstractTaskPlanExecutor] Task worker [Thread[main,5,main]] finished, busy: 4.035 secs, idle: 0.047 secs
16:21:58.644 [ERROR] [org.gradle.BuildExceptionReporter] 
16:21:58.645 [ERROR] [org.gradle.BuildExceptionReporter] FAILURE: Build failed with an exception.
16:21:58.645 [ERROR] [org.gradle.BuildExceptionReporter] 
16:21:58.646 [ERROR] [org.gradle.BuildExceptionReporter] * What went wrong:
16:21:58.646 [ERROR] [org.gradle.BuildExceptionReporter] Could not resolve all dependencies for configuration ':unitTestCompile'.
16:21:58.646 [ERROR] [org.gradle.BuildExceptionReporter] > Could not find junit:junit:4.11.
16:21:58.646 [ERROR] [org.gradle.BuildExceptionReporter]   Required by:
16:21:58.647 [ERROR] [org.gradle.BuildExceptionReporter]       :analytics-android:0.6.2

Solution

The problem is that you only declare a repository for build script dependencies and not one for regular dependencies. (In other words, you need a top-level repositories block.) Also, dependencies blocks should always be outside of android blocks, as it’s a core Gradle feature that has nothing to do with Android plugins.

Related Problems and Solutions