# 解决 Google Play Services 冲突

不同框架的不同插件可能会使用不同版本的 google-play-services 库，这可能导致您的项目因库版本过时而无法编译或崩溃。

以下部分为不同插件提供了解决方案。

## 库位置

您可以在 `<Android SDK>/extras/google` 路径下找到 Google Play Services 库。

该库有两种格式可用：

1. **单体 jar 文件**（在单个文件中包含所有 Google Play Services 组件）：
   ```
   sdk/extras/google/google_play_services/libproject/google-play-services_lib/libs/google-play-services.jar
   ```

2. **模块化 aar 文件**（Android Archive），其中每个组件都在一个单独的文件中：
   ```
   sdk/extras/google/m2repository/com/google/android/gms/…
   ```

一个 **aar** 文件可以通过解压（aar 是一个 zip 压缩包）并提取其中的 **classes.jar**（库的代码）来精简为一个 **jar** 文件（目前并非所有框架都支持 aar）。

### 1. Cordova

在 Cordova 上，所有 google play services 冲突都通过 gradle 自动解决。有时（例如，不同插件使用不同主版本的库），解决过程可能会失败，导致最终构建中出现同一库的两个不同版本。

### 2. Unity、Appcelerator Titanium

您可以在插件（或示例项目）中找到 Google Play Services 库。它们易于更新、更改或移除以解决可能的冲突：

*   Unity: [https://github.com/Pushwoosh/pushwoosh-unity/tree/master/PushwooshUnitySample/Assets/Plugins/Android](https://github.com/Pushwoosh/pushwoosh-unity/tree/master/PushwooshUnitySample/Assets/Plugins/Android)
*   Unity 和 Google Analytics 插件：Google Analytics 插件使用了一个过时的 google play 库，需要进行更新。更多信息请参见：[https://github.com/Pushwoosh/pushwoosh-unity/issues/5](https://github.com/Pushwoosh/pushwoosh-unity/issues/5)

```bash
rm -rf Assets/Plugins/Android/google-play-services_lib/libs
rm -rf Assets/Plugins/Android/google-play-services_lib/res
cp ${ANDROID_HOME}/extras/google/m2repository/com/google/android/gms/play-services-analytics/8.4.0/play-services-analytics-8.4.0.aar Assets/Plugins/Android/
cp ${ANDROID_HOME}/extras/google/m2repository/com/google/android/gms/play-services-ads/8.4.0/play-services-ads-8.4.0.aar Assets/Plugins/Android/
```

*   Appcelerator Titanium（请注意，模块版本可能比本文档更新得更快！）：[https://github.com/Pushwoosh/pushwoosh-appcelerator-titanium/tree/master/Module-Source/android/src/com/pushwoosh/module](https://github.com/Pushwoosh/pushwoosh-appcelerator-titanium/tree/master/Module-Source/android/src/com/pushwoosh/module)

### 3. Adobe Air

获取 ANE 扩展，将其解压、修改，然后重新打包。

使用以下 **PushNotification.ane** 的示例：

```bash
unzip PushNotification.ane -d tmp
```

此命令会将 .ane 文件的内容提取到 `tmp` 目录中。

现在，在 _tmp/META-INF/ANE/Android-ARM/_ 和 _tmp/META-INF/ANE/Android-x86/_ 目录中，您可以找到所有可能已被修改、更新或移除的库：android-support_、play-services_ 等。

如果您仍然遵循上述文档中的链接，请更改 `tmp/META-INF/ANE/Android-ARM/platform.xml` 和 `tmp/META-INF/ANE/Android-x86/platform.xml`，然后将扩展重新压缩，一切应该就能正常工作。

如果您遇到以下错误：

> PushNotification.ane is not a valid native extension file.

请按照以下步骤重新打包扩展：

1.  删除 `tmp/META-INF/ANE/Android-ARM/platform.xml` 和 `tmp/META-INF/ANE/Android-x86/platform.xml`
2.  根据上述链接中的指南，改为修改 `tmp/META-INF/ANE/Android-ARM/android-depends.xml` 和 `tmp/META-INF/ANE/Android-x86/android-depends.xml`
3.  运行以下 bash 命令：

```bash
#rename platform.xml on iOS, required to repackage the extension
mv tmp/META-INF/ANE/iPhone-ARM/platform.xml tmp/META-INF/ANE/iPhone-ARM/platform-iOS.xml 

#pack the extension back with adt
cd tmp/META-INF/ANE/
“${flex.sdk}/bin/adt" -package -target ane PushNotification.ane extension.xml -swc ../../../../SWC-Pushwoosh/PushNotification.swc -platform iPhone-ARM -platformoptions iPhone-ARM/platform-iOS.xml -C iPhone-ARM/ . -platform Android-ARM -platformoptions Android-ARM/android-depends.xml -C Android-ARM/ . -platform Android-x86 -C Android-x86/ . -platformoptions Android-x86/android-depends.xml -platform default -C default/ .
```

您可以在这里找到 PushNotification.swc：[https://github.com/Pushwoosh/pushwoosh-adobe-air/blob/master/SWC-Pushwoosh/PushNotification.swc](https://github.com/Pushwoosh/pushwoosh-adobe-air/blob/master/SWC-Pushwoosh/PushNotification.swc)

### 4. Unreal Engine

Unreal Engine 和 Pushwoosh Plugin 几乎总是会相互冲突。这是因为 Unreal Engine 默认包含 Google Play services，并且不同版本的引擎使用不同版本的这些库。Pushwoosh 插件也使用这些库，但使用的是不同的子集。在集成用于 Unreal Engine 的 Pushwoosh Plugin 时务必小心。即使您的项目看起来编译和运行良好，也要确保 Pushwoosh 和 Unreal Engine 使用相同版本的 Google Play Services。在同一个项目中使用不同版本的 google-play-services 会导致未定义的行为。

要解决冲突，您应执行以下操作：

**1.** 确定项目中已在使用哪些 Google Play services 库。检查 `${UNREAL_PROJECT}/Intermediate/Android/APK/JavaLibs` 和 `${UNREAL_PROJECT}/Intermediate/Android/APK/libs` 目录。例如，在使用 Unreal Engine 4.15 创建的[示例项目](https://github.com/Pushwoosh/pushwoosh-unreal-engine)中，有以下库：

JavaLibs:

*   downloader_library
*   play-services-ads-lite-9.2.0
*   play-services-auth-base-9.2.0
*   play-services-basement-9.2.0
*   play-services-drive-9.2.0
*   play-services-gass-9.2.0
*   play-services-plus-9.2.0
*   support-v4-23.0.0
*   play-services-ads-9.2.0
*   play-services-auth-9.2.0
*   play-services-base-9.2.0
*   play-services-clearcut-9.2.0
*   play-services-games-9.2.0
*   play-services-nearby-9.2.0
*   play-services-tasks-9.2.0
*   pushwoosh-ue-plugin // 由 Pushwoosh 插件添加

libs:

*   SystemUtils.jar
*   VrApi.jar
*   play-services-gcm-9.2.0.jar // 由 Pushwoosh 插件添加
*   play-services-iid-9.2.0.jar // 由 Pushwoosh 插件添加

**2.** 如果 Pushwoosh 和 Unreal Engine 中使用的 play-services* 库的版本不匹配，请从 [Pushwoosh 插件](https://github.com/Pushwoosh/pushwoosh-unreal-engine/tree/master/Plugins/Pushwoosh/lib/Android/JavaLibs/pushwoosh-ue-plugin)中移除这些库。

**3.** 确定应向 Pushwoosh Plugin 添加哪些缺失的库。该插件需要 android-support-v4、play-services-base、play-services-basement、play-services-gcm 和 play-services-iid 库。对于示例项目，只有 **play-services-gcm** 和 **play-services-iid** 不重叠，因此在这种特定情况下，它们是唯一需要添加的库。

**4.** 通过将 Android SDK 中的 .jar 文件放置在 [https://github.com/Pushwoosh/pushwoosh-unreal-engine/tree/master/Plugins/Pushwoosh/lib/Android/libs](https://github.com/Pushwoosh/pushwoosh-unreal-engine/tree/master/Plugins/Pushwoosh/lib/Android/libs) 中来添加库。Google Play 库的位置在本文开头有描述。

## 与我们分享您的反馈

您的反馈有助于我们创造更好的体验。如果您在 SDK 集成过程中遇到任何困难，请[通过此表单](https://docs.google.com/forms/d/e/1FAIpQLSd_0b8jwn-V_JmoPLIxIFYbHACCQhrzidOZV3ELywoQPXRSxw/viewform)与我们分享您的想法。