3

I need to create an Android Library that uses google's gcm and location services. Initially I did it inside the application's module and everything went fine.

Now I need to create an Android Library to be used by more than one app, and I'm having a lot of problems:

The first one is that my lib doesn't have a google-services.json file, since it should use the app's module to configure itself. So google play services plugin is not working on the libs' build.gradle file.

The second one is that it seems that google play services plugin detects that I'm not calling apply plugin inside the lib's build.gradle and uses by default the version 9.0.0 of the library, even when I'm configuring it to use 10.0.1. So I'm also having a strange problem to run my app, Unable to execute dex: Multiple dex files define ...

What's the right way to create an Android library that uses google-play-services which can also coexist with an Android Application that also uses google-play-services?

Kayan Almeida
  • 611
  • 8
  • 15

4 Answers4

1

First of all in your library, in the build.gradle you have to remove the line

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

Since you can't have a google-services.json file, you can't use the plugin.
You have to add the dependencies needed, and it is enough to compile to library.

Instead in the projects which will use the library, you have to add the plugin and the google-services.json file.

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
0

I have faced a similar situation and concluded that:

1- Android Library should have it

google-services.json

file and should also declare dependencies and google services plugin.

2- App using the library can have their own

google-services.json

file and dependencies declared.

The point to be noted here is that we should use the same versions of dependencies and google services plugin.

Also while adding library dependency,

transitive = true

is added.

Nargis
  • 4,687
  • 1
  • 28
  • 45
  • But why should I have this file? If you take a look at Intercom Android Sdk install, they do not use a new googleservices.json. the transitive makes sense, will try that! – Kayan Almeida Jan 24 '17 at 12:06
0

I found in the internet that this error of yours is usually caused by multiple copies of library(duplicate) that you are using in your project. Go into the Project Structure -> modules, then check in the target or the apk-libs folder if there are more than one copy. After this, do a clean and build the project.

For more solution about this issue, check this related SO question.

Now, for your question about creating an Android library that uses google-play-services, try to check this tutorial if it can help you. It provides screenshot that you can follow.

For more information, check these threads:

Community
  • 1
  • 1
KENdi
  • 7,576
  • 2
  • 16
  • 31
-1

Even after invalidating caches and restarting Android Studio. The only thing that solved my problem was restarting the computer. Don't know how, but suddenly everything was correct again.

Kayan Almeida
  • 611
  • 8
  • 15