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

升级Android Target SDK至34后Google Play Games Services(C++)初始化失败求助

问题概述

将Android Target SDK从33升级至34后,Google Play Games Services C++ SDK初始化失败,platform_configuration_->Valid() == false,初始错误日志如下:

1 GamesNativeSDK          V  Using classes from /data/user/0/com.mycompany.myapp/app_.gpg.classloader/4105d0375b9d69fe6ee3a07b7893a4f1_games.jar.
2 GamesNativeSDK          I  Using existing jar.
3 GamesNativeSDK          I  Writing 1881 bytes to jar file
a.myapp                 E  Attempt to load writable dex file: /data/user/0/com.mycompany.myapp/app_.gpg.classloader/4105d0375b9d69fe6ee3a07b7893a4f1_games.jar
4 Compatibil...geReporter D  Compat change id reported: 218865702; UID 10402; state: ENABLED
5 GamesNativeSDK          E  Exception in dalvik/system/DexClassLoader.<init>: java.lang.SecurityException: Writable dex file '/data/user/0/com.mycompany.myapp/app_.gpg.classloader/4105d0375b9d69fe6ee3a07b7893a4f1_games.jar' is not allowed..
6 GamesNativeSDK          E  Could not create class loader from file.
7 GamesNativeSDK          E  Could not load additional classes from embedded jar.

尝试在Activity的onCreate中添加代码设置单个jar文件为只读后,日志2-5消失,但初始化仍失败:

File(Context.getDataDir() + "/app_.gpg.classloader/4105d0375b9d69fe6ee3a07b7893a4f1_games.jar").setReadOnly();
解决方案

1. 更新Google Play Games Services SDK到最新版本

Android 14(API 34)对Dex文件加载的权限限制更严格,旧版GPG SDK未适配该规则。将GPG相关依赖更新至最新稳定版,示例Gradle配置:

implementation "com.google.android.gms:play-services-games-v2:23.1.0"
implementation "com.google.android.gms:play-services-auth:20.7.0"

建议锁定具体版本,版本号可参考Google官方文档的最新稳定版说明。

2. 批量处理GPG生成的Jar文件权限

单个jar文件的哈希名可能随SDK版本变化,手动指定不可靠。遍历app_.gpg.classloader目录下所有jar文件,统一设置为只读:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // 处理GPG jar文件权限,需在GPG初始化前执行
    File gpgClassLoaderDir = new File(getDataDir(), "app_.gpg.classloader");
    if (gpgClassLoaderDir.exists() && gpgClassLoaderDir.isDirectory()) {
        File[] jarFiles = gpgClassLoaderDir.listFiles((dir, name) -> name.endsWith(".jar"));
        if (jarFiles != null) {
            for (File jarFile : jarFiles) {
                jarFile.setReadOnly();
            }
        }
    }
    // 初始化Google Play Games Services
    initGooglePlayGames();
}

3. 适配Android 14的Dex加载规则

  • 检查AndroidManifest.xml,移除可能干扰文件权限的异常配置
  • 确认Scoped Storage相关设置未限制GPG目录的访问权限

4. 清理缓存后重新测试

  • 卸载应用并清除设备上的应用缓存
  • 重新构建并安装应用,避免旧缓存文件残留导致权限问题

5. 排查Native SDK初始化流程

  • 验证C++端PlatformConfiguration的参数设置(如Context传递是否有效)
  • 监听初始化回调的详细错误信息,定位具体失败原因

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.19 18:32:39