首页 > 解决方案 > 在 Android Studio 上设置 Firebase

问题描述

我正在尝试在我的 android studio 上设置 firebase。我使用的是 2.2.3 版本。所以这是我在 app.gradle 文件中的代码:

   apply plugin: 'com.android.application'

    android {
        compileSdkVersion 25
        buildToolsVersion "25.0.2"
        defaultConfig {
            applicationId "com.example.parth.carpool"
            minSdkVersion 22
            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-a`enter code here`nnotations'
        })
        compile 'com.android.support:appcompat-v7:25.2.0'
        compile 'com.google.firebase:firebase-core:16.0.1'
        testCompile 'junit:junit:4.12'
    }
    apply plugin: 'com.google.gms.google-services'

这是我收到的错误。

Error:(27, 13) Failed to resolve: com.google.firebase:firebase-core:16.0.1

这是我的 project.gradle 文件:

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

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

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

allprojects {
    repositories {
        jcenter()
    }
}

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

为了纠正这个错误,我在看到 stackoverflow 上的一些帖子后安装了谷歌播放服务、谷歌存储库、SDK 平台、SDK 平台工具等。有人有解决方案吗?

标签: androidfirebase

解决方案


首先你需要将android studio升级到最新版本:

如果您没有使用 Android Studio 3.1 来开发您的应用程序,则需要升级才能在 IDE 中获得正确的版本检查行为。

https://android-developers.googleblog.com/2018/05/announcing-new-sdk-versioning.html

然后您需要使用以下顶级 gradle 文件:

buildscript {
 repositories {
      google()
      jcenter()
  }



dependencies {
 classpath 'com.android.tools.build:gradle:3.1.3'
 classpath 'com.google.gms:google-services:4.0.1'
 }
}

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

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

推荐阅读