谷歌服务插件无法检测到com.google.android.gms或com.google.firebase的任何版本 - 奇怪的行为

7

我有一个包含4个模块的项目:

  • app(主要模块)
  • common-lib
  • C
  • D

我已经正确地将Firebase设置为状态,详见此链接:https://firebase.google.com/docs/android/setup

在我的app模块中,我没有使用任何额外的库,只有模块依赖关系:

dependencies {
    debugCompile project(path: ':common-lib', configuration: 'debug')
    releaseCompile project(path: ':common-lib', configuration: 'release')
}

在我的common-lib模块中,我使用Firebase库:
dependencies {
    (...)
    compile 'com.google.firebase:firebase-core:11.2.0'
    compile 'com.google.firebase:firebase-crash:11.2.0'
    compile 'com.google.firebase:firebase-messaging:11.2.0'
    compile 'com.google.firebase:firebase-config:11.2.0'
    compile 'com.google.firebase:firebase-ads:11.2.0'
}

在这种情况下,项目编译成功,但我收到了以下消息:
google-services plugin could not detect any version for com.google.android.gms or com.google.firebase, default version: 9.0.0 will be used.
please apply google-services plugin at the bottom of the build file.

当我将common-lib firebase依赖项复制到我的应用程序模块时,有趣的是这条消息消失了。

这是一个错误吗?我设置错了吗?我的应用输出文件是否包含适当的11.2.0版本的firebase库,还是像消息所说的9.0.0版本?


已编辑

项目build.gradle:

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

allprojects {
    repositories {
        jcenter()
        maven {
            url 'https://maven.google.com'
            // Alternative URL is 'https://dl.google.com/dl/android/maven2/'
        }
        maven { url 'https://jitpack.io' }
    }
}

app模块build.gradle文件

apply plugin: 'com.android.application'

android {
    (...)
}


dependencies {

    debugCompile project(path: ':common-lib', configuration: 'debug')
    debugTestCompile project(path: ':common-lib', configuration: 'debugTest')
    releaseCompile project(path: ':common-lib', configuration: 'release')

}

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

common-lib模块的build.gradle文件

apply plugin: 'com.android.library'


android {
    (...)
}
dependencies {
    (...)
    //firebase
    compile 'com.google.firebase:firebase-core:11.2.0'
    compile 'com.google.firebase:firebase-crash:11.2.0'
    compile 'com.google.firebase:firebase-messaging:11.2.0'
    compile 'com.google.firebase:firebase-config:11.2.0'
    compile 'com.google.firebase:firebase-ads:11.2.0'
}

哦,我认为你应该把它放在common-lib上。或者任何实际上具有Firebase依赖项的库中。 - OneCricketeer
当我在我的common-lib中添加“apply plugin:'com.google.gms.google-services'”时,我遇到了错误:“无法获取类型为com.android.build.gradle.LibraryExtension的对象的未知属性'LibraryVariants'。” - AppiDevo
@AppiDevo,你尝试过使用gradle app:dependencies命令查看gradle依赖树吗? - Satish Sojitra
有没有找到为什么会出现这个问题的原因?我也遇到了同样的问题,不得不将 Firebase 添加到应用程序的依赖项中以解决此问题。过去我不需要这样做,但只是在升级 Gradle 等时出现了这个问题。 - WillEllis
2个回答

0
你尝试过在gradle文件的根级别中添加以下内容吗?

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


是的,我在应用程序模块 build.gradle 文件的底部有这个(其中我没有 firebase 依赖项)。 - AppiDevo
请将您的Gradle文件粘贴在这里以确保正确。 - Vyacheslav
然后,在您的模块 Gradle 文件中(通常是 app/build.gradle),在文件底部添加 apply plugin 行以启用 Gradle 插件: - Vyacheslav
它应该放在 app/build.gradle 文件中,而不是根目录下的文件 @Appi - OneCricketeer
@Vyacheslav 我编辑了帖子,并包含了我的 build.gradle 文件。 - AppiDevo
@cricket_007 是的,我把它放在我的 app/build.gradle 文件中,而不是项目根目录中。 - AppiDevo

0
我通过在我的app(主)模块依赖项中添加解决了这个问题:
implementation "com.google.firebase:firebase-core:$firebase_version"
implementation "com.android.support:appcompat-v7:$support_version"

现在我的appcommon-lib(库)模块中都有上述依赖项。 我还添加了$firebase_version$support_version,以便维护更加容易。变量放置在项目的gradle.build中:

buildscript {

    ext {
        firebase_version = '11.8.0'
        support_version = '26.1.0'
    }
    (...)
}

问题出现是因为google-services插件检查依赖项时在应用gradle文件中找不到它。但即使您通过将implementation更改为api来明确公开它,它也不会检查库依赖项:

common-lib gradle.build:

api "com.google.firebase:firebase-core:$firebase_version"
api "com.android.support:appcompat-v7:$support_version"

它还是不起作用。



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