Android Wear:如何在可穿戴模块和手持模块之间共享代码?

BoD*_*BoD 5 android android-gradle-plugin wear-os

我正在制作一款具有Wear功能的Android应用.

我想在可穿戴模块和手持模块之间共享一些代码.具体来说,我想分享使用Google Play服务类的通信代码,例如com.google.android.gms.common.api.GoogleApiClient.

显而易见的方法是使用一个模块(我称之为common)并在手持设备和可穿戴模块中添加一个依赖项.

由于这个通用模块使用Play服务,我需要依赖它com.google.android.gms:play-services.

我不知道该为版本号添加什么 - 这里的官方文档说要使用5.0.77,但这不起作用,因为最新的SDK在任何地方都没有这个版本,而是带有5.0.895.2.08.

如果我使用5.0.89,可穿戴应用程序不起作用,出现此错误:Google Play services out of date. Requires 5089000 but found 5077534.手表上的版本比我用来编译的版本旧.

而不是依赖于com.google.android.gms:play-services公共模块可能依赖于com.google.android.gms:play-services-wearable但是然后在构建时存在冲突,因为手持模块依赖于com.google.android.gms:play-services,并且这两个人工制品使用相同的包名称(com.google.android.gms),因此gradle构建失败.

解决方案是什么?

.

在讨论之后编辑并使我的问题更清楚.

为了能够在我的common模块中使用通信API,我有两个选择:

  1. 使common依赖于com.google.android.gms:play-services
  2. 使common依赖于com.google.android.gms:play-services-wear

⇒解决方案1不起作用,因为用于开发的可用版本(5.0.89)比手表上的版本(5.0.77)更新.

⇒解决方案2不起作用,因为handheld模块已经依赖com.google.android.gms:play-services,与之冲突com.google.android.gms:play-services-wear.

小智 5

几天前我碰到了同样的问题.我的共享模块也依赖于com.google.android.gms:play-services,所以Gradle拒绝建立并不停地唠叨我:

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

我将此行添加到我的移动项目的gradle文件中,错误消失了:

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