Android Studio构建错误 - “无法解决:monitor”

4
我之前有个项目正在运行,版本已经升级到了最新。现在由于下面的构建错误,无法构建该项目。

无法解析:monitor
打开文件

“打开文件”链接指向build.gradle(模块)文件。我尝试过使用“使无效并重新启动”功能,但没有帮助。
在“无法解析:monitor”或“无法解析:”下搜索未得出任何解决方案。
我现在完全不知所措了。
模块: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'
}

尝试在移除androidTestImplementation 'com.android.support.test:runner:1.0.2'并增加最小sdk版本后进行构建。 - singh.indolia
@singh.indolia Son Truong提供的解决方案有效,但您能否提供更多信息,说明为什么您认为'com.android.support.test:runner:1.0.2'和mind sdk版本可能是一个问题?这将有助于我解决未来的构建错误。 - ethan
可能是重复的问题:无法解决:recyclerview-v7 - Radesh
@Radesh 在发布这个问题之前,我尝试了彻底的搜索解决方案。也许是因为我在 Gradle 构建故障排除方面缺乏知识,否则我永远不会找到你提到的帖子。 - ethan
2个回答

8

我昨天遇到了这个问题,每当我从Git仓库检出新分支或在Android Studio中创建新项目时。

根本原因:当我尝试构建应用程序时,Android Studio显示此错误。

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

在浏览器中打开https://jcenter.bintray.com/com/android/support/test/monitor/1.0.2/monitor-1.0.2.jar时,出现了以下错误。

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

我的推测是jcenter仓库中的monitor依赖项已被移除(可能是暂时的)。因此,构建过程失败了。所以我想出了两个解决方案: 解决方案1:进入build.gradle(Project)并更改仓库的顺序,这样gradle构建系统将首先在google仓库中找到monitor依赖项。幸运的是,该依赖项目前可在该仓库中使用。
allprojects {
    repositories {
        jcenter()
        google()
    }
}

to

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

解决方案 2:进入 build.gradle (Module:App),注释或删除这些行。这意味着我们的应用程序不需要 Android 测试支持库,该库为测试 Android 应用程序提供了广泛的框架。
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'
}

然后点击立即同步


令人惊讶的是,这个解决方案有效。我也感到惊讶和印象深刻,不知道怎么会有这样的解决方案。对我来说,这毫无意义。 - ethan
这个解决方案的原理是什么,为什么它能够起作用? - Srikar Reddy
1
根本原因在此回答中有解释: https://dev59.com/Pa_la4cB1Zd3GeqPqkrE#52982816 在你的情况下, monitor-1.0.2.jar 不再托管在 jcenter 上,但 pom.xml 仍然存在!(请参见 https://jcenter.bintray.com/com/android/support/test/monitor/1.0.2/:monitor-1.0.2.pom) - M.Ricciuti
@M.Ricciuti 感谢您的信息,我的假设是正确的。一些依赖关系已经从 jcenter 中删除,原因不明。如果我们能找到任何来自 jcenter 的官方消息,那就更好了。 - Son Truong

2
问题似乎出在jcenter上。我已经花了几个小时来解决这个问题,你的问题似乎与我的类似,我认为以下解决方案应该有效。
由于某种原因,在jcenter上许多库的pom文件被保留,但相应的aar文件已被删除。play-services-basement库也是如此。请参考以下内容(play-services-basement的pom文件可在jcentre的此处找到,但aar文件不可在jcentre的此处找到):
解决方案: 在项目级别的gradle文件中,更改以下代码块。
allprojects {
    repositories {
        jcenter()
        google()
    }
}

为了

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

为什么这个有效?

在我们的第一个代码块中,当gradle尝试在存储库中解决依赖项(在我的情况下,它是jcentre存储库中的google-services-basement),由于相应的aar文件已被删除,因此无法解决。结果,构建失败并显示以下内容:

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

在我们的第二个代码块中,google库已经在jcenter存储库之前被引用。当gradle构建开始时,它首先查找在repositories{...中列出的库以解决项目中使用的任何库。现在,当gradle尝试在jcenter中解决play-services-basement时,由于之前引用了google库(最新版本的相同aar文件不在jcenter存储库中),因此成功解析依赖项并提供相应的aar文件。请检查并让我知道是否有效。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接