Search code examples
androidandroid-studiolibgdxgoogle-play-services

GoogleApiClient error in lollipop and earlier versions


I've been trying to implement google play games services in my libgdx game with BaseGameUtils in android studio for some time and this works and initialize correctly, but only in the latest versions of android like Oreo or Nougat. Earlier versions show the following exception as soon as the app open:

E/AndroidRuntime: FATAL EXCEPTION: main
        Process: com.xx.mygame, PID: 1234
        java.lang.VerifyError: com/google/android/gms/common/api/GoogleApiClient$Builder
              at com.google.example.games.basegameutils.GameHelper.createApiClientBuilder(GameHelper.java:260)
              at com.google.example.games.basegameutils.GameHelper.setup(GameHelper.java:297)
              at com.xx.mygame.AndroidLauncher.onCreate(AndroidLauncher.java:31)
              at android.app.Activity.performCreate(Activity.java:5231)
              at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
              at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
              at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
              at android.app.ActivityThread.access$800(ActivityThread.java:135)
              at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
              at android.os.Handler.dispatchMessage(Handler.java:102)
              at android.os.Looper.loop(Looper.java:136)
              at android.app.ActivityThread.main(ActivityThread.java:5017)
              at java.lang.reflect.Method.invokeNative(Native Method)
              at java.lang.reflect.Method.invoke(Method.java:515)
              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
              at dalvik.system.NativeStart.main(Native Method)

I have been investigating the files of the BaseGameUtils module trying to identify the problem but I can't do anything. Any help appreciated

This is the build.gradle file:

project(":android") {
apply plugin: "android"

configurations { natives }

dependencies {
    compile project(":core")
    compile "com.badlogicgames.gdx:gdx-backend-android:$gdxVersion"
    natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-armeabi"
    natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-armeabi-v7a"
    natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-arm64-v8a"
    natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-x86"
    compile "com.badlogicgames.gdx:gdx-freetype:$gdxVersion"
    natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-armeabi"
    natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-armeabi-v7a"
    natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-arm64-v8a"
    natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-x86"
    natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-x86_64"
    compile "com.android.support:support-core-utils:26.0.0"
    compile 'com.google.android.gms:play-services-base:11.0.4'
    compile "com.google.android.gms:play-services-games:11.0.4"
    compile project(":BaseGameUtils")
}}

Build.gradle (Module: BaseGameUtils)

    dependencies {
    // Set defaults so that BaseGameUtils can be used outside of BasicSamples
    if (!project.hasProperty('appcompat_library_version')) {
        ext.appcompat_library_version = '20.0.+'
    }
    if (!project.hasProperty('support_library_version')) {
        ext.support_library_version = '20.0.+'
    }
    if (!project.hasProperty('gms_library_version')) {
        ext.gms_library_version = '8.4.0'
    }

    compile "com.android.support:appcompat-v7:${appcompat_library_version}"
    compile "com.android.support:support-v4:${support_library_version}"
    compile "com.google.android.gms:play-services-games:${gms_library_version}"
    compile "com.google.android.gms:play-services-plus:${gms_library_version}"
}

android {
    // Set defaults so that BaseGameUtils can be used outside of BasicSamples
    if (!project.hasProperty('android_compile_version')) {
        ext.android_compile_version = 23
    }
    if (!project.hasProperty('android_version')) {
        ext.build_tools_version = "23.0.2"
    }
    // Set defaults so that BaseGameUtils can be used outside of BasicSamples
    if (!project.hasProperty('android_min_sdk_version')) {
        // Google Play Services minimum requirements is 14
        ext.android_min_sdk_version = 14
    }

    compileSdkVersion android_compile_version
    buildToolsVersion "25.0.2"

    defaultConfig {
        minSdkVersion android_min_sdk_version
        targetSdkVersion android_compile_version
    }
}

Solution

  • Version mismatch/artifact conflict.

    Why two versions of same artifact. You're injecting version 11.0.4 of play-services-games in your project root build.gradle file and version 8.4.0 of same artifact in build.gradle file of BaseGameUtils module.

    So keep one version of any artifact in all over your project or better most updated version. Currently that is 11.2.2


    • So remove play-service dependency from root build.gradle file
    • Update gms library version in build.gradle file of BaseGameUtils to 11.2.2
    • Add Google's Maven repository in repositories list, From version 11.2.0 artifact now available on Google maven repo.

      repositories {
         maven { url "https://maven.google.com/" }
      }