Google Play Services:本质、组件和 Android 开发

作者: IT Sectr 发布日期: 2026-04-30 阅读时间: 11 分钟

Google Play Services — 是 Android 上的一层 Google 系统服务,确保所有 Google 应用和 API 的正常运行:从 Google Maps 到 Firebase 和 Google Sign-In。Play Services 作为独立的 APK 包运行,通过 Google Play Store 自动更新,独立于 Android 固件更新。根据 Android Developers, 2025 的数据,Google Play Services 安装在 99.8% 的活跃 Android 设备上,包含超过 50 个独立模块。

要点

  • Google Play Services — 用于 Android 的 Google 服务系统层,独立于操作系统更新
  • 自动更新 — Play Services 通过 Play Store 更新,独立于制造商的固件
  • 模块 — 超过 50 个组件:Maps、Auth、Ads、Wallet、Location、SafetyNet 等
  • API 级别 — Play Services 从 Android 2.3(API 9)开始可用,大多数模块从 API 14+ 开始
  • Google Mobile Services — GMS 包包括 Play Services 以及 Google 应用(Chrome、Gmail、YouTube)

什么是 Google Play Services?

Google Play Services — 是在 Android 操作系统之上运行的 Google 专有服务层。与仅包含基本库的 AOSP(Android 开源项目)不同,Play Services 提供与 Google 生态系统集成的 API:地图、地理位置、认证、推送通知、广告和支付。Play Services 作为具有更高权限的系统应用安装,可以访问普通应用无法访问的 API。

Google Play Services 与标准 Android 库的关键区别 — 通过 Play Store 更新的能力。如果 Google 发布新版本的 Maps SDK 或 Auth API,用户将通过 Play Services 收到更新,无需等待制造商的 OTA 更新。这解决了 Android 碎片化问题 — 根据 Statista(2025) 的数据,约 40% 的 Android 设备的操作系统版本超过 3 年,但 85% 的设备上的 Play Services 已更新到最新版本。

Google Play Services 不是 Android 开源项目的一部分,在没有 Google 许可证的设备上不可用(例如 2019 年后的华为)。对于在无 GMS 设备上运行的应用,Google 建议使用具有跨平台支持的 Firebase SDK,或转向替代解决方案(华为移动服务)。

Play Services 的架构和组件

Google Play Services 的架构构建为一组独立模块(APK 包),每个模块负责其自身功能。主 APK(com.google.android.gms)包含服务核心和约 50 个按需加载的附加模块。用户进程称为 Google Play Services 进程,在后台以高优先级运行。

每个 Google Play Services 模块都有自己的版本和 API。开发人员只通过 build.gradle 连接必要的模块,这减少了应用的大小。例如,Google Sign-In 需要 com.google.android.gms:play-services-auth,Google Maps 需要 play-services-maps。Google Play Services 自动解决模块间的依赖关系并加载缺失的组件。

组件Gradle 包功能
Authplay-services-authGoogle Sign-In、Credential Manager、ID Token
Mapsplay-services-mapsGoogle Maps SDK 地图渲染、相机
Locationplay-services-locationFusedLocationProvider、地理围栏、Activity Recognition
Adsplay-services-adsGoogle Mobile Ads、AdMob、Ad Manager
Walletplay-services-walletGoogle Pay、Passes、支付 API
SafetyNetplay-services-safetynet设备检查、reCAPTCHA、认证

通过 AIDL 交互

交互 在应用和 Play Services 之间通过 AIDL(Android 接口定义语言)进行。应用调用 SDK 方法,SDK 向 Google Play Services 进程发送 IPC 请求,该进程执行实际工作(向 Google 服务器发送网络请求、处理 GPS、加密)。Play Services 进程与应用隔离 — 如果它崩溃,应用继续运行。

自动更新机制和版本管理

Google Play Services 通过 Google Play Store 自动更新 — 用户在后台收到新版本,无需确认。更新分阶段推出(staged rollout):首先推送到 1% 的设备,然后 10%、50% 和 100%。如果在新版本中发现严重错误,Google 可以在 24 小时内将更新回滚到稳定版本。

Google Play Services 的版本用两个数字编码:APK 版本(例如 25.15.32)和 SDK 版本(例如 12.8.0)。开发人员需要通过 GoogleApiAvailability 检查设备上的 Play Services 版本 — 如果用户关闭了自动更新或使用了自定义固件,版本可能已过时。根据 Google 的数据,活跃设备上 Play Services 的平均版本不超过 6 个月。

kotlin
// 检查设备上的 Google Play Services 版本
val availability = GoogleApiAvailability.getInstance()
val resultCode = availability.isGooglePlayServicesAvailable(context)

when (resultCode) {
    ConnectionResult.SUCCESS ->
        Log.d("GMS", "Google Play Services 可用")

    ConnectionResult.SERVICE_MISSING,
    ConnectionResult.SERVICE_VERSION_UPDATE_REQUIRED -> {
        // 显示更新对话框
        availability.showErrorDialogFragment(
            activity, resultCode, REQUEST_CODE
        )
    }
}

版本问题:如果 Google Play Services 被禁用(用户在设置中手动禁用),所有 Google API 将停止工作。应用应在每次 Google API 调用前检查 Play Services 的可用性,并显示用户可理解的消息,提供进入设置或下载 Play Services 的按钮。Google 通过 showErrorDialogFragment 提供错误对话框,自动引导到 Play Store。

核心模块及其用途

Google Play Services Auth — 认证模块,提供 Google Sign-In、Credential Manager 和密码 Smart Lock。自 2024 年起,Google 推荐将 Credential Manager 作为所有认证类型的统一 API。Google Play Services Maps — Google 地图渲染、地理编码、Places API 和路线。Location — Fused Location Provider,结合 GPS、Wi-Fi 和蜂窝数据,以最低功耗实现精确定位。

Play Integrity API:替代 SafetyNet

Google Play Services Wallet 模块支持 Google Pay 用于应用内和网站支付,以及 Google Passes(会员卡、登机牌、门票)。SafetyNet(正被 Play Integrity API 取代) — 设备完整性检查:root 访问、自定义固件、模拟器。Play Integrity API(自 2024 年起推荐)— 更精确的检查:device integrity(完整性)、app integrity(签名)、account integrity(Google 账户)。

kotlin
// 通过 FusedLocationProvider 请求位置
val fusedClient = LocationServices.getFusedLocationProviderClient(context)

val locationRequest = LocationRequest.Builder()
    .setPriority(Priority.PRIORITY_HIGH_ACCURACY)
    .setInterval(10000)
    .setFastestInterval(5000)
    .build()

if (ActivityCompat.checkSelfPermission(
    context, Manifest.permission.ACCESS_FINE_LOCATION
) == PackageManager.PERMISSION_GRANTED) {
    fusedClient.requestLocationUpdates(
        locationRequest,
        locationCallback,
        Looper.getMainLooper()
    )
}

Google Play Services Awareness 模块 — 组合 API,整合位置、时间、天气、用户活动和连接的耳机数据。Awareness API 允许应用响应上下文:例如,当用户在工作时启用静音模式,或在出门前显示降雨通知。API 从 Play Services 16.0.0 开始可用。

将 Google Play Services 集成到 Android 项目

Google Play Services 集成到 Android 项目通过在应用级别的 build.gradle 中添加依赖来完成。Google 建议使用单独的模块化依赖,而不是通用的 play-services-all 包,以减小 APK 大小。Play Services SDK 的最低版本 — 21.0.0(对应 Android 14),但大多数模块支持 API 19+。

集成需要配置 Google Services Gradle 插件和从 Firebase 控制台下载的 google-services.json 文件。JSON 文件包含项目标识符、API 密钥和用于 OAuth 的客户端 ID。没有 google-services.json,大多数 Play Services 模块无法在 Google 服务器上进行授权。如果项目不使用 Firebase,只需在 AndroidManifest.xml 中添加 API 密钥即可。

groovy
// build.gradle(项目级别)
buildscript {
    dependencies {
        classpath "com.google.gms:google-services:4.4.2"
    }
}

// build.gradle(应用级别)
apply plugin: 'com.google.gms.google-services'

dependencies {
    implementation "com.google.android.gms:play-services-auth:21.2.0"
    implementation "com.google.android.gms:play-services-maps:19.0.0"
    implementation "com.google.android.gms:play-services-location:21.3.0"
}

重要细节:不同的 Google Play Services 模块可能要求不同的版本。如果一个模块需要 play-services-basement 版本 18.0.0,而另一个需要 18.1.0,Gradle 将冲突解决为更高版本。建议通过变量为所有模块使用统一版本:ext.playServicesVersion = '21.2.0'。Google Play Services 还依赖于 compileSdk 版本 — 对于版本 21.0.0,需要 compileSdk 34+。

没有 Google Play Services 的 Android 设备

没有 Google Play Services 的设备(华为、荣耀、一些中国品牌)无法使用 GMS 提供的 API。对于此类设备,Google 推荐使用 Firebase SDK,其中包含无需 Play Services 即可运行的跨平台库。Firebase Authentication 直接使用 REST API,Firebase Realtime Database 使用 WebSocket 连接,Firebase Cloud Messaging 使用自有协议。

替代方法 — 使用 华为移动服务(HMS),它提供类似的 API:华为地图 Kit、位置 Kit、推送 Kit。为了支持两种类型的设备,开发人员实现一个抽象层,在启动时检测 GMS 或 HMS 的存在并连接相应的 SDK。根据 Counterpoint Research(2025),无 GMS 设备的份额约占全球 Android 市场的 5%。

kotlin
// 检查设备上 GMS 的存在
fun isGmsAvailable(): Boolean {
    return try {
        GoogleApiAvailability.getInstance()
            .isGooglePlayServicesAvailable(context) ==
            ConnectionResult.SUCCESS
    } catch (e: Exception) {
        false
    }
}

// 用于 GMS/HMS 的抽象层
interface PushService {
    fun getToken(): String
}

class GmsPushService : PushService {
    override fun getToken() =
        FirebaseMessaging.getInstance().token.await()
}

GMS 和 HMS 的抽象层

对于在无 GMS 设备上运行的应用,测试所有使用 Google API 的功能至关重要。如果找不到服务,Play Services 返回 SERVICE_MISSING 错误。建议优雅降级:如果 Google Sign-In 不可用,提供电子邮件登录;如果 Google 地图不可用,显示带有 Yandex 地图或 OpenStreetMap 的 WebView。华为应用市场发布全球约 15% 的 Android 应用。

代码示例:检查和更新 Play Services

检查 Google Play Services 可用性并处理所有可能状态的完整示例:服务可用、需要更新、服务已禁用、服务缺失。代码使用 GoogleApiAvailability 进行检查,并使用 showErrorDialogFragment 显示标准 Google 对话框并导航到 Play Store。

kotlin
class GmsCheckActivity : AppCompatActivity() {

    companion object {
        private const val REQ_UPDATE = 1001
    }

    fun checkGooglePlayServices() {
        val api = GoogleApiAvailability.getInstance()
        when (api.isGooglePlayServicesAvailable(this)) {
            ConnectionResult.SUCCESS ->
                initializeApp()
            ConnectionResult.SERVICE_DISABLED ->
                showSettingsDialog()
            ConnectionResult.SERVICE_VERSION_UPDATE_REQUIRED ->
                api.showErrorDialogFragment(
                    this, ConnectionResult.SERVICE_VERSION_UPDATE_REQUIRED,
                    REQ_UPDATE
                )
        }
    }

    override fun onActivityResult(request: Int,
            result: Int, data: Intent?) {
        super.onActivityResult(request, result, data)
        if (request == REQ_UPDATE && result == RESULT_OK)
            initializeApp()
    }
}

建议在每次应用启动时检查 Google Play Services 的可用性,而不仅是在首次。用户可能在安装应用后在设置中禁用 Play Services,这将导致所有 Google API 无法工作。对于关键功能(例如通过 Google Pay 支付),检查应该是强制性的 — 没有 Play Services,应用无法处理支付。

迁移到 Play Core 和 Modular Delivery

Play Core — 是用于管理应用模块、运行时更新和下载额外资源的 Google Play Services 库。Play Core 允许实现 Dynamic Delivery — 按需交付应用模块:用户下载基础应用,附加功能(例如管理模块或 Premium 内容)仅在用户首次打开相应屏幕时下载。

通过 Play Core 的模块化交付可将应用的初始安装大小减少 30-50%。这对于网络较慢的市场尤为重要 — 根据 Google Play Console,每 10 MB 会降低 1% 的安装转化率。Play Core SDK 需要 Android 5.0(API 21)和 Google Play Services 21.0.0+。重要提示:模块下载后无法删除,只能更新整个应用。

kotlin
// 通过 Play Core 按需加载模块
val manager = SplitInstallManagerFactory.create(context)
val request = SplitInstallRequest
    .Builder()
    .addModule("premium")
    .build()

manager.startInstall(request)
    .addOnSuccessListener {
        Log.d("Split", "Premium 模块已加载")
    }

迁移到 Play Core 需要更改应用架构:功能被拆分为 Android Studio 中的 on-demand 模块,Gradle 将每个模块构建为独立的 APK。Google Play Store 随后创建 APK 集(Android App Bundle),并仅向用户提供基础 APK。Play Core 还支持应用内更新(in-app updates)— 用户无需进入 Play Store 即可在应用内更新应用。到 2026 年,约 70% 的 Google Play 应用将使用 Android App Bundle 和 Play Core。

常见问题

如果在 Android 上删除 Google Play Services 会发生什么?

如果删除 Google Play Services,所有 Google 服务将停止工作:Google Maps、Google Sign-In、FCM 推送通知、Google Pay、Play Integrity。使用这些 API 的应用将显示错误或崩溃。在大多数设备上,无法通过标准方式删除 Play Services — 只能在设置中禁用。

如何手动更新 Google Play Services?

Google Play Services 通过 Play Store 自动更新。要手动更新:打开 Play Store → 我的应用和游戏 → 找到 Google Play Services → 更新。如果更新不可用,请从 APKMirror 下载最新版本的 APK(仅限有经验的用户)。自动更新通常在发布后 2 周内进行。

Google Play Services 的最低 Android 版本是多少?

最低支持版本是 Android 4.4 KitKat(API 19)。但某些模块(例如 Credential Manager)需要 Android 6.0(API 23)或更高版本。对于现代开发,Google 建议以 Android 14(API 34)为目标并使用 Play Services 版本 21.0.0+。运行 Android 4.4 的过时设备仅接收关键的 Play Services 更新。

可以在没有 Google Play Services 的情况下使用 Firebase 吗?

可以,Firebase SDK 可以在没有 GMS 的设备上无需 Google Play Services 即可运行。Firebase Authentication、Realtime Database、Firestore、Cloud Functions 和 Hosting 不需要 Play Services。Firebase Cloud Messaging(FCM)可以使用直接的 HTTP 协议代替 GMS。但 Firebase Crashlytics 和 Performance Monitoring 需要 Play Services 来收集数据。

Google Play Services 的 APK 有多大?

Google Play Services 的基础 APK 在系统分区中占用约 80-120 MB。附加模块(地图、auth、location)按需加载,可能增加 10-50 MB。相比之下:iOS 中的 Apple Push Notification Service 约占 5 MB。尽管体积较大,Play Services 不影响用户的可用存储空间 — 它们位于系统分区中。

总结

  • Google Play Services — 确保 Maps、Auth、Ads、Location 及其他 API 运行的 Google 服务系统层
  • 自动更新 — Play Services 通过 Play Store 独立于固件更新,解决碎片化问题
  • 模块 — 超过 50 个独立组件,通过单独的 Gradle 依赖连接
  • 检查 — GoogleApiAvailability 可以确定 Play Services 版本并向用户建议更新
  • 无 GMS 设备 — 华为和其他品牌使用 HMS;Firebase SDK 支持无 Play Services 运行
  • Play Core — 用于模块化交付、应用内更新和动态功能加载的库
  • Play Integrity API — 用于检查设备和应用完整性的 SafetyNet 现代替代品

我们将开发一款交钥匙移动应用程序

IT Sectr自2017年以来为初创企业和企业打造iOS和Android应用程序。我们将为您提供咨询并提出最佳解决方案。

讨论项目

另请阅读