Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Wear: how to share code between the wearable and handheld modules?

I am making an Android app with Wear capabilities.

I want to share some code between the wearable and handheld modules. Specifically, I want to share communication code that uses Google Play Services classes, e.g. 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 the handheld and the wearable modules.

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

I was not sure what to put for the version number - the official documentation here says to use 5.0.77, but that does not work, as 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 app does not work, with this error: 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 of depending on com.google.android.gms:play-services the common module could depend on com.google.android.gms:play-services-wearable but then there is a conflict when building because the handheld module depends on com.google.android.gms:play-services, and these two artefacts use the same package name (com.google.android.gms), and so the gradle build fails.

What's the solution?

.

EDIT after discussing a bit and to make my question clearer.

To be able to use communication APIs in my common module I have two choices:

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

⇒ Solution 1 does not work because the version available (5.0.89) for development is more recent than the one on the watch (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.

like image 917
BoD Avatar asked Aug 16 '14 20:08

BoD


2 Answers

I bumped into the same problem a few days ago. My shared module depended on com.google.android.gms:play-services as well, so Gradle refused to build and kept nagging at me:

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

I added this line to my mobile project's gradle file and the error disappeared magically:

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

like image 174
Kent Avatar answered Oct 27 '22 16:10

Kent


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

I had created common project that is shared between mobile and wear, and contains my constants. Remember to set there dummy manifest file and:

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

I had also encountered problem with play-services version - I had solved it by using

compile 'com.google.android.gms:play-services-wearable:+' compile 'com.google.android.support:wearable:+' instead of specifying particular version - to be honest it should be separete question - as it is out of scope of previous (sharing code between projects).

It is possible to need invalidate cache/restart after changing - you can/should remove build paths in your projects to get rid of all other versions.

like image 28
Michał Tajchert Avatar answered Oct 27 '22 16:10

Michał Tajchert