Skip to content

Instantly share code, notes, and snippets.

@dtaylorus
Last active May 9, 2022 20:27
Show Gist options
  • Save dtaylorus/63fef408cec34999a1e566bd5fac27e5 to your computer and use it in GitHub Desktop.
Save dtaylorus/63fef408cec34999a1e566bd5fac27e5 to your computer and use it in GitHub Desktop.
Workaround for broken interstitial and rewarded ad callbacks in Xamarin.GooglePlayServices.Ads.
using System;
using Android.Runtime;
namespace Android.Gms.Ads.Hack
{
public abstract class InterstitialAdLoadCallback : global::Android.Gms.Ads.Interstitial.InterstitialAdLoadCallback
{
private static Delegate cb_onAdLoaded;
private static Delegate GetOnAdLoadedHandler()
{
if (cb_onAdLoaded is null)
{
cb_onAdLoaded = JNINativeWrapper.CreateDelegate((Action<IntPtr, IntPtr, IntPtr>)n_onAdLoaded);
}
return cb_onAdLoaded;
}
private static void n_onAdLoaded(IntPtr jnienv, IntPtr native__this, IntPtr native_p0)
{
InterstitialAdLoadCallback thisobject = GetObject<InterstitialAdLoadCallback>(jnienv, native__this, JniHandleOwnership.DoNotTransfer);
global::Android.Gms.Ads.Interstitial.InterstitialAd resultobject = GetObject<global::Android.Gms.Ads.Interstitial.InterstitialAd>(native_p0, JniHandleOwnership.DoNotTransfer);
thisobject.OnAdLoaded(resultobject);
}
[Register("onAdLoaded", "(Lcom/google/android/gms/ads/interstitial/InterstitialAd;)V", "GetOnAdLoadedHandler")]
public virtual void OnAdLoaded(global::Android.Gms.Ads.Interstitial.InterstitialAd interstitialAd)
{
}
}
public abstract class RewardedAdLoadCallback : global::Android.Gms.Ads.Rewarded.RewardedAdLoadCallback
{
private static Delegate cb_onAdLoaded;
private static Delegate GetOnAdLoadedHandler()
{
if (cb_onAdLoaded is null)
{
cb_onAdLoaded = JNINativeWrapper.CreateDelegate((Action<IntPtr, IntPtr, IntPtr>)n_onAdLoaded);
}
return cb_onAdLoaded;
}
private static void n_onAdLoaded(IntPtr jnienv, IntPtr native__this, IntPtr native_p0)
{
RewardedAdLoadCallback thisobject = GetObject<RewardedAdLoadCallback>(jnienv, native__this, JniHandleOwnership.DoNotTransfer);
global::Android.Gms.Ads.Rewarded.RewardedAd resultobject = GetObject<global::Android.Gms.Ads.Rewarded.RewardedAd>(native_p0, JniHandleOwnership.DoNotTransfer);
thisobject.OnAdLoaded(resultobject);
}
[Register("onAdLoaded", "(Lcom/google/android/gms/ads/rewarded/RewardedAd;)V", "GetOnAdLoadedHandler")]
public virtual void OnAdLoaded(global::Android.Gms.Ads.Rewarded.RewardedAd rewardedAd)
{
}
}
}
@dtaylorus
Copy link
Author

Updated to not require unsafe code compiler flag. See xamarin/GooglePlayServicesComponents#425 for details.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment