23

I have been tryong to add FCM to my android application for almost 48 hours now. I am keep on failing with these two errors on Android Studio. I have tried all the solutions in this post. Still I couldn't fix it.

I want firebase cloud messaging to be integrated in my app. So I did the followings.

  • Created an app in the firebase console
  • Included the .json config file
  • Added the SDK
  • implementation 'com.google.firebase:firebase-messaging:17.0.0' (In the app level gradle)
  • apply plugin: 'com.google.gms.google-services' (In the app level gradle file last line)
  • classpath 'com.google.gms:google-services:4.0.1' (In the project level gradle)
  • Added the repository google() (In the project level gradle)

I have tried syncing many times and I keep on getting the error

Failed to resolve: play-services-tasks Open File

Failed to resolve: play-services-basement Open File

On a side note, I am using the gradle plugin 3.1.3

    classpath 'com.android.tools.build:gradle:3.1.3'

Can someone please give me a hand here please.

ReyAnthonyRenacia
  • 17,219
  • 5
  • 37
  • 56
Fawzan
  • 4,738
  • 8
  • 41
  • 85
  • 1
    It would be easier if you post the whole build.gradle file so others could try and copy-paste it on their side and see the errors themselves. – AL. Jun 20 '18 at 13:48
  • 3
    Could you try adding the google() repository as the first option in your dependencies blocks? – Arthur Thompson Jun 20 '18 at 14:58
  • @ArthurThompson it worked. May I kbow the reason for this please? – Fawzan Jun 20 '18 at 15:27
  • 1
    I believe there was an issue with the dependencies pushed/synced to jcenter and others. This will likely be resolved in a future push but for now using the google() repo first will allow all Firebase dependencies to resolve correctly. – Arthur Thompson Jun 21 '18 at 14:45
  • Alright. I can accept an answer or can delete the post. But I think it should stay in SO. – Fawzan Jun 21 '18 at 15:00

5 Answers5

81

In gradle (project), just change the position of google() before jcenter(), and the error is gone.

repositories {
    google()
    jcenter()
}
Mike Yan
  • 1,669
  • 2
  • 21
  • 27
  • 2
    It worked like a charm! Could you explain the reason? Just curiosity. – Óscar Aug 10 '18 at 09:00
  • 2
    jcenter() need to be the last repositories, seem that jcenter() are using some of the repositories in google(). Putting jcenter() first will cause some repositories fail to load – Mike Yan Oct 30 '18 at 02:46
20

jcenter() sometimes act as a mirror repository for some Google dependencies. During Gradle building process, it looks for dependencies in the first entry listed in your repositories {...} block. So if this repository is broken or something bad occurs with any dependency the process will fail.

Here there is a recommended order for repository list

    repositories {
        google()
        maven {
            url 'https://maven.google.com/'
        }
        jcenter()
    }
Armando
  • 583
  • 2
  • 7
  • 16
11

Put google() repository at the very first line of dependencies. It will work.

Samarakande
  • 193
  • 2
  • 3
1

Add in your app.gradle

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

and upgrade your dependencies version.

0

I had this occurring due to outdated build tools version. Updating from 28.0.2 to 28.0.3 resolved it.

buildToolsVersion '28.0.2'

to:

buildToolsVersion '28.0.3'

in build.gradle file.

CamHart
  • 3,825
  • 7
  • 33
  • 69