You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Ionic项目中持久化修改Google Play服务依赖版本?

解决Ionic/Cordova中GMS依赖版本持久化修改的问题

Got it, this is a super common pain point with Cordova/Ionic since the platforms/ directory is intentionally ignored—you don't want to commit build artifacts after all! Here are the best ways to make this change persistent so every developer on your team gets the correct GMS version automatically:

方法1:通过项目根目录的config.xml配置(推荐)

Cordova allows you to define system library dependencies directly in your config.xml, which will be injected into the platforms/android/project.properties file every time you run ionic cordova prepare or build.

Just add this inside the <platform name="android"> block in your root config.xml:

<platform name="android">
    <!-- 其他安卓配置... -->
    <preference name="cordova.system.library.1" value="com.google.android.gms:play-services-auth:11+" />
</platform>

This will override the default + version wildcard with your fixed 11+ version on every build. Since config.xml is tracked in git, all team members will get this setting automatically when they pull the code.

方法2:通过插件变量指定(如果依赖来自某个Cordova插件)

If the play-services-auth dependency is being added by a specific Cordova plugin (like cordova-plugin-google-plus or a similar auth plugin), you can set the version directly in the plugin's configuration in config.xml.

Look for the plugin entry in your config.xml and add a variable for the GMS version:

<plugin name="cordova-plugin-google-plus" spec="^8.5.2">
    <!-- 其他插件变量... -->
    <variable name="PLAY_SERVICES_AUTH_VERSION" value="11+" />
</plugin>

Most popular Cordova plugins that use Google Play Services expose these version variables, so check the plugin's documentation to confirm the exact variable name if needed.

方法3:使用Cordova钩子脚本(兜底方案)

If the above methods don't work for your setup, you can create a Cordova hook that automatically modifies the project.properties file after every prepare step.

  1. Create a hooks/after_prepare directory in your project root (if it doesn't exist already).
  2. Inside that directory, create a file named 01_fix_gms_version.js with this content:
#!/usr/bin/env node

const fs = require('fs');
const path = require('path');

// Path to the project.properties file
const propsPath = path.join(__dirname, '../../platforms/android/project.properties');

fs.readFile(propsPath, 'utf8', (err, data) => {
    if (err) {
        console.error('Error reading project.properties:', err);
        return;
    }
    // Replace the old version with the new one
    const updatedData = data.replace(
        /cordova.system.library.1=com.google.android.gms:play-services-auth:\+/g,
        'cordova.system.library.1=com.google.android.gms:play-services-auth:11+'
    );
    // Write the updated content back
    fs.writeFile(propsPath, updatedData, 'utf8', (err) => {
        if (err) console.error('Error writing project.properties:', err);
    });
});
  1. On Mac/Linux, give the script execution permissions with:
chmod +x hooks/after_prepare/01_fix_gms_version.js

Now, every time you run ionic cordova prepare android, the hook will automatically update the project.properties file. Since the hooks/ directory is tracked in git, this script will be shared with your team.

内容的提问来源于stack exchange,提问作者Craig

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 03:42:13