简体   繁体   English

Android Studio:尝试在 build.gradle 中实现 OKHTTP3 时收到错误

[英]Android Studio: I am receiving an error when trying to implement OKHTTP3 into the build.gradle

I'm getting the following error in android studio when I add OKHTTP3 to the app level build.gradle and attempt to sync the build.gradle:当我将 OKHTTP3 添加到应用级别 build.gradle 并尝试同步 build.gradle 时,我在 android studio 中收到以下错误:

ERROR: Unable to resolve dependency for ':app@debug/compileClasspath': Could not resolve com.squareup.okhttp3:okhttp:4.4.0.

My code for implementing the dependency looks like this:我用于实现依赖项的代码如下所示:

dependencies {
    ...
    implementation 'com.squareup.okhttp3:okhttp:4.4.0'
    ...
}

This is the latest version of OKHTTP3 that I took from https://search.maven.org/artifact/com.squareup.okhttp3/okhttp/4.4.0/jar so this should work fine.这是我从https://search.maven.org/artifact/com.squareup.okhttp3/okhttp/4.4.0/jar获取的最新版本的 OKHTTP3,所以这应该可以正常工作。

In the project level build.gradle I have jcenter() included within the repositories at both the buildscript and allprojects levels so I am unsure why I am receiving the above error as, from other stackoverflow questions, I can see that jcenter() is the maven central repository which this library comes from.在项目级别的 build.gradle 中,我在buildscriptallprojects级别的存储库中都包含了jcenter() ,所以我不确定为什么会收到上述错误,因为从其他 stackoverflow 问题中,我可以看到jcenter()是这个库来自的 maven 中央存储库。

I have looked at all other stackoverflow questions on this matter but none of their solutions are working for me and I am receiving the same error regardless of any attempts.我已经查看了关于这个问题的所有其他 stackoverflow 问题,但他们的解决方案都没有为我工作,无论进行任何尝试,我都会收到相同的错误。

I appreciate any suggestion anyone may have.我感谢任何人可能提出的任何建议。 Thank you in advance.先感谢您。

For anyone who runs into the above problem.对于遇到上述问题的任何人。 This is what worked for me:这对我有用:

I did some research into the new Android Studio version as I figured out that I had done everything else correctly and the error was most likely to do with the recent Android Studio update.我对新的 Android Studio 版本进行了一些研究,因为我发现我已经正确完成了所有其他操作,并且错误很可能与最近的 Android Studio 更新有关。 To fix the above solution, I toggled the build.gradle offline mode to off which resolved the entire problem.为了解决上述解决方案,我将build.gradle离线模式切换为关闭,从而解决了整个问题。

To perform the same steps, this is taken from Android Studio's 3.6 blog post:要执行相同的步骤,这取自 Android Studio 的 3.6 博客文章:

New location to toggle Gradle's offline mode To enable or disable Gradle's offline mode, first select View > Tool Windows > Gradle from the menu bar.切换 Gradle 离线模式的新位置 要启用或禁用 Gradle 离线模式,首先从菜单栏中选择查看 > 工具窗口 > Gradle。 Then, near the top of the Gradle window, click Toggle Offline Mode Gradle offline button in the Gradle panel..然后,在 Gradle 窗口顶部附近,单击 Gradle 面板中的 Toggle Offline Mode Gradle offline 按钮。

This post can be found here https://developer.android.com/studio/preview/features#gradle-offline-ui这篇文章可以在这里找到https://developer.android.com/studio/preview/features#gradle-offline-ui

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM