googleads / googleads-mobile-unity

Official Unity Plugin for the Google Mobile Ads SDK
https://developers.google.com/admob/unity
Apache License 2.0
1.38k stars 1.08k forks source link

[Android] ClassNotFoundException for UMP classes if obfuscation is enabled #3078

Closed 256p closed 8 months ago

256p commented 11 months ago

[REQUIRED] Step 1: Describe your environment

[REQUIRED] Step 2: Describe the problem

Steps to reproduce:

  1. Create new project and import Google Mobile Ads Plugin
  2. Implement UMP consent pop-up
  3. In Project Setting -> Player -> Publishing Settings -> Minify enable minification
  4. Build and run
  5. Consent pop-up will not show and in the logcat will be error:
    AndroidJavaException: java.lang.ClassNotFoundException: com.google.android.ump.UserMessagingPlatform
    java.lang.ClassNotFoundException: com.google.android.ump.UserMessagingPlatform
    at java.lang.Class.classForName(Native Method)
    at java.lang.Class.forName(Class.java:536)
    Caused by: java.lang.ClassNotFoundException: com.google.android.ump.UserMessagingPlatform
    ... 2 more
    at UnityEngine.AndroidJNISafe.CheckException () [0x00000] in <00000000000000000000000000000000>:0 
    at UnityEngine.AndroidJNISafe.FindClass (System.String name) [0x00000] in <00000000000000000000000000000000>:0 
    at UnityEngine.AndroidJavaClass._AndroidJavaClass (System.String className) [0x00000] in <00000000000000000000000000000000>:0 
    at GoogleMobileAds.Ump.Android.ConsentInformationClient..ctor () [0x00000] in <00000000000000000000000000000000>:0 
    at GoogleMobileAds.Ump.Android.ConsentInformationClient..cctor () [0x00000] in <00000000000000000000000000000000>:0 
    at GoogleMobileAds.Ump.Android.UmpClientFactory.ConsentInformation

Relevant Code:

https://github.com/256p/ump-class-not-found

The reason of the issue is that UMP's Java classes are accessed from C# through reflection, and these Java classes aren't added to the Proguard rules to preserve them from obfuscation. All class names (except com.google.unity.ump.*) listed here https://github.com/googleads/googleads-mobile-unity/blob/main/source/plugin/Assets/GoogleMobileAds/Ump/Platforms/Android/Utils.cs aren't accessible after obfuscation. Probably need to add native bridging UMP implementation to the android-library.

NVentimiglia commented 10 months ago

Thanks, we will look into this issue.

amjaliks commented 10 months ago

@256p, add to proguard-user.txt:

-keep class com.google.android.ump.** { public *; }

@NVentimiglia, it would be useful to include a note about Proguard/R8 in the implementation documentation.

NVentimiglia commented 10 months ago

Thanks,

We will look into this.

NVentimiglia commented 10 months ago

Confirming the issue. If you use magnification please add a custom proguard file.

-keep class com.google.** { public *; }
batteredhedgehog commented 8 months ago

@NVentimiglia .. there isn't a whole lot of point using minification if you're going to put rules like that into proguard. The purpose is to slim down your build by removing unneeded code. Can someone suggest a set of less inclusive proguard rules for using admob?

NVentimiglia commented 8 months ago

@batteredhedgehog you can try

 -keep class com.google.android.ump.** { public *; }