85

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

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

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.

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.

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 Manager Project Structure

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.

Any ideas are welcome.

starball
  • 20,030
  • 7
  • 43
  • 238
Fran Marzoa
  • 4,293
  • 1
  • 37
  • 53
  • 2
    I don't see `apply plugin: 'com.google.gms.google-services'` – Tim Aug 15 '17 at 12:10
  • I don't see it either in the Set Up Google Play Services guide. Where does that come from? – Fran Marzoa Aug 15 '17 at 12:38
  • JFTR, replacing the version number with 11.0.0 instead of 11.2.0 seems to do the trick, but I'm still doing some testing here so I won't publish it as an answer until I'm sure about that. – Fran Marzoa Aug 15 '17 at 14:53
  • Possible duplicate of [Error:(23, 13) Failed to resolve: com.google.android.gms:play-services:11.2.0 "Install Repository and sync project" dose not work](https://stackoverflow.com/questions/45688654/error23-13-failed-to-resolve-com-google-android-gmsplay-services11-2-0-i) – Darush Aug 26 '17 at 19:24
  • @Darush this one contains specific information on how to address the problem with libGDX based projects on both, the question and the answer, that's absent on the other. – Fran Marzoa Aug 28 '17 at 12:19
  • @Fran I believe both have the same solution if u look at the answers. – Darush Aug 28 '17 at 12:25
  • 1
    @Darush The build.gradle files on a libGDX project follow a different structure than the ones on an standard Android Studio project. Specifically the sections "allprojects" and "project(:android)" mentioned in my answer do not exist in the last and it is the right place for putting these things on the former. It also provides additional general information that's absent on the other answer (like the changelog document in which the whole answer is founded), so yet when both solutions follow from the same problem, I don't think is a real dupe. – Fran Marzoa Aug 28 '17 at 13:47
  • Another option is tu update the gradle services to the latest version. – Jesua Oct 30 '18 at 19:43

14 Answers14

226

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

Here is my final 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
}
Moussa
  • 4,066
  • 7
  • 32
  • 49
  • 1
    It worked ... But why that issue came in the first place? – other Tall guy Oct 24 '18 at 04:26
  • 1
    @PaulMathew : if I had the explanation of this issue/fix I would have put it on the post. I "discovered" the fix by chance when struggling with this issue and testing a lot of manipulations. – Moussa Oct 24 '18 at 10:09
  • 1
    What kind of sorcery is this? Works like a charm. Maybe something is wrong between these 2 repos... Another one who wants to know the explanation. – Pl4yeR Nov 05 '18 at 13:48
  • One of the reasons could be following: it tries to find following repo on jcenter() first, finds one that is private or that fails for any other reason and returns error to you. Then if you put "google()" on top it first looks on google repo and only then (if nothing is found) it goes to jcenter. – A. Kazarovets Mar 19 '19 at 09:03
  • I miss Eclipse so much, since Android Studio I seems like I spend as much time 'fixing' gradle as I do programming – Martin Belcher - AtWrk May 24 '19 at 14:53
85

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.

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

I quote from there:

Highlights from the Google Play services 11.2 release. Google Play services dependencies are now available via 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.

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:

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

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

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

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

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.

After that, I got a few different errors, but none about Gradle or dependencies. In a brief, 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.

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

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

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

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.

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

Fran Marzoa
  • 4,293
  • 1
  • 37
  • 53
  • 6
    I have a feeling this is going to be well visited answer over the next few weeks. –  Aug 15 '17 at 16:46
  • 4
    This is an excellent answer: fix, documentation, other issues fixes. Just let me add that from the [same linked page](https://developer.android.com/studio/build/dependencies.html#google-maven) they updated `maven { url "https://maven.google.com/"}` to `google()`. The maven line is mandatory for lower Gradle versions [i.e. lower than Gradle 4.1]. Cheers mate! – Antonino Dec 12 '17 at 00:44
  • Do you have any idea why you have to put the repo dependency in the top level `build.gradle` file's `allprojects{ repositories{...}}` instead of in the module specific build file, e.g. `app/build.gradle` 's `repositories`? – stan0 Jul 03 '18 at 14:11
17

Add this to your project-level build.gradle file:

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

It worked for me

zsubzwary
  • 1,196
  • 1
  • 14
  • 22
14

I tried solving this problem for hours after I haven't used Android Studio some time and wasn't aware of the updates.

It is important that google() is the first item that stands in repositories like this:

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

Somehow google() was the second item after jcenter(), so everything was messed up and didn't work. Maybe this helps someone.

AlexioVay
  • 4,338
  • 2
  • 31
  • 49
11

Google Play services SDK is inside 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.

Current Google Repository version is 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.

allprojects {
     ..
     repositories {
     ...
        maven {
            url 'https://maven.google.com'
            // Alternative URL is 'https://dl.google.com/dl/android/maven2/'
        }
     }
}
Abhishek Aryan
  • 19,936
  • 8
  • 46
  • 65
  • Google Repository is already installed. I'm going to add an screenshot with that too. – Fran Marzoa Aug 15 '17 at 13:08
  • maybe you're pointing the wrong SDK. Check it out in File>ProjectStructure and make sure you got the right SDK. – Abhishek Aryan Aug 15 '17 at 13:32
  • Quoting myself from the question: "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...)". I'm going to add another screenshot to avoid further confusion, and so nobody has to take my word on this neither... – Fran Marzoa Aug 15 '17 at 13:41
  • It is not correct. With the 11.2.0 you have to use the google maven repo – Gabriele Mariotti Aug 20 '17 at 15:23
6

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):

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

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

Noureddine AMRI
  • 2,942
  • 1
  • 21
  • 28
4

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

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

Instead, you must specific the detail service like Google Drive

com.google.android.gms:play-services-drive:15.0.0
Alen Lee
  • 2,479
  • 2
  • 21
  • 30
4

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

2

In my case I was using version 17.0.1 .It was showing error.

implementation "com.google.android.gms:play-services-location:17.0.1"

After changing version to 17.0.0, it worked

implementation "com.google.android.gms:play-services-location:17.0.0"

Reason might be I was using maps dependency of version 17.0.0 & location version as 17.0.1.It might have thrown error.So,try to maintain consistency in version numbers.

Tarun Anchala
  • 2,232
  • 16
  • 15
1

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

MJakhongir
  • 2,101
  • 2
  • 16
  • 27
1

I got this error too but for a different reason. It turns out I had made a typo when I tried to specify the version number as a variable:

dependencies {
    // ...
    implementation "com.google.android.gms:play-services-location:{$playServices}"
    // ...
}

I had defined the variable playServices in gradle.properties in my project's root directory:

playServices=15.0.1

The typo was {$playServices} which should have said ${playServices} like this:

dependencies {
    // ...
    implementation "com.google.android.gms:play-services-location:${playServices}"
    // ...
}

That fixed the problem for me.

Michael Osofsky
  • 11,429
  • 16
  • 68
  • 113
0

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.

Amirhosein
  • 11
  • 1
0

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:

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

Abolfazl Miadian
  • 3,099
  • 2
  • 19
  • 15
0

step 1: Open android folder

step 2: open gradlew.bat file

step 3: Search this (possible line no 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%

Step 4: add --offline add the end like

@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

mewadaarvind
  • 349
  • 3
  • 9