Problem building app bundle with latest Google Mobile ads library

I am using Unity editor version 2021.3.24f1 and Google Mobile ads 8.1.0. When I try to generate an app bundle, I get the following error:

Execution failed for task ‘:launcher:minifyReleaseWithProguard’.

java.io.IOException: Can’t read [C:.….gradle\caches\transforms-2\files-2.1\22478dd874d113db50106831ca8005f0\googlemobileads-unity-runtime.jar(;;;;;;;**.class)]
(Can’t process class [com/google/unity/ads/AdNetworkExtras.class] (Unsupported version number [55.0] (maximum 54.0, Java 10)))

I am using the Unity built-in version of JDK so I assume that the problem is with the Google Mobile ads version. I tried the solutions suggested for similar issues but they did not fix the problem.

Can someone help?

Can you try enabling R8 from build settings? unfortunately, I can’t reproduce it. If possible send me a sample project.

Thanks for the reply. I tried that - it works but I the app gets stuck when run (loading) and the log shows the following error:

AndroidJavaException: java.lang.NoClassDefFoundError: Failed resolution of: Landroidx/activity/result/ActivityResultLauncher;
java.lang.NoClassDefFoundError: Failed resolution of: Landroidx/activity/result/ActivityResultLauncher;
at java.lang.reflect.Executable.getParameterTypesInternal(Native Method)
at java.lang.reflect.Method.getParameterTypes(Method.java:186)
at java.lang.Class.getDeclaredMethods(Class.java:1915)
at com.unity3d.player.ReflectionHelper.getMethodID(Unknown Source:26)
at com.unity3d.player.UnityPlayer.nativeRender(Native Method)
at com.unity3d.player.UnityPlayer.access$500(Unknown Source:0)
at com.unity3d.player.UnityPlayer$e$1.handleMessage(Unknown Source:114)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loopOnce(Looper.java:201)
at android.os.Looper.loop(Looper.java:288)
at com.unity3d.player.UnityPlayer$e.run(Unknown Source:20)
Caused by: java.lang

I understand that the problem is that the ActivityResultLauncher is not found, probably because it gets removed by R8. Is there a fix to this?

Possible but not sure. Pass me a sample unity project to look into.

Thank you.

How can I send a sample Unity project? What do you need exactly?

My project is using the default gradle and proguard files… Should I use a custom Proguard file and enter commands to keep the above library?

well, you need to try creating a proguard file with keep options for the classes it states as missing.
Just upload the same project somewhere and pass me the link. I just need a way to reproduce on my end to see what solution fits.

I tried different things to fix the problem. I even created a new project and copied assets, code and settings to it (from the project that has the problem) and I still have the same issue. When I run a development build on the device it works correctly. Therefore, the problem is that R8 somehow removes or obfuscates the ActivityResultLauncher class.

I modified the proguard file (Assets\Plugins\Android\proguard-user.txt) with the following entries but it still does not work. Is there anything else I can try?

-keep class androidx.activity.result.ActivityResultLauncher { ; }
-keep class androidx.activity.result.
* { *; }

-keep class * implements androidx.activity.result.ActivityResultCallback {
;
}
-keep class * extends androidx.activity.result.ActivityResultLauncher {
(…);
void launch(…);
}

I tried again and managed to fix the problem using the proguard rules above (not sure what was the problem before). It works in the new project I created but not on the old one. Also, when R8 is not enabled, I get the other problem with Java version mismatch (54/55). I am not an expert in Unity but it looks like there are a lot of things that need fixing. I decided to use Unity LTS to have a more stable version but it looks like the LTS versions also have issues…

Does anyone have any idea on how to fix this?

We are getting the same exception, but in our case it happens when we try to check Google Play Availability:

public static int GetGooglePlayAvailability()
{
   const string googleApiAvailabilityClassname = "com.google.android.gms.common.GoogleApiAvailability";
   AndroidJavaClass clazz = new AndroidJavaClass(googleApiAvailabilityClassname);
   AndroidJavaObject apiAvailabilityInstance = clazz.CallStatic<AndroidJavaObject>("getInstance");

   int value = apiAvailabilityInstance.Call<int>("isGooglePlayServicesAvailable", activity);

   // result codes from https://developers.google.com/android/reference/com/google/android/gms/common/ConnectionResult

   // 0 == success
   // 1 == service_missing
   // 2 == update service required
   // 3 == service disabled
   // 18 == service updating
      // 9 == service invalid
   return value;
}