Didn't find class "com.google.android.gms.ads.MobileAdsInitProvider"

Briefly, it appears “R8” Code Shrinking causes an Android App to crash if using AdMob (even latest version 118.3.0).

To explain…

The scenario is, using Visual Studio 2019 you’ve just added AdMob to your MonoGame Android project…

You’ve added these updates to the AppManifest.xml:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
	<uses-permission android:name="android.permission.INTERNET" />

...
<application ...>
<!-- TODO: Replace with your real AdMob app ID -->
		<meta-data
			android:name="com.google.android.gms.ads.APPLICATION_ID"
			android:value="ca-app-pub-3940256099942544~3347511713" />

</application>

You’ve signed your app.

You’ve deployed to a device in Debug mode. Successfully.

You then deploy in Release mode with “R8” code shrinking enabled (“ProGuard” is no longer supported, apparently) . Your app crashes with no message.

Hmmmm. You then go back to Debug configuration and enable “R8” code shrinking on that. The app still crashes but now you see the error message starting:

Java.Lang.RuntimeException: 'Unable to get provider com.google.android.gms.ads.MobileAdsInitProvider: java.lang.ClassNotFoundException: Didn't find class "com.google.android.gms.ads.MobileAdsInitProvider" on path: DexPathList[[zip file "/system/framework/org.apache.http.legacy.boot.jar", zip file "/data/app/

…followed by info particular to your app name.

I’ve tried google and generic “Android questions” on stackoverflow such as these:

https://stackoverflow.com/questions/63306622/unable-get-provider-mobileadsinitprovider-classnotfoundexception-didnt-find-c

But commonly they’re either not answered or result in “Did you manage to solve this?” messages.

One workaround, of course, is to not use ANY code shrinking since ProGuard is not allowed and “R8” causes your app to crash.

But why? Well, another workaround I’ve found is to specify that the Linker skips the assembly containing the “.MobileAdsInitProvider” class namely the, “Xamarin.GooglePlayServices.Ads.Lite” assembly. So adding:

Xamarin.GooglePlayServices.Ads.Lite

to the “Skip Linking Assemblies” also works around the problem.

Being unfamiliar I read here: How to Avoid Exception From Setting Linker Behavior to Link All - Telerik UI for Xamarin

As the linker is a static tool, it can not plan for or include types and methods that are called through reflection, or dynamically instantiated. In short, if that type is not explicitly referenced in your project code, it will get removed by the linker.

So my understanding is that reflection or dynamic (Java interop?) code is being used in the AdMob implementation of “Xamarin.GooglePlayServices.Ads.Lite” (and maybe other dependancies too, maybe there are further problems waiting down the road by doing this?) and the linker is stripping them out if any “code shrinking” is enabled.

I’m unsure if this is correct though as it seems such a mainstream thing yet there there seems very little public knowledge, instruction or discussion about it, other than “Did you manage to solve this?”, unless I’m just missing it completely.

Is anyone here able to confirm or deny any of this? Are there better solutions? Is Code Shrinking just bad form and something to be avoided when using any interop?

Well an update on this debacle.

Developing for Amazon FireOS (ie Android without GooglePlayServices installed) it seems we must use the full fat version of Xamarin.GooglePlayServices.Ads (which seems to incorporate a different “Lite” variant.

Things worked out until I deigned to attempt to use rewarded ads.

Immediately it fails:

Java.Lang.LinkageError: ‘no non-static method “Lcom/google/android/gms/internal/ads/zzarw;.setRewardedVideoAdListener(Lcom/google/android/gms/ads/reward/RewardedVideoAdListener;)V”’

Looking in Xamarin.GooglePlayServices.Ads for “zzarw” yields nothing, although some very similar named stuff inside the Android.Gms.Internal.Ads namespace.

The skipping link trick no longer works and I can’t find what other assembly I might have to add to the skip list, I tried several including Xamarin.GooglePlayServices.Ads itself.

This is exactly the

maybe there are further problems waiting down the road by doing this?

scenario.

For now I’m going to back up and remove the “R8” Code Shrinking as it’s the only workaround that works now.

Does anyone know anything about this shambles?

I am suffering from exactly same problem as yours. I read your post yesterday but I had no account on this site so I waited for reply.

So to the problem.

(“ProGuard” is no longer supported, apparently)

It’s not true you can find ProGuard files being generated in \obj\Release\100\proguard.
After reading your post I checked those files, apart from com.google.android.gms.ads.MobileAdsInitProvider class all classes kept by ProGuard.

So I did as you mentioned , I skipped Xamarin.GooglePlayServices.Ads.Lite from Linking. Then it started working.

Skipping complete package won’t add much size to package. My package was 20.02 Mb before skipping and after skipping it become 20.89 Mb , So basically difference will be around 900 Kb.

I am currently using adaptive ads. So far working fine. I’ll check with Rewarded ads and let you know if it’s working or not.

My past application were working fine with R8 and Ads.Lite. Seems this issue is related to Visual Studio 2019 update.

[edit]

I checked my ProGuard files. Seems class with related methods present in file.

setRewardedVideoAdListener(…);

-keep class com.google.android.gms.ads.InterstitialAd
-keepclassmembers class com.google.android.gms.ads.InterstitialAd {
<init>(...);
*** getAdListener(...);
*** setAdListener(...);
*** getAdMetadata(...);
*** getAdUnitId(...);
*** setAdUnitId(...);
*** isLoaded(...);
*** isLoading(...);
*** getMediationAdapterClassName(...);
*** loadAd(...);
*** setAdMetadataListener(...);
*** setImmersiveMode(...);
*** setRewardedVideoAdListener(...);
*** show(...);
*** zzd(...);
}

[edit]

Added Firebase Analytics and release broke again. Even adding package to skip. As you did, I also ignored R8 for now.

[edit]

Ignored R8, Set Android package format to Bundle. Now package size came down 13Mb

Thanks for your response, signing up here and documenting your findings as you go, I’m very grateful :slight_smile:

I’m not familiar with

adaptive ads

, what are they?

So, if I’m understanding, the initial Linking skip worked for you as it did for me but then adding Firebase Analytics (not something I know anything about either) it broke for you again like Rewarded ads did for me. I did worry the workaround would cause further issues down the road.

I would guess there must be one of the assemblies that could be skipped to stop each error, rather than disabling R8 all together, but I’m damned if I know which one it is. Maybe I’ll try each assembly from the Nuget packages (I have 28 of them, maybe I can add them all to skipping?) to see if there is one I can skip that might stop this. If I do I’ll report back here.

So, your workaround currently is to removed R8 Code Shrinking but use the Bundle format to get the file size down? I didn’t know a bundle would do that!

I’m not sure if Amazon supports Bundles, I thought that was a Google thing but maybe I’m wrong about that.

For my project I’m using split apks, one for each architecture and for Amazon I just upload the “armeabi-v7a.apk” version as that’s the only one their devices seems to support. BTW I do now actually have separate projects for Google and Amazon since their “TV” implementations are different and seem to need different approaches to work.

For file sizes my vanilla no ads, R8 version stands at 6.5MB where the Non R8 ads version is 8.5MB so not a big deal for me but still annoying all the same.

One of the issues I find with Monogame in this cross platform regard is that while it is extremely capable and amazing (I have to keep pinching myself this is a small team making this magic happen), platform information for Android as it relates to Monogame & C# is very hard to come by with all documentation being referencing Java/Kotlin/Android Studio/other stuff I don’t know or care about.

I’m also going to assume you’re right about this being a Visual Studio 2019 issue as I’ve noticed alot of bugs with it after moving over from VS2017 (forced to because of Google insisting on API 29 and VS2017 bug of not being able to compile for that!).

Like with all this stuff, it’s like you have to get all the damn ducks lined up in a row and only when they are all perfectly aligned can you get it to work, the rest of the time is pure hell. I’m happy to deal with my own program bugs but I throw my toys when forced to workaround other people’s bugs and I get very grumpy indeed. :joy:

Edit: I just tried a test to skip all the assemblies brought in by nuget, using this string (I think this is all of them):

Xamarin.GooglePlayServices.Ads.Lite;Xamarin.GooglePlayServices.Ads;Xamarin.AndroidX.Activity;Xamarin.AndroidX.Annotation;Xamarin.AndroidX.Arch.Core.Common;Xamarin.AndroidX.Arch.Core.Runtime;Xamarin.AndroidX.Browser;Xamarin.AndroidX.Collection;Xamarin.AndroidX.Core;Xamarin.AndroidX.CustomView;Xamarin.AndroidX.Fragment;Xamarin.AndroidX.Lifecycle.Common;Xamarin.AndroidX.Lifecycle.LiveData.Core;Xamarin.AndroidX.Lifecycle.Runtime;Xamarin.AndroidX.Lifecycle.ViewModel;Xamarin.AndroidX.Loader;Xamarin.AndroidX.MultiDex;Xamarin.AndroidX.SavedState;Xamarin.AndroidX.VersionedParcelable;Xamarin.AndroidX.ViewPager;Xamarin.Google.Guava.ListenableFuture;Xamarin.GooglePlayServices.Ads.Base;Xamarin.GooglePlayServices.Ads.Identifier;Xamarin.GooglePlayServices.Basement;Xamarin.GooglePlayServices.Gass;Xamarin.GooglePlayServices.Measurement.Base;Xamarin.GooglePlayServices.Measurement.Sdk.Api;Xamarin.GooglePlayServices.Tasks;Java.Interop

But still the same problem:

Java.Lang.LinkageError: ‘no non-static method “Lcom/google/android/gms/internal/ads/zzarw;.setRewardedVideoAdListener(Lcom/google/android/gms/ads/reward/RewardedVideoAdListener;)V”’

So much for that idea.

Try keeping above class which contains setRewardedVideoAdListener. The list your provided of skipped assemblies don’t have the actual class which giving the error in the first place. I guess… :grinning:

Java.Lang.LinkageError definitely points towards one of the class which giving error.

Sorry bout that, I didn’t know you were using Amazon. Yes it’s google thing, Application I am developing is based on Xamarin Android.

I’m sorry I just don’t understand what you mean :worried:

com.google.android.gms.ads.InterstitialAd

looks like a Java package or namespace/class identifier whereas what I’m skipping in VS2019 are declared to be .NET Assemblies (which tend not to be named com.google…). My string is simply to skip every single assembly brought into the project by Nuget when I installed the

Xamarin.GooglePlayServices.Ads

package (supposing that it is one or more of these that is causing the issues with code shrinking). This is the string of all those .NET Assemblies;

Java.Interop;Xamarin.AndroidX.Activity;Xamarin.AndroidX.Annotation;Xamarin.AndroidX.Arch.Core.Common;Xamarin.AndroidX.Arch.Core.Runtime;Xamarin.AndroidX.Browser;Xamarin.AndroidX.Collection;Xamarin.AndroidX.Core;Xamarin.AndroidX.CustomView;Xamarin.AndroidX.Fragment;Xamarin.AndroidX.Lifecycle.Common;Xamarin.AndroidX.Lifecycle.LiveData.Core;Xamarin.AndroidX.Lifecycle.Runtime;Xamarin.AndroidX.Lifecycle.ViewModel;Xamarin.AndroidX.Loader;Xamarin.AndroidX.MultiDex;Xamarin.AndroidX.SavedState;Xamarin.AndroidX.VersionedParcelable;Xamarin.AndroidX.ViewPager;Xamarin.Google.Guava.ListenableFuture;Xamarin.GooglePlayServices.Ads;Xamarin.GooglePlayServices.Ads.Base;Xamarin.GooglePlayServices.Ads.Identifier;Xamarin.GooglePlayServices.Ads.Lite;Xamarin.GooglePlayServices.Basement;Xamarin.GooglePlayServices.Gass;Xamarin.GooglePlayServices.Measurement.Base;Xamarin.GooglePlayServices.Measurement.Sdk.Api;Xamarin.GooglePlayServices.Tasks

I have seen people on Android forums talking about “-keep” switches using java package names like the one you’ve specified (which seems a similar concept to the “Skip Linking Assemblies” in Visual Studio) but they are using Android Studio and I have no idea at all how to do the “-keep” using Visual Studio. I thought “Skip Linking Assemblies” would achieve the same thing.

I feel like I’m missing something, can you explain it a bit more for me please, how I can “keep” that particular class using Visual Studio?

A lot of text to read through. I have interstatial ads working but I haven’t tried reward based ads yet. What is the exact error message that you are receiving now?

Hello, a quick summary for you:

Interstitial Ads, using R8 Code Shrinking:

Solved by Skip Linking on the assembly:

Xamarin.GooglePlayServices.Ads.Lite

Rewarded video ads with R8 Code Shrinking and the above assembly already skipped or not:

Java.Lang.LinkageError: ‘no non-static method “Lcom/google/android/gms/internal/ads/zzarw;.setRewardedVideoAdListener(Lcom/google/android/gms/ads/reward/RewardedVideoAdListener;)V”’

I have no solution other than to remove R8 code shrinking when using Rewarded ads.

Similarly with Firebase I believe, although note that APK bundling can reduce file sizes.

Check if your project contain this folder, If it is then you’ll have ProGuard files generated there. You can edit proguard file and add following block, if it is not there.

When you skip Xamarin.GooglePlayServices.Ads.Lite from “Skip Linking Assemblies” , Visual Studio 2019 generates ProGuard file while keeping all classes from that package using -keep.

Hmmmm, so compiling for Release with R8 code shrinking but without any “skip linking assemblies” I have 3 files in the folder

obj\Release\90\proguard

Of which one of them:

proguard_project_references.cfg

Contains this:

-keep class com.google.android.gms.ads.InterstitialAd
-keepclassmembers class com.google.android.gms.ads.InterstitialAd {
(…);
*** getAdListener(…);
*** setAdListener(…);
*** getAdMetadata(…);
*** getAdUnitId(…);
*** setAdUnitId(…);
*** isLoaded(…);
*** isLoading(…);
*** getMediationAdapterClassName(…);
*** loadAd(…);
*** setAdMetadataListener(…);
*** setImmersiveMode(…);
*** setRewardedVideoAdListener(…);
*** show(…);
*** zzd(…);
}

In fact, everything in that file seems to be marked as “-keep”?

So I’m not sure what to make of this. What has Proguard got to do with R8? Where is the config for R8 stating “-keep” for everything like Proguard does? Is this Proguard configuration that would have correctly setup “-keep” flags whereas R8 does not (and so causes failure at runtime)?

If I try to build instead with “Proguard” code shrinking then the build itself fails with this:

1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Xamarin\Android\Xamarin.Android.Common.targets(556,2): error XA1011: Using ProGuard with the D8 DEX compiler is no longer supported. Please set the code shrinker to r8 in the Visual Studio project property pages or edit the project file in a text editor and set the AndroidLinkTool MSBuild property to r8.

Which was the whole reason for using R8 in the first place.

So then if I try instead to compile using “dx” for “Dex compiler” (I have no idea what this is btw or how it is different to “d8”) then I get a whole load of warnings and finally a build fail like this:

1>PROGUARD : warning : there were 163 unresolved references to classes or interfaces.
1>PROGUARD : warning : there were 31 unresolved references to library class members.
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Xamarin\Android\Xamarin.Android.DX.targets(32,5): error MSB6006: “java.exe” exited with code 1.

The result is also identical if I have

Xamarin.GooglePlayServices.Ads.Lite

included as a “skip linking assemblies”.

So around and round I go, does anyone know what to make of any of this?

In the meantime I’m left with asserting that R8 code shrinking is the only buildable option in VS2019 and can only be used in conjunction with the following nuget packages:

Xamarin.GooglePlayServices.Ads or Xamarin.GooglePlayServices.Ads.Lite

if you’re not using Rewarded Ads AND you’ve added

Xamarin.GooglePlayServices.Ads.Lite

to “skip Linking assemblies”.

If Rewarded Ads are used then code shrinking cannot be used at all. There is no known fix.

I do not understand this very well and of course I’d love to be proved wrong.

I’ve just spotted this warning on the Amazon dev console which I think is related to this issue:

Alert for apps with In-App Purchasing (IAP) items: The latest update to Android Studio applies a default optimization from the R8 compiler that causes IAP purchases in the Amazon Appstore to fail. If your Android Studio uses Gradle 3.4.0 or higher and your app has IAP items, you must add several lines into your proguard.cfg file to exclude R8 from optimizing the IAP code. See Obfuscate The App Code for details.

I wonder if their solution can be modified to solve for Google Ads as well?