4

I had a previously running project, where the versions were upgraded to the latest. Now I cannot build the project due to below build error.

Failed to resolve: monitor
Open File

The "Open File" link is to the build.gradle (Module) file. I tried "Invalidate and Restart" which does not help.

Searching under "Failed to resolve: monitor" or "Failed to resolve:" did not result in any solutions.

I am completely stumped at this point.

Module: build.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 28
    buildToolsVersion '28.0.3'

    defaultConfig {
        applicationId "com.example.android.sunshine"
        minSdkVersion 14
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
        }
    }
    productFlavors {
    }
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'com.android.support:appcompat-v7:28.0.0'

    implementation 'com.android.support:recyclerview-v7:28.0.0'
    implementation 'com.android.support:preference-v7:28.0.0'

    androidTestImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support:support-annotations:28.0.0'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test:rules:1.0.2'
}
ethan
  • 346
  • 3
  • 16
  • Try to build after remove androidTestImplementation 'com.android.support.test:runner:1.0.2' and increase your min sdk version – singh.indolia Oct 25 '18 at 04:23
  • @singh.indolia solution provided by Son Truong works, but could you provide additional information as to why you feel 'com.android.support.test:runner:1.0.2' and mind sdk version may be an issue? It will help me to troubleshoot future build errors. – ethan Oct 25 '18 at 05:18
  • Possible duplicate of [Failed to resolve: recyclerview-v7](https://stackoverflow.com/questions/50891617/failed-to-resolve-recyclerview-v7) – Radesh Oct 27 '18 at 07:45
  • @Radesh I attempted a thorough search for a solution before posting this question. Perhaps because I lack knowledge in gradle build troubleshooting, I never would have found the post you mentioned. – ethan Oct 27 '18 at 22:02

2 Answers2

8

I had encountered this problem yesterday whenever I checkout a new branch from a Git repo or create a new project in Android Studio.

Root cause: When I try to build application, the Android Studio show this error

Could not find monitor.jar (com.android.support.test:monitor:1.0.2).
Searched in the following locations:
    https://jcenter.bintray.com/com/android/support/test/monitor/1.0.2/monitor-1.0.2.jar

Opening https://jcenter.bintray.com/com/android/support/test/monitor/1.0.2/monitor-1.0.2.jar on a browser I got this error.

{
  "errors" : [ {
    "status" : 404,
    "message" : "Could not find resource"
  } ]
}

My assumption is the monitor dependency has been removed (might be temporarily) from jcenter repo. As a result the build process failed. So I figure out 2 solutions:

Solution 1: Go to build.gradle (Project) change the order of repos, so gradle build system will find monitor dependency in google repo first. Fortunately, this dependency is available in the repo (at least so far).

allprojects {
    repositories {
        jcenter()
        google()
    }
}

to

allprojects {
    repositories {
        google()
        jcenter()
    }
}

Solution 2: Go to build.gradle (Module:App) comment-out or delete these lines. It mean our app do not need the Android Testing Support Library which provides an extensive framework for testing Android apps.

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'com.android.support:appcompat-v7:28.0.0'

    implementation 'com.android.support:recyclerview-v7:28.0.0'
    implementation 'com.android.support:preference-v7:28.0.0'

    androidTestImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support:support-annotations:28.0.0'

    // Comment-out or delete these lines
//    androidTestImplementation 'com.android.support.test:runner:1.0.2'
//    androidTestImplementation 'com.android.support.test:rules:1.0.2'
}

Then hit Sync Now.

Son Truong
  • 13,661
  • 5
  • 32
  • 58
  • Surprisingly this solution works. I am also dumbfounded and impressed at how one arrives at a solution like this. To me it makes no sense. – ethan Oct 25 '18 at 04:34
  • What is the rationale and why does this solution work? – Srikar Reddy Oct 25 '18 at 07:49
  • 1
    the root cause is explained in this answer: https://stackoverflow.com/a/52982816/6899896 in your case , the `monitor-1.0.2.jar` is not hosted in jcenter anymore, but the `pom.xml` still is! (see https://jcenter.bintray.com/com/android/support/test/monitor/1.0.2/:monitor-1.0.2.pom ) – M.Ricciuti Oct 25 '18 at 08:32
  • @M.Ricciuti Thanks for you info, so my assumption is correct. some of dependencies has been removed from `jcenter` with unknown reason. It's better if we find any official from `jcenter` why they do that. – Son Truong Oct 25 '18 at 08:48
2

The problem seems to be with jcenter. I have spent hours together with this problem and your problem seems to be similar to mine and I think the following solution should work.

For some reason and for many libraries in jcenter, the pom files of many libraries are kept in place but corresponding aar files have been removed. This is also the case with play-services-basement library. Check the following here for reference ( pom file of play-services-basement is available at jcentre here but aar file is not available at jcentre here):

Solution : In your project level gradle file , change the following block of code

allprojects {
    repositories {
        jcenter()
        google()
    }
}

to

allprojects {
    repositories {
        google()
        jcenter()
    }
}

why this works ?

In our first code block, when gradle tries to resolve a dependency in the repository(in my case,it was google-services-basement in jcentre repository), it ddi not get resolved as corresponding aar files has been removed. As a result , build fails with something like :

 Could not find play-services-basement.aar (com.google.android.gms:play-services-basement:15.0.1).

In our second code block, google repository has been referenced before jcenter repository. When gradle build starts, it looks first in the libraries listed first in repositories{... for resolving any library that is used in the project. Now, when gradle tries to resolve play-services-basement in jcenter, it is successful in resolving dependency as corresponding aar file has been made available by google repository(the same aar file of latest version is not available in jcenter repository) which has been referenced before jcenter repository is assessed. Do check and let me know if that works.

Abhishek Luthra
  • 2,575
  • 1
  • 18
  • 34