Android Wear: how to share code between portable and portable modules?

I am creating an Android app with Wear capabilities.

I want to share some code between portable and portable modules. In particular, I want to share a communication code that uses Google Play Services classes, for example. com.google.android.gms.common.api.GoogleApiClient .

The obvious way to do this is to have a module (I called it common ) and add a dependency to it in both portable and wearable modules.

Since this common module uses Play Services, I need to depend on com.google.android.gms:play-services .

I was not sure what to put the version number - the official documentation here says to use 5.0.77 , but this does not work, since the latest SDK does not have this version anywhere, instead it comes with 5.0.89 and 5.2.08 .

If I use 5.0.89 , the Wearable application does not work, with this error: Google Play services out of date. Requires 5089000 but found 5077534 Google Play services out of date. Requires 5089000 but found 5077534 . The version on the watch is older than the one I used to compile.

Instead, depending on com.google.android.gms:play-services general module could depend on com.google.android.gms:play-services-wearable , but then there is a conflict when creating, because the pocket module depends on com.google.android.gms:play-services , and these two artifacts use the same package name ( com.google.android.gms ), and therefore the gradle build fails.

What solution?

.

EDIT after discussion a bit and make my question more clear.

To be able to use the communication APIs in my common module, I have two options:

  • Make common depend on com.google.android.gms:play-services
  • Make common depend on com.google.android.gms:play-services-wear

β‡’ Solution 1 does not work, because the version (5.0.89) for development is later than the version on the clock (5.0.77).

β‡’ Solution 2 does not work because the handheld module already depends on com.google.android.gms:play-services , which conflicts with com.google.android.gms:play-services-wear .

+5
source share
2 answers

I ran into the same problem a few days ago. My general module depended on com.google.android.gms:play-services , so Gradle refused to build and kept grumbling:

Error: more than one library with package name 'com.google.android.gms

I added this line to my Gradle mobile project and the error disappeared magically:

compile(project(':sharedModule')) { transitive = false }

+5
source

Take a look here: https://github.com/tajchert/SWear_Weather

I created a generic project that is divided between mobile and wear, and contains my constants. Remember to install the dummy manifest file there and:

apply plugin: 'com.android.library' in the build.gradle file.

I also had a problem with the play-services version - I solved it with

compile 'com.google.android.gms:play-services-wearable:+' compile 'com.google.android.support:wearable:+' instead of specifying a specific version - to be honest, this should be a secret question - because it goes beyond the previous one (exchange code between projects).

After the change, an invalid cache / restart may be required - you can / must remove the build paths in your projects in order to get rid of all other versions.

+3
source

Source: https://habr.com/ru/post/1200486/


All Articles