I'm trying to implement rewarded ads in Xamarin.Android in C#.
The code snippets in the Android documentation are written in Java and normally I don't have much problem adopting that code in C#, but now it does seem to be a problem.
To load a RewardedAd in Java, you have to do the following (according to the official documentation):
AdRequest adRequest = new AdRequest.Builder().build();
RewardedAd.load(this, "ca-app-pub-3940256099942544/5224354917",
adRequest, new RewardedAdLoadCallback(){
@Override
public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
// Handle the error.
Log.d(TAG, loadAdError.getMessage());
mRewardedAd = null
}
@Override
public void onAdLoaded(@NonNull RewardedAd rewardedAd) {
mRewardedAd = rewardedAd;
Log.d(TAG, "onAdFailedToLoad");
}
});
Now the problem here is that the object RewardedAdLoadCallback passed in this code, also defines the method implementations (onAdFailedToLoad and onAdLoaded), and in C# this cannot be done.
So my solution was to make my own class, MyRewardedAdLoadCallback, and override the methods.
public class MyRewardedAdLoadCallback : RewardedAdLoadCallback
{
public override void OnAdLoaded(Java.Lang.Object p0)
{
base.OnAdLoaded(p0);
}
public override void OnAdFailedToLoad(LoadAdError p0)
{
base.OnAdFailedToLoad(p0);
}
}
Then I simply load the rewarded ad with this code:
AdRequest adRequest = new AdRequest.Builder().Build();
RewardedAd.Load(this, "ca-app-pub-3940256099942544/5224354917", adRequest, new MyRewardedAdLoadCallback());
Now for some strange reason, I get this error when I build the project:
error: name clash: onAdLoaded(Object) in MyRewardedAdLoadCallback and onAdLoaded(AdT) in AdLoadCallback have the same erasure, yet neither overrides the other
public void onAdLoaded (java.lang.Object p0)
where AdT is a type-variable:
AdT extends Object declared in class AdLoadCallback
I tried to search for this error on the internet and came to the conclusion that it is a Java error and has something to do with generic types (see this SO question). However I do not see any generic types in my code, nor in the Android documentation about rewarded ads. There is also zero Xamarin documentation about the rewarded ads I'm trying to implement.
I'd be really glad if someone could help me out with this. Thanks in advance.