【问题标题】:Failed to resolve 'com.google.firebase:firebase-messaging:11.0.4' in Android Studio 2.3.3无法在 Android Studio 2.3.3 中解析“com.google.firebase:firebase-messaging:11.0.4”
【发布时间】:2018-01-18 21:51:57
【问题描述】:

我想在 Android Studio 上设置一个 Firebase 云消息传递客户端应用程序, 我正在使用最新版本的 Android Studio(2.3.3),并且我的 android SDK 已完全更新。

这是我的项目 gradle 文件:

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

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

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

这是我的应用构建 gradle 文件:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.3"
    defaultConfig {
        applicationId "ir.digiwindow.driver.gps_status"
        minSdkVersion 14
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })

    compile 'com.android.support:appcompat-v7:25.3.1'
    compile 'com.android.support.constraint:constraint-layout:1.0.0-alpha7'
    testCompile 'junit:junit:4.12'
    compile 'com.google.firebase:firebase-messaging:11.0.4'


}
apply plugin: 'com.google.gms.google-services'

我总是给出这个错误:

Error:(30, 13) Failed to resolve: compile 'com.google.firebase:firebase-messaging:11.0.4'

这里有趣的一点是,当我将版本从 11.0.4 更改为 10.0.1 时,没有发生错误并且 gradle build 成功! 我阅读了stackoverflow和谷歌开发者网站中的所有相关帖子,但这个错误仍然存​​在! 欢迎任何想法...

编辑 1: 当我去寻找更新时,我在图片中看不到任何关于更新 google play 服务的选项:

【问题讨论】:

  • 确保您在 Android SDK 管理器中拥有最新的 Google 存储库
  • 在 Android SDK manager 中,Google Repository 版本为 51。

标签: android firebase gradle firebase-cloud-messaging android-studio-2.3


【解决方案1】:

我也遇到了同样的问题,我通过更新两件事解决了这个问题

  1. 谷歌播放服务
  2. 谷歌存储库

【讨论】:

  • 没错,我必须更新 Google Play 服务和 Google 存储库,这解决了我的问题
【解决方案2】:

Jan 先生,请点击下面的链接,它清楚地说明了->

https://firebase.google.com/docs/android/setup#Prerequisites

先决条件-

  • 运行 Android 4.0(冰淇淋三明治)或更高版本以及 Google Play 服务 11.6.0 或更高版本的设备
  • Google 存储库中的 Google Play 服务 SDK,可在 Android SDK 管理器中获得
  • Android Studio 的最新版本,1.5 版或更高版本

所以在 SDK 管理器中升级您的 Google Play 服务和 Google 存储库

打开 Android Studio > 转到文件 > 设置 > 外观和行为 > 系统设置 > Android SDK > SDK 工具 > 更新

1- Google Play 服务和

2- 支持存储库

下的 Google 存储库

【讨论】:

    【解决方案3】:

    正如 Dileep 在文档中提到的,您需要安装 Google Play Services 11.0.4 或更高版本。

    只需打开 SDK 管理器,
    然后是 Android SDK(在左侧菜单上)
    然后到 SDK 工具选项卡并更新当前版本 43 (2017-08-10) 的 Google Play 服务。

    这为我解决了。

    【讨论】:

    • 感谢您的回答,我安装了 Google Play Services 版本 43,但仍然存在错误
    【解决方案4】:

    尝试通过 firebase Assitance 添加Firebase 功能,依次转到工具和 Firebase,然后选择要添加的功能,例如实时数据库。 它会自动将编译依赖项添加到您的项目中。我做了同样的事情并解决了我的问题。

    【讨论】:

      【解决方案5】:

      应该是这样的:

      dependencies {
          classpath 'com.android.tools.build:gradle:3.1.1'
      
          classpath 'com.google.gms:google-services:3.1.1' // ADD this
      }
      

      应用程序:(11.8.0)

      implementation 'com.google.firebase:firebase-messaging:11.8.0' // ADD this
      

      【讨论】:

        【解决方案6】:

        如果上述任何解决方案不起作用,Un Tick Offline Gradle Build 将解决此问题

        【讨论】:

          【解决方案7】:

          截至 2020 年 1 月 10 日,这对我有用。

          顶级build.gradle文件:

          buildscript {
              repositories {
                  google()
                  jcenter()
              }
              dependencies {
                  classpath "com.android.tools.build:gradle:4.1.1"
                  classpath 'com.google.gms:google-services:4.3.4'
              }
          }    
          allprojects {
              repositories {
                  google()
                  jcenter()
              }
          }
          task clean(type: Delete) {
              delete rootProject.buildDir
          }
          

          应用级build.gradle文件:

          plugins {
              id 'com.android.application'
              id 'com.google.gms.google-services'
          }
          //Top code is same as= apply plugin: 'com.android.application'
          
          android {
              compileSdkVersion 30
              buildToolsVersion "30.0.0"
          
              defaultConfig {
                  applicationId "com.gc.pushnotificationssampleapp2"
                  minSdkVersion 24
                  targetSdkVersion 30
                  versionCode 1
                  versionName "1.0"
          
                  testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
              }
          
              buildTypes {
                  release {
                      minifyEnabled false
                      proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
                  }
              }
              compileOptions {
                  sourceCompatibility JavaVersion.VERSION_1_8
                  targetCompatibility JavaVersion.VERSION_1_8
              }
          }
          
          dependencies {
              implementation 'androidx.appcompat:appcompat:1.2.0'
              implementation 'com.google.android.material:material:1.2.1'
              implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
              testImplementation 'junit:junit:4.+'
              androidTestImplementation 'androidx.test.ext:junit:1.1.2'
              androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
          
              //Start of unique libs for this project:
              implementation 'com.google.firebase:firebase-messaging:20.0.1'
          }
          

          确保您的应用已在 Firebase 中注册。因此,您的项目中应该有一个 google-services.json 文件,其中包含项目的包名称,如下所示:

          "client_info": {
            "mobilesdk_app_id": "1:551460864994:android:c0310a960a3afb06df2fbb",
            "android_client_info": {
              "package_name": "com.gc.pushnotificationssampleapp2"
            }
          

          AndroidManifest.xml 需要:

          <uses-permission android:name="android.permission.INTERNET"></uses-permission>
          . . . 
          <service
              android:name=".FirebaseMessagingService">
              <intent-filter>
                  <action android:name="com.google.firebase.MESSAGING_EVENT"/>
              </intent-filter>
          </service>
          

          【讨论】:

            【解决方案8】:

            Gradle 版本、firebase services 版本和 google-play-services 版本在使用时要一致。例如,您不能将 gradle 2.x 与 firebase core 16.x 一起使用。

            【讨论】:

              【解决方案9】:

              解决办法是更新到最新版本的 com.google.gms:google-services 的类路径

              Android Studio 在项目的 build.gradle 文件中提示

              【讨论】:

                【解决方案10】:

                在gradle中添加

                 Dependencies{
                    
                     // for the Firebase
                        implementation platform('com.google.firebase:firebase-bom:26.5.0')
                        implementation 'com.google.firebase:firebase-core:16.0.1'
                        implementation 'com.google.firebase:firebase-messaging:20.1.3'
                         implementation 'com.google.firebase:firebase-analytics:17.2.3'
                    
                    
                    
                    }
                    
                    apply plugin: 'com.google.gms.google-services'
                

                并添加清单

                 <application>
                
                     <service android:name=".util.FirebaseMessageReceiver"
                            android:enabled="true"
                            android:exported="true">
                            <intent-filter>
                                <action android:name="com.google.firebase.MESSAGING_EVENT" />
                            </intent-filter>
                        </service>
                
                
                    </application>
                

                和 FirebaseMessageReceiver.java

                这是自定义布局

                    package com.---------.app.util;
                
                import android.app.NotificationChannel;
                import android.app.NotificationManager;
                import android.app.PendingIntent;
                import android.content.Context;
                import android.content.Intent;
                import android.os.Build;
                import android.widget.RemoteViews;
                
                import androidx.core.app.NotificationCompat;
                
                import com.google.firebase.messaging.FirebaseMessagingService;
                import com.google.firebase.messaging.RemoteMessage;
                import com.pisel.app.MainActivity;
                import com.pisel.app.R;
                
                public class FirebaseMessageReceiver
                        extends FirebaseMessagingService {
                
                    // Override onMessageReceived() method to extract the
                    // title and
                    // body from the message passed in FCM
                    @Override
                    public void
                    onMessageReceived(RemoteMessage remoteMessage) {
                        // First case when notifications are received via
                        // data event
                        // Here, 'title' and 'message' are the assumed names
                        // of JSON
                        // attributes. Since here we do not have any data
                        // payload, This section is commented out. It is
                        // here only for reference purposes.
                        /*if(remoteMessage.getData().size()>0){
                            showNotification(remoteMessage.getData().get("title"),
                                          remoteMessage.getData().get("message"));
                        }*/
                
                        // Second case when notification payload is
                        // received.
                        if (remoteMessage.getNotification() != null) {
                            // Since the notification is received directly from
                            // FCM, the title and the body can be fetched
                            // directly as below.
                            showNotification(
                                    remoteMessage.getNotification().getTitle(),
                                    remoteMessage.getNotification().getBody());
                        }
                    }
                
                    // Method to get the custom Design for the display of
                    // notification.
                    private RemoteViews getCustomDesign(String title,
                                                        String message) {
                        RemoteViews remoteViews = new RemoteViews(
                                getApplicationContext().getPackageName(),
                                R.layout.notification);
                        remoteViews.setTextViewText(R.id.title, title);
                        remoteViews.setTextViewText(R.id.message, message);
                        remoteViews.setImageViewResource(R.id.icon,
                                R.drawable.flag_belarus);
                        return remoteViews;
                    }
                
                    // Method to display the notifications
                    public void showNotification(String title,
                                                 String message) {
                        // Pass the intent to switch to the MainActivity
                        Intent intent
                                = new Intent(this, MainActivity.class);
                        // Assign channel ID
                        String channel_id = "notification_channel";
                        // Here FLAG_ACTIVITY_CLEAR_TOP flag is set to clear
                        // the activities present in the activity stack,
                        // on the top of the Activity that is to be launched
                        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        // Pass the intent to PendingIntent to start the
                        // next Activity
                        PendingIntent pendingIntent
                                = PendingIntent.getActivity(
                                this, 0, intent,
                                PendingIntent.FLAG_ONE_SHOT);
                
                        // Create a Builder object using NotificationCompat
                        // class. This will allow control over all the flags
                        NotificationCompat.Builder builder
                                = new NotificationCompat
                                .Builder(getApplicationContext(),
                                channel_id)
                                .setSmallIcon(R.drawable.flag_india)
                                .setAutoCancel(true)
                                .setVibrate(new long[]{1000, 1000, 1000,
                                        1000, 1000})
                                .setOnlyAlertOnce(true)
                                .setContentIntent(pendingIntent);
                
                        // A customized design for the notification can be
                        // set only for Android versions 4.1 and above. Thus
                        // condition for the same is checked here.
                        if (Build.VERSION.SDK_INT
                                >= Build.VERSION_CODES.JELLY_BEAN) {
                            builder = builder.setContent(
                                    getCustomDesign(title, message));
                        } // If Android Version is lower than Jelly Beans,
                        // customized layout cannot be used and thus the
                        // layout is set as follows
                        else {
                            builder = builder.setContentTitle(title)
                                    .setContentText(message)
                                    .setSmallIcon(R.drawable.flag_british_indian_ocean_territory);
                        }
                        // Create an object of NotificationManager class to
                        // notify the
                        // user of events that happen in the background.
                        NotificationManager notificationManager
                                = (NotificationManager) getSystemService(
                                Context.NOTIFICATION_SERVICE);
                        // Check if the Android Version is greater than Oreo
                        if (Build.VERSION.SDK_INT
                                >= Build.VERSION_CODES.O) {
                            NotificationChannel notificationChannel
                                    = new NotificationChannel(
                                    channel_id, "web_app",
                                    NotificationManager.IMPORTANCE_HIGH);
                            notificationManager.createNotificationChannel(
                                    notificationChannel);
                        }
                
                        notificationManager.notify(0, builder.build());
                    }
                }
                

                为通知设计创建布局

                  <!-- Parent Layout of ImageView -->
                    <LinearLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content">
                
                        <!--Image to be displayed beside the notification text-->
                        <ImageView
                            android:id="@+id/icon"
                            android:layout_width="50dp"
                            android:layout_height="50dp"
                            android:padding="5dp"
                            android:src="@drawable/flag_benin" />
                    </LinearLayout>
                
                    <!-- Parent layout for holding the Title and the Body-->
                    <LinearLayout
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:orientation="vertical"
                        android:padding="5dp">
                
                        <!-- TextView for Title -->
                        <TextView
                            android:id="@+id/title"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:text="Title"
                            android:textColor="#000"
                            android:textStyle="bold" />
                
                        <!-- TextView for Body -->
                        <TextView
                            android:id="@+id/message"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:text="Message"
                            android:textSize="15sp" />
                
                    </LinearLayout>
                
                </LinearLayout>
                

                【讨论】:

                  【解决方案11】:

                  对于那些使用 firebase bom 的人

                  implementation platform('com.google.firebase:firebase-bom:29.0.1')
                  // Firebase Cloud Messaging (Java)
                      implementation 'com.google.firebase:firebase-messaging'
                  
                      // Firebase Cloud Messaging (Kotlin)
                      implementation 'com.google.firebase:firebase-messaging-ktx'
                  
                      // For an optimal experience using FCM, add the Firebase SDK
                      // for Google Analytics. This is recommended, but not required.
                      implementation 'com.google.firebase:firebase-analytics'
                  

                  【讨论】:

                    【解决方案12】:

                    我遇到了同样的问题。后来发现我完全从 firebase 文档中复制了该行。

                    类路径'com.google.gms:google-services:3.1.1'

                    注意大写“C”。

                    应该是

                    classpath 'com.google.gms:google-services:3.1.1'

                    使用小写“c”。

                    愚蠢的错误,但很容易被忽视。

                    【讨论】:

                      【解决方案13】:

                      我遇到了同样的问题,我只是将版本从 11.0.4 更改为 10.0.1 并且错误消失了。试试吧,它可能会有所帮助。

                      【讨论】:

                        猜你喜欢
                        • 1970-01-01
                        • 1970-01-01
                        • 1970-01-01
                        • 1970-01-01
                        • 2019-03-30
                        • 1970-01-01
                        • 1970-01-01
                        • 2023-04-10
                        • 1970-01-01
                        相关资源
                        最近更新 更多