在使用Gradle的IntelliJ Idea中未能解析com.google.android.gms:play-services。

85

我正在尝试将Google Play服务添加到我的libGDX项目中,使用IntelliJ Idea进行开发。我已经按照这里的设置指南进行操作:https://developers.google.com/android/guides/setup

看起来很简单。我只是在对应部分的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'
    }
}

然后我尝试在Idea中同步我的gradle项目,结果出现了“无法解析”的错误。

好吧,设置指南也说“确保每次更新Google Play服务时更新此版本号”,但问题是似乎很难找到该版本号:根据Android SDK管理器,我的Google Play Services SDK版本为“43”,到目前为止,我一直无法将这种“11.2.0”或其他典型的版本字符串与“43”版本号相对应。而设置指南并没有提及这方面的内容。

总之,我尝试了许多与此相关的问题,但都没有成功。特别要指出的是,我已经正确更新了我的Android SDK,并且确定它正在被Idea使用(我已经三重检查过了...):

Android SDK Manager Project Structure

我正在使用API级别26,但无论如何,其他定义都在使用相同的Android SDK目录。此外,在这台笔记本电脑上我没有安装任何其他android SDK,因此不存在Idea使用其他SDK的问题。

欢迎任何想法。


2
我看不到 apply plugin: 'com.google.gms.google-services' - Tim
我在“设置Google Play服务”指南中也没有看到它。那是从哪里来的? - Fran Marzoa
顺道提一下,将版本号换成11.0.0似乎解决了问题,但我还在这里进行一些测试,因此在确定之前不会发布它作为答案。 - Fran Marzoa
@Darush,这个包含了关于如何解决基于libGDX的项目问题的具体信息,无论是在问题还是答案中,而其他内容则没有。 - Fran Marzoa
1
@Darush,在libGDX项目中,build.gradle文件的结构与标准Android Studio项目中的不同。特别是我回答中提到的“allprojects”和“project(:android)”部分在后者中不存在,而在前者中放置这些内容是正确的位置。它还提供了其他一般信息,这些信息在其他答案中缺失(例如整个答案所基于的更改日志文档),因此尽管这两个解决方案都源自同一个问题,但我认为它们并不完全相同。 - Fran Marzoa
显示剩余3条评论
14个回答

1
我也遇到了这个错误,但原因不同。事实证明,当我试图将版本号指定为变量时,我打错了一个字母:
dependencies {
    // ...
    implementation "com.google.android.gms:play-services-location:{$playServices}"
    // ...
}

我在项目根目录的gradle.properties文件中定义了变量playServices:

playServices=15.0.1

这个错别字是{$playServices},应该是像这样的${playServices}
dependencies {
    // ...
    implementation "com.google.android.gms:play-services-location:${playServices}"
    // ...
}

那对我来说解决了问题。

0

步骤1: 打开android文件夹

步骤2: 打开gradlew.bat文件

步骤3: 搜索这个(可能是第74行) @rem Execute Gradle "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%

步骤4: 在末尾添加 --offline,如下所示

@rem Execute Gradle "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% --offline


0

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


0

这可能是因为您输入了错误的依赖版本。 您可以从以下选择正确的依赖项:

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

如果您找不到所需的内容,您应该通过以下方式更新您的SDK:

工具> Android> SDK管理器> SDK更新>选择任何需要的内容>确认


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