Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Studio Build Error - "Failed to resolve: monitor"

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'
}
like image 375
ethan Avatar asked Dec 24 '22 03:12

ethan


2 Answers

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.

like image 137
Son Truong Avatar answered Dec 25 '22 22:12

Son Truong


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.

like image 38
Abhishek Luthra Avatar answered Dec 26 '22 00:12

Abhishek Luthra