Java 无法解决:com.google.android.gms:play-services in IntelliJ Idea with gradle

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45692460/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 02:07:50  来源:igfitidea点击:

Failed to resolve: com.google.android.gms:play-services in IntelliJ Idea with gradle

javaandroidintellij-ideagradlelibgdx

提问by Fran Marzoa

I'm trying to add google play services to my libGDX project in IntelliJ Idea. I've followed the setup guide here: https://developers.google.com/android/guides/setup

我正在尝试在 IntelliJ Idea 中将 google play 服务添加到我的 libGDX 项目中。我已按照此处的设置指南进行操作:https: //developers.google.com/android/guides/setup

which looks pretty straightforward. I just added those lines to my build.gradle in the corresponding section, so things look now like:

这看起来很简单。我刚刚将这些行添加到相应部分的 build.gradle 中,所以现在看起来像:

project(":android") {
    apply plugin: "android"
    apply plugin: 'com.android.application'

    configurations { natives }

    dependencies {
        compile project(":core")
        compile "com.badlogicgames.gdx:gdx-backend-android:$gdxVersion"
        natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-armeabi"
        natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-armeabi-v7a"
        natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-arm64-v8a"
        natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-x86"
        natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-x86_64"
        compile "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion"
        natives "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-armeabi"
        natives "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-armeabi-v7a"
        natives "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-x86"
        compile "com.badlogicgames.gdx:gdx-freetype:$gdxVersion"
        natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-armeabi"
        natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-armeabi-v7a"
        natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-arm64-v8a"
        natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-x86"
        natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-x86_64"
        compile 'com.google.android.gms:play-services:11.2.0'
    }
}

Then I try to sync my gradle project in Idea just to get that "Failed to resolve" error.

然后我尝试在 Idea 中同步我的 gradle 项目,只是为了得到“无法解决”错误。

Well, the setup guide also says "Be sure you update this version number each time Google Play services is updated", but the problem is that it seems nearly impossible to find that version number: my Google Play Services SDK version according to the Android SDK manager is "43", and so far I have been unable to correlate such "11.2.0" or whatever typical version string with the "43" version number. Not that the setup guide says nothing about that.

好吧,设置指南还说“每次更新 Google Play 服务时都要确保更新此版本号”,但问题是似乎几乎不可能找到该版本号:根据 Android SDK,我的 Google Play 服务 SDK 版本manager 是“43”,到目前为止我一直无法将这样的“11.2.0”或任何典型的版本字符串与“43”版本号相关联。并不是说设置指南对此一无所知。

Anyway, I have tried a lot of things from the plethora of questions related to this to no avail. Specifically, I have to point out that I do have my Android SDK properly updated and I'm sure it is the one it's being used by Idea (I've already triple-checked this...):

无论如何,我已经尝试了很多与此相关的问题,但无济于事。具体来说,我必须指出我确实正确更新了我的 Android SDK,并且我确定它是 Idea 正在使用的那个(我已经三重检查了这个......):

Android SDK ManagerProject Structure

Android SDK 管理器项目结构

I'm using the API level 26, but anyway the other defines do use the very same directory for the Android SDK. Moreover, I do NOT have any other android SDK installed at all in this laptop, so there's no question about Idea being using that one and that one only.

我正在使用 API 级别 26,但无论如何其他定义确实使用与 Android SDK 完全相同的目录。此外,我在这台笔记本电脑中根本没有安装任何其他 android SDK,所以毫无疑问 Idea 只使用那个和那个。

Any ideas are more than welcome.

任何想法都非常受欢迎。

Thanks in advance!

提前致谢!

采纳答案by Fran Marzoa

I just replaced version 11.2.0 with 11.0.0 and then it seemed to work fine, so that had to mean that 11.2.0 wasn't included with the latest Android SDK.

我只是用 11.0.0 替换了 11.2.0 版本,然后它似乎工作正常,所以这意味着 11.2.0 没有包含在最新的 Android SDK 中。

So, after struggling with all the available scattered documentation, I reached this document by pure chance (I guess it is not indexed high enough by Google): https://developers.google.com/android/guides/releases

因此,在与所有可用的分散文档苦苦挣扎之后,我纯粹是偶然地找到了这个文档(我猜它没有被 Google 索引得足够高):https: //developers.google.com/android/guides/releases

I quote from there:

我从那里引用:

Highlights from the Google Play services 11.2 release. Google Play services dependencies are now available via maven.google.com

Google Play 服务 11.2 版本的亮点。Google Play 服务依赖项现在可通过 maven.google.com 获得

Now, even when that shouldn't necessarily mean that they are not available with the downloaded SDK anymore, it seems that this is actually the case.

现在,即使这不一定意味着它们不再适用于下载的 SDK,但事实似乎确实如此。

Anyway, adding google() to my build.gradle didn't work (not found, undefined, or whatever...), so I used a different approach that I found in this document referenced from the previous one:

无论如何,将 google() 添加到我的 build.gradle 不起作用(未找到、未定义或其他任何……),所以我使用了一种不同的方法,我在本文档中引用了上一个:

https://developer.android.com/studio/build/dependencies.html#google-maven

https://developer.android.com/studio/build/dependencies.html#google-maven

I modified my build.gradle file adding that line to allprojects/repositories, as in:

我修改了我的 build.gradle 文件,将该行添加到 allprojects/repositories,如下所示:

allprojects {
...
    repositories {
...
        maven { url "https://maven.google.com/"}
    }
}

And then also in the android section in the same build.gradle file:

然后也在同一个 build.gradle 文件中的 android 部分:

project(":android") {
...
    dependencies {
...
        compile 'com.google.android.gms:play-services-ads:11.2.0'
    }
}

Those two lines were enough to make Gradle sync without problems. I didn't need to add any plugins apart from the ones that are already added in my libGDX project by default.

这两行足以使 Gradle 同步而不会出现问题。除了默认情况下已添加到我的 libGDX 项目中的插件之外,我不需要添加任何插件。

After that, I got a few different errors, but none about Gradle or dependencies. In a brief, JFTR:

在那之后,我遇到了一些不同的错误,但没有关于 Gradle 或依赖项。简而言之,JFTR:

First, I had a minSdkVersion of 8. Solved by raising it to 14. I think I could live without supporting all those devices below 14.

首先,我的 minSdkVersion 为 8。通过将其提高到 14 来解决。我想我可以不支持所有 14 以下的设备。

Second, I had problems with the dex upper limit of references. I've never faced this problem before, but maybe you've already noticed the solution I used: instead of compiling the whole 'com.google.android.gms:play-services' I used only 'com.google.android.gms:play-services-ads' that's the API I'm actually interested right now. For those other particular cases where a solution like this may not be useful, this document could provide some better insight: https://developer.android.com/studio/build/multidex.html

其次,我在引用的 dex 上限方面遇到了问题。我以前从未遇到过这个问题,但也许您已经注意到我使用的解决方案:我只使用了 com.google.android.gms 而不是编译整个 'com.google.android.gms:play-services' :play-services-ads' 这是我现在真正感兴趣的 API。对于像这样的解决方案可能没有用的其他特定情况,本文档可以提供一些更好的见解:https: //developer.android.com/studio/build/multidex.html

Third, even after that I got this "jumbo" thing problem described and answered here: https://stackoverflow.com/a/26248495/1160360

第三,即使在那之后,我也在这里描述并回答了这个“巨型”问题:https: //stackoverflow.com/a/26248495/1160360

And that's it. As of now, everything builds and my game does finally shows those Admob banners.

就是这样。到目前为止,一切都建立起来了,我的游戏终于显示了那些 Admob 横幅。

I've spent hours with this, thought, which makes me wonder if all these building automation systems we are using lately are worth the extra load they add.

我花了好几个小时思考这个问题,这让我想知道我们最近使用的所有这些楼宇自动化系统是否值得它们增加的额外负载。

I mean, the first time I had to add Admob to an app five years ago or so, I just had to download a .jar file and put it on a directory on my project. It was pretty obvious and the whole process, from googling "how to setup Admob in my android project" to have my app showing an Admob banner took me just a few minutes. I'm gonna leave it here, since this is not the place for such kind of debate.

我的意思是,五年前我第一次不得不将 Admob 添加到应用程序中时,我只需要下载一个 .jar 文件并将其放在我项目的目录中。很明显,整个过程,从谷歌搜索“如何在我的 android 项目中设置 Admob”到让我的应用程序显示 Admob 横幅只花了我几分钟。我要把它留在这里,因为这里不是进行这种辩论的地方。

Nonetheless, I hope my own experience is useful for someone else further.

尽管如此,我希望我自己的经验对其他人有用。

回答by Aryan

Google Play services SDK is inside Google Repository.

Google Play 服务 SDK 位于Google Repository.

  1. Start Intellij IDEA.

  2. On the Tools menu, click Android > SDK Manager.

  3. Update the Android SDK Manager: click SDK Tools, expand Support Repository, select Google Repository, and then click OK.

  1. 启动 Intellij IDEA。

  2. 在工具菜单上,单击 Android > SDK 管理器。

  3. 更新 Android SDK 管理器:单击SDK Tools,展开Support Repository,选择Google Repository,然后单击 OK。

Current Google Repository version is 57.

当前的 Google 存储库版本是 57。

After update sync your project.

更新后同步您的项目。

EDIT

编辑

From version 11.2.0, we've to use the google maven repo so add google maven repo link in repositories tag. Check release note from here.

从 version 开始11.2.0,我们必须使用 google maven repo,因此在 repositories 标签中添加 google maven repo 链接。从这里检查发行说明。

allprojects {
     ..
     repositories {
     ...
        maven {
            url 'https://maven.google.com'
            // Alternative URL is 'https://dl.google.com/dl/android/maven2/'
        }
     }
}

回答by Amirhosein

I had that problem. And I found this solve. In Android Studio, Open File menu, and go to Project Structure, In Module app, go to dependencies tab and you can add 'com.google.android.gms:play-services:x.x.x' by clicking on + button.

我有那个问题。我找到了这个解决方案。在 Android Studio 中,打开文件菜单,然后转到项目结构,在模块应用程序中,转到依赖项选项卡,您可以通过单击 + 按钮添加“com.google.android.gms:play-services:xxx”。

回答by zsubzwary

Add this to your project-level build.gradlefile:

将此添加到您的项目级build.gradle文件中:

repositories {
    maven {
        url "https://maven.google.com"
    }
}

It worked for me

它对我有用

回答by Noureddine AMRI

A more up to date answer:

一个更新的答案:

allprojects {
    repositories {
        google() // add this
    }
}

And don't forget to update gradle to 4.1+ (in gradle-wrapper.properties):

并且不要忘记将 gradle 更新到 4.1+(在 gradle-wrapper.properties 中):

distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-all.zip

source: https://developer.android.com/studio/build/dependencies.html#google-maven

来源:https: //developer.android.com/studio/build/dependencies.html#google-maven

回答by Abolfazl Miadian

this is probably about you don't entered correct dependency version. you can select correct dependency from this:

这可能是关于您没有输入正确的依赖版本。您可以从中选择正确的依赖项:

file>menu>project structure>app>dependencies>+>Library Dependency>select any thing you need > OK

if cannot find your needs you should update your sdk from below way:

如果找不到您的需求,您应该从以下方式更新您的 SDK:

tools>android>sdk manager>sdk update>select any thing you need>ok

工具>android>sdk manager>sdk update>选择你需要的任何东西>ok

回答by Alen Lee

At the latest Google Play Services 15.0.0, it occurs this error when you include entire play service like this

最迟Google Play Services 15.0.0,当您像这样包含整个播放服务时,会发生此错误

implementation 'com.google.android.gms:play‐services:15.0.0'

Instead, you must specific the detail service like Google Drive

相反,您必须指定详细服务,例如 Google Drive

com.google.android.gms:play-services-drive:15.0.0

回答by MJahongir

Check you gradle settings, it may be set to Offline Work

检查你的gradle设置,它可能被设置为离线工作

回答by Moussa

I had the issue when I put jcenter()before google()in project level build.gradle. When I changed the order and put google()before jcenter()in build.gradle the problem disappeared

jcenter()之前google()在项目级别 build.gradle 中放入时遇到了问题。当我更改顺序并放在build.gradlegoogle()之前jcenter(),问题就消失了

Here is my final build.gradle

这是我的最终 build.gradle

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {

    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.1.3'


        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

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

task clean(type: Delete) {
    delete rootProject.buildDir
}

回答by user3318703

I have found following solution to replace following code

我找到了以下解决方案来替换以下代码

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {

    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.1.3'
        classpath 'com.google.gms:google-services:3.2.0'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

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

task clean(type: Delete) {
    delete rootProject.buildDir
}

It's work fine for me

这对我来说很好用