Implementing admob in xamarin android throws Java.Lang.RuntimeException

Viewed 1044

I'm creating an android application with xamarin and want to add ads using admob, I have already registered and created a banner unit so I have an app id and an ad id. I have implemented everything according to some guides but after I want to run it on my device it throws the following exception:

Java.Lang.RuntimeException
Message=Unable to get provider com.google.android.gms.ads.MobileAdsInitProvider: java.lang.IllegalStateException: 

******************************************************************************
* The Google Mobile Ads SDK was initialized incorrectly. AdMob publishers    *
* should follow the instructions here: googl link to add a valid  *
* App ID inside the AndroidManifest. Google Ad Manager publishers should     *
* follow instructions here: googl link.                           *
******************************************************************************

Here is the relevant part of my AndroidManifest.xml:

<meta-data android:name="com.google.android.gms.ads.APPLICATION_ID" 
               android:value="ca-app-pub-myappid"/> <!-- I have my actual app id here -->
    <activity android:name="com.google.android.gms.ads.AdActivity" 
              android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" 
              android:exported="false" 
              android:theme="@android:style/Theme.Translucent" />
    <provider android:name="com.google.android.gms.ads.MobileAdsInitProvider" 
              android:authorities="com.companyname.tapitmate.mobileadsinitprovider" 
              android:exported="false" 
              android:initOrder="100" />
    <meta-data android:name="com.google.android.gms.version" 
               android:value="@integer/google_play_services_version" />

This is my adview class:

public class AdControlView : View
{
}

This is my view renderer:

public class AdViewRenderer : ViewRenderer<AdControlView, AdView>
    {
        //This is the test id according to the documentation
        private readonly string _adUnitId = "ca-app-pub-3940256099942544/6300978111";
        private readonly AdSize _adSize = AdSize.SmartBanner;
        private AdView _adView;

        public AdViewRenderer(Context context) : base(context)
        {
        }

        AdView CreateAdView()
        {
            if (_adView != null)
                return _adView;
            _adView = new AdView(Context);
            _adView.AdSize = _adSize;
            _adView.AdUnitId = _adUnitId;
            var adParams = new LinearLayout.LayoutParams(LayoutParams.WrapContent, LayoutParams.WrapContent);
            _adView.LayoutParameters = adParams;
            _adView.LoadAd(new AdRequest.Builder().Build());
            return _adView;
        }

        protected override void OnElementChanged(ElementChangedEventArgs<AdControlView> e)
        {
            base.OnElementChanged(e);
            if (Control is null)
            {
                CreateAdView();
                SetNativeControl(_adView);
            }
        }
    }

And here is my main activity:

protected override void OnCreate(Bundle savedInstanceState)
        {
            TabLayoutResource = Resource.Layout.Tabbar;
            ToolbarResource = Resource.Layout.Toolbar;
            RequestedOrientation = ScreenOrientation.Portrait;

            base.OnCreate(savedInstanceState);

            Xamarin.Essentials.Platform.Init(this, savedInstanceState);
            global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
            MobileAds.Initialize(ApplicationContext);// I have tried both ways, same exception.
            //MobileAds.Initialize(ApplicationContext, "myactualappid");
            LoadApplication(new App());

I'm using:

  • Xamarin.Essentials 1.5.1
  • Xamarin.Forms 4.5.0.396
  • Xamarin.GooglePlayServices.Ads 71.1720.1

What am I doing wrong? Please help. }

3 Answers

Looks like you have missed ExportRenderer

[assembly: ExportRenderer(typeof(AdControlView), typeof(AdViewRenderer))]

In mainactivity the below line should go before Forms.Init line , you have added after Init

  MobileAds.Initialize(ApplicationContext, "ca-app-pub-3940256099942544/6300978111");

Your manifest should have adUnitId in the meta-data element within application element. Not sure about that since entire manifest has not been posted here. Like here

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.companyname.samplemtadmob">
    <uses-sdk android:minSdkVersion="21" android:targetSdkVersion="28" />
    <application android:label="SampleMTAdmob.Android">
      <meta-data android:name="com.google.android.gms.ads.APPLICATION_ID" android:value="ca­-app-pub-­3940256099942544~3347511713"/>
      <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version"/>
      <activity android:name="com.google.android.gms.ads.AdActivity" android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" android:theme="@android:style/Theme.Translucent" />
    </application>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
</manifest>

Also

Important

To test the banner during the development google uses two Banner Id, one for Android > and the other for iOS. Use them then remember to replace them with your own IDs:

Android: ca-app-pub-3940256099942544/6300978111 iOS: ca-app-pub-3940256099942544/2934735716

Try this nuget instead of implementing Renderers marcojak/MTAdmob

Most likely you just have to remove that MobileAdsInitProvider:

<provider
    android:name="com.google.android.gms.ads.MobileAdsInitProvider" 
    android:authorities="com.companyname.tapitmate.mobileadsinitprovider" 
    android:exported="false" 
    android:initOrder="100" />

The documentation does not even have it; therefore I'd assume you refer to an outdated example.

I had this issue when i update ads to version 71.1720.1, rolling back to version 60.1142.1 solved this issue for me had to do the same to Xamarin.maps

Related