Unity Troubleshooting: Google Play Games and Mobile Ads Plugins Integration

As a mobile games developer, in game ads are usually a primary source of income. Specifically for android games, signing into Google Play Games (GPG) is also necessary to provide more features. Implementing each of these functionalities on their own is a relatively easy as the plugin developers have done a phenomenal job. You can find below links of these repositories.

play-games-plugin-for-unity: https://github.com/playgameservices/play-games-plugin-for-unity

google-mobileads-unity (GMA): https://github.com/googleads/googleads-mobile-unity

Each of these plugin contains an additional package called ExternalDependencyManager which downloads all of the android dependency required by the project at build time. However for projects that contain both of these plugins, the build process may produce some errors that are tricky to troubleshoot. This post details one of the solutions to this issue.

The Symptoms

When GPG and GMA are not playing well together, the project may show the following problems:

  1. Game immediately closes on start (only on device, works fine in unity editor)
  2. Game opens fine but GPG sign in never occurs

Diagnosing The Problem

To diagnose the problem, the best way is to debug the apk on a physical device or android emulator. For building the apk, make sure to set it to ‘development build’. When done, we can debug the apk on your device straight from android studio via the option “profile or debug apk”. This will run the app on your device and output any print/error messages thrown by the app in the console.

The enemy here is java.lang.ClassNotFoundException. With this error, we’ll also be shown which class the apk build is missing. It appears here that when both GPG and GMA are in a single project, the ExternalDependencyManager package does not add a few essential class libraries to build.

Solution

To solve this, we need to tell the android build system to keep all of the classes needed by both GPG and GMA. This can be done by providing a custom proguard-user.txt file. First, the proguard-user.txt file in the Assets/Plugins/Android. Copy and paste the content below into this new file.

-dontwarn com.google.vr.ndk.base.DaydreamApi
-keep class com.facebook.** {
   *;
}
-keep class com.google.unity.** {
   *;
}
-keep public class com.google.android.gms.ads.**{
   public *;
}
-keep class com.google.android.gms.games.leaderboard.** { *; }
-keep class com.google.android.gms.games.snapshot.** { *; }
-keep class com.google.android.gms.games.achievement.** { *; }
-keep class com.google.android.gms.games.event.** { *; }
-keep class com.google.android.gms.games.stats.** { *; }
-keep class com.google.android.gms.games.video.** { *; }
-keep class com.google.android.gms.games.* { *; }
-keep class com.google.android.gms.common.api.ResultCallback { *; }
-keep class com.google.android.gms.signin.** { *; }
-keep class com.google.android.gms.dynamic.** { *; }
-keep class com.google.android.gms.dynamite.** { *; }
-keep class com.google.android.gms.tasks.** { *; }
-keep class com.google.android.gms.security.** { *; }
-keep class com.google.android.gms.base.** { *; }
-keep class com.google.android.gms.actions.** { *; }
-keep class com.google.games.bridge.** { *; }
-keep class com.google.android.gms.common.ConnectionResult { *; }
-keep class com.google.android.gms.common.GooglePlayServicesUtil { *; }
-keep class com.google.android.gms.common.api.** { *; }
-keep class com.google.android.gms.common.data.DataBufferUtils { *; }
-keep class com.google.android.gms.games.quest.** { *; }
-keep class com.google.android.gms.nearby.** { *; }
-keep public class com.google.ads.**{
   public *;
}
-keepattributes *Annotation*
-dontobfuscate

Next, enable Custom Gradle Properties Template and Custom Proguard File in Unity’s Player Settings as shown in the image below.

Ensure that unity is pointing to your newly created proguard text file. (Note the Custom Gradle Properties Template option may be unnecessary).

Conclusion

Build a new apk and do a run through. Both GPG and GMA should be up and running, if this is not the case, debug the apk once and again for any other missing libraries. The missing libraries can then be added to the proguard file. Hope this helps someone (I spent like 3 days on this problem, sad times).

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.