在库模块中使用Google Play Services Gradle插件

时间:2019-06-12 12:17:13

标签: android gradle module google-play-services

我正在将单片应用程序重构为多模块应用程序,而我在使用Google Play服务gradle插件时遇到了困难。我使用了一些Play服务功能,例如Maps或Google Login,需要一些api密钥或客户端ID,这些ID或客户端ID由GPS gradle插件方便地生成到android资源,并且可以在我的代码中使用它们。但是,如果我把这条神奇的线

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

在我的base模块build.gradle的底部,我得到了隐秘的错误


FAILURE: Build failed with an exception.

* What went wrong:
A problem occurred configuring project ':base'.
> java.lang.NullPointerException

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

CONFIGURE FAILED in 0s
Cause: java.lang.NullPointerException

我已经阅读了一些线程,必须将此插件应用于顶层应用程序模块。但是,如何在功能/库模块中访问该插件提供的生成的资源?

1 个答案:

答案 0 :(得分:0)

在您的图书馆模块中,您应该添加Google Play服务依赖项:

   dependencies {
           api 'com.google.android.gms:play-services-auth:16.0.1'
           api 'com.google.android.gms:play-services-games:17.0.0'
        }

在此之后,您可以在应用程序模块中添加依赖项:

 dependencies {
       api project(':yourlibrary')
    }

在应用模块的底部添加插件

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

确保settings.gradle也有您的媒体库:

include ':app', ':yourlibrary'

还要在您的项目级别gradle中添加依赖项

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

确保您拥有google-services.json 在app /目录中(位于Android Studio应用模块的根目录)

我认为这应该可行。通过这种方式,您可以从您的图书馆模块中访问应用程序中的Google Play服务。

还要添加

dependencies {
   api project(':yourlibrary')
}

任何其他需要播放服务的模块(如果它们不依赖于应用模块)。

如果这不起作用,您能向我简要介绍一下您的用例吗?