Jc.AdMob.Avalonia
5.0.0
dotnet add package Jc.AdMob.Avalonia --version 5.0.0
NuGet\Install-Package Jc.AdMob.Avalonia -Version 5.0.0
<PackageReference Include="Jc.AdMob.Avalonia" Version="5.0.0" />
<PackageVersion Include="Jc.AdMob.Avalonia" Version="5.0.0" />
<PackageReference Include="Jc.AdMob.Avalonia" />
paket add Jc.AdMob.Avalonia --version 5.0.0
#r "nuget: Jc.AdMob.Avalonia, 5.0.0"
#:package Jc.AdMob.Avalonia@5.0.0
#addin nuget:?package=Jc.AdMob.Avalonia&version=5.0.0
#tool nuget:?package=Jc.AdMob.Avalonia&version=5.0.0
Jc.AdMob.Avalonia
Library to bring AdMob advertisements to Avalonia mobile projects.
Avalonia solution derived from marius-bughiu/Plugin.AdMob
Table of Contents
Introduction
Jc.AdMob.Avalonia is a library to bring Google AdMob services to Avalonia Android and iOS projects.
The library currently supports the following ad units:
| Consent | Banner | Interstitial | Rewarded interstitial | Rewarded | Native advanced | App open | |
|---|---|---|---|---|---|---|---|
| Android | ✓ | ✓ | ✓ | ✓ | ✓ | Preview | ✓ |
| iOS | ✓ | ✓ | ✓ | ✓ | ✓ | Preview | ✓ |
Usage
To use Jc.AdMob.Avalonia you must add the Jc.AdMob.Avalonia package to your cross-platform project.
dotnet add package Jc.AdMob.Avalonia
Followed by adding the Android/iOS Jc.AdMob.Avalonia.xxx package to the platform specific project(s).
dotnet add package Jc.AdMob.Avalonia.Android
dotnet add package Jc.AdMob.Avalonia.iOS
Then you must register AdMob in the CustomizeAppBuilder method in MainActivity.cs and AppDelegate.cs for Android and iOS respectively:
protected override AppBuilder CustomizeAppBuilder(AppBuilder builder)
{
return base.CustomizeAppBuilder(builder)
...
.UseAdMob(new AdMobOptions
{
TestDeviceIds = [],
TagForUnderAgeOfConsent = false,
TagForChildDirectedTreatment = false,
})
.AfterSetup(_ =>
{
AdMob.Current.Consent.OnConsentInitialized += (_, _) => AdMob.Current.Consent.ShowConsent();
});
}
Finally, follow the AdMob platform specific instructions in regard to configuring the application/unit ids.
Android
The Android package uses GMA Next-Gen. Set <SupportedOSPlatformVersion>24.0</SupportedOSPlatformVersion> in your Android app project. If you still support API 23, use the last legacy package.
Pass your Android AdMob application ID as AdMobOptions.AppId when calling .UseAdMob(...). Existing apps can also supply the ID through manifest metadata. Keep the following entry inside <application> for User Messaging Platform and use the same ID in both places:
<meta-data android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="ca-app-pub-XXXXXXXXXXXXXXXX~YYYYYYYYYY" />
Call .UseAdMob(...) before loading ads, including when constructing the Android rewarded wrappers directly. Subscribe to AdMob.Current.OnAdsInitialized when your app needs to wait for SDK initialization explicitly.
If your app uses mediation, check Google's Next-Gen adapter compatibility and migration instructions. Remove direct references to legacy Ads packages and update any adapter that pulls them in. Run dotnet nuget why <android-project> Xamarin.GooglePlayServices.Ads.Lite to find the referencing package. Repeat for Xamarin.GooglePlayServices.Ads and Xamarin.GooglePlayServices.Ads.Api if they appear in your dependency graph.
iOS
For iOS projects, from v11.13.0, you must add the following to your csproj file to support linking with Swift system libraries:
<Target Name="LinkWithSwift" DependsOnTargets="_ParseBundlerArguments;_DetectSdkLocations" BeforeTargets="_LinkNativeExecutable">
<PropertyGroup>
<_SwiftPlatform Condition="$(RuntimeIdentifier.StartsWith('iossimulator-'))">iphonesimulator</_SwiftPlatform>
<_SwiftPlatform Condition="$(RuntimeIdentifier.StartsWith('ios-'))">iphoneos</_SwiftPlatform>
</PropertyGroup>
<ItemGroup>
<_CustomLinkFlags Include="-L" />
<_CustomLinkFlags Include="/usr/lib/swift" />
<_CustomLinkFlags Include="-L" />
<_CustomLinkFlags Include="$(_SdkDevPath)/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/$(_SwiftPlatform)" />
<_CustomLinkFlags Include="-Wl,-rpath" />
<_CustomLinkFlags Include="-Wl,/usr/lib/swift" />
</ItemGroup>
</Target>
You must also ensure that the SupportedOSPlatformVersion attribute is set to 15.0 or higher in your project file:
<PropertyGroup>
<TargetFramework>net9.0-ios</TargetFramework>
<SupportedOSPlatformVersion>15.0</SupportedOSPlatformVersion>
</PropertyGroup>
along with setting it in the Info.plist:
<key>MinimumOSVersion</key>
<string>15.0</string>
Failing to set this will result in a compiler warning.
Test Devices
To configure test devices, the .UseAdMob() method accepts a collection of test device ids.
Consent
Google requires all publishers serving ads to EEA and UK users to use a Google-Certified Consent Management Platform (CMP).
Jc.AdMob.Avalonia provides a consent service using the Google User Messaging Platform (UMP) SDK.
iOS App Transparency Tracking
When using NSUserTrackingUsageDescription in your Info.plist, your app must present the AppTrackingTransparency (ATT) dialog to the user to enable the AdMob personalised ads.
By default this will be displayed after the consent dialog, but you can disable this by setting SkipTransparencyTracking to true in the AdMobOptions:
return base.CustomizeAppBuilder(builder)
...
.UseAdMob(new AdMobOptions
{
...
SkipTransparencyTracking = true,
});
Usage
When using the consent service, all services are accessed via the singleton AdMob.Current.
Call .UseAdMob(...) before any app-level .AfterSetup(...) that interacts with AdMob.Current. The consent and ads initialized events are readiness events, so subscribing after initialization has completed will invoke the handler immediately.
To enable ads in regions where consent is required, show the consent dialog once consent is initialized:
.UseAdMob(adMobOptions)
.AfterSetup(_ =>
{
AdMob.Current.Consent.OnConsentInitialized += (_, _) => AdMob.Current.Consent.ShowConsent();
});
Events
| Event | Notes |
|---|---|
| OnConsentInitialized | Replayed for late subscribers after init |
| OnConsentFailedToInitialize | |
| OnConsentFormLoaded | iOS only - issue being investigated |
| OnConsentFormFailedToLoad | iOS only - issue being investigated |
| OnConsentFormFailedToPresent | |
| OnConsentProvided | User consented and ads can be shown |
| OnConsentFormClosed | User consent form was displayed and dismissed |
Banners
| iOS | Android |
|---|---|
| <img alt="iOS Banner" src="img/iOS Banner.png" width="250" /> | <img alt="iOS Banner" src="img/Android Banner.jpeg" width="250" /> |
The BannerAd control can be added to your XAML like so:
...
xmlns:admob="https://github.com/jc-admob-avalonia"
...
<admob:BannerAd UnitId="{ADMOB_UNIT_ID}" />
Sizes
The banner can be configured with the following sizes:
| AdSize | Size |
|---|---|
| Banner | Mobile Marketing Association (MMA) banner ad size (320x50 density-independent pixels). |
| FullBanner | Interactive Advertising Bureau (IAB) full banner ad size (468x60 density-independent pixels). |
| Invalid | An invalid AdSize that will cause the ad request to fail immediately. |
| LargeBanner | Large banner ad size (320x100 density-independent pixels). |
| Leaderboard | Interactive Advertising Bureau (IAB) leaderboard ad size (728x90 density-independent pixels). |
| MediumRectangle | Interactive Advertising Bureau (IAB) medium rectangle ad size (300x250 density-independent pixels). |
| WideSkyscrapper | IAB wide skyscraper ad size (160x600 density-independent pixels). |
Note: If the size is left unspecified, the banner will resize to fit the parent container.
Events
The banner exposes the following events:
| Event | Notes |
|---|---|
| OnAdLoaded | |
| OnAdFailedToLoad | |
| OnAdImpression | |
| OnAdClicked | |
| OnAdOpened | |
| OnAdClosed | |
| OnAdSwiped | Retained for source compatibility. Next-Gen banners do not raise this event. |
Native Advanced ads
| Demo | iOS | Android |
|---|---|---|
| Image | <img src="img/ios-native-image.png" alt="Native image test ad on iOS" width="250" /> | <img src="img/android-native-image.png" alt="Native image test ad on Android" width="250" /> |
| Video | <img src="img/ios-native-video.png" alt="Native video test ad with a play button on iOS" width="250" /> | <img src="img/android-native-video.png" alt="Native video test ad playing on Android" width="250" /> |
Define the placement once in AXAML. The same layout runs on Android and iOS after the usual .UseAdMob(options) setup.
<UserControl xmlns="https://github.com/avaloniaui"
xmlns:admob="https://github.com/jc-admob-avalonia">
<admob:NativeAdView>
<admob:NativeAdView.AdContent>
<StackPanel Spacing="8">
<admob:NativeAdAttribution />
<Grid ColumnDefinitions="Auto,*">
<admob:NativeAdIcon Width="40" Height="40" Margin="0,0,8,0" />
<admob:NativeAdHeadline Grid.Column="1" />
</Grid>
<admob:NativeAdMedia />
<admob:NativeAdBody />
<admob:NativeAdCallToAction HorizontalAlignment="Right" />
</StackPanel>
</admob:NativeAdView.AdContent>
</admob:NativeAdView>
</UserControl>
Use Grid and StackPanel to arrange the assets. Include one each of NativeAdAttribution, NativeAdHeadline, NativeAdIcon, NativeAdMedia, and NativeAdCallToAction. You can also include NativeAdBody and NativeAdAdvertiser once each. Asset controls collapse when their optional asset is absent. Use an Auto grid row or column when its space should collapse too.
The attribution defaults to “Ad”. Set NativeAdAttribution.Text to a localized equivalent. Leave the top-right corner clear for AdChoices and allow at least 120 × 120 logical units for the media view. Give text room to wrap, and keep other app content outside the ad placement. Google’s SDK handles taps, impressions, AdChoices, and image or video playback. AdMob layout requirements
Set UnitId to the current platform’s native ad unit for production. An omitted or blank value loads a native demo ad. To test playback, use the Android native video unit or iOS native video unit. In the sample, select Show Native Image Ad or Show Native Video Ad to open the placement on a separate page. The native sample placements use Google’s demo ad units. Set App.TestConsentFlow to false to run the demo without the consent prompt. To test consent, enable App.TestConsentFlow in the shared sample and use an AdMob app ID with a published consent message in the Android manifest and app options or the iOS Info.plist. Apps using the library still control consent through their own AdMobOptions.
The control loads once per native attachment after SDK initialization and consent. Set the layout and unit ID before attachment.
Events
The native ad exposes the following events:
| Event | Notes |
|---|---|
| OnAdLoaded | |
| OnAdFailedToLoad | |
| OnAdImpression | |
| OnAdClicked | |
| OnAdOpened | |
| OnAdClosed |
Interstitial
| iOS | Android |
|---|---|
| <img alt="iOS Banner" src="img/iOS Interstitial.png" width="250" /> | <img alt="iOS Banner" src="img/Android Interstitial.jpeg" width="250" /> |
Interstitial ads can be used by calling Interstitial.Create(unitId) on the AdMob singleton.
This call will load and the return the interstitial ad with an OnAdLoaded event once it's finished loaded. .Show() can then be called to display the ad.
public ICommand ShowInterstitialAdCommand { get; }
public MainViewModel()
{
ShowInterstitialAdCommand = ReactiveCommand.Create(ShowInterstitialAd);
}
private void ShowInterstitialAd()
{
var interstitial = AdMob.Current.Interstitial.Create();
interstitial.OnAdLoaded += (_, _) => interstitial.Show();
}
Android
For Android on Avalonia 12+, no Activity is passed into .UseAdMob(). The library resolves the current Activity from lifecycle callbacks:
protected override AppBuilder CustomizeAppBuilder(AppBuilder builder)
{
return base.CustomizeAppBuilder(builder)
...
.UseAdMob(adMobOptions);
}
Events
| Event | Notes |
|---|---|
| OnAdLoaded | |
| OnAdFailedToLoad | |
| OnAdPresented | |
| OnAdFailedToPresent | |
| OnAdImpression | |
| OnAdClicked | |
| OnAdClosed |
Rewarded Interstitial
| iOS | Android |
|---|---|
| <img alt="iOS Banner" src="img/iOS Rewarded Interstitial.jpeg" width="250" /> | <img alt="iOS Banner" src="img/Android Rewarded Interstitial.jpg" width="250" /> |
Interstitial ads can be used by calling RewardedInterstitial.Create(unitId) on the AdMob singleton.
This call will load and the return the interstitial ad with an OnAdLoaded event once it's finished loaded. .Show() can then be called to display the ad.
The reward event OnUserEarnedReward can then be used to know when a reward can be given to the user.
public ICommand ShowRewardedInterstitialAdCommand { get; }
public MainViewModel()
{
ShowRewardedInterstitialAdCommand = ReactiveCommand.Create(ShowRewardedInterstitialAd);
}
private void ShowRewardedInterstitialAd()
{
var rewardedInterstitial = AdMob.Current.RewardedInterstitial.Create();
rewardedInterstitial.OnAdLoaded += (_, _) => rewardedInterstitial.Show();
rewardedInterstitial.OnUserEarnedReward += (_, reward) => Debug.WriteLine($"User earned reward: {reward.Amount} {reward.Type}");
}
Android
For Android on Avalonia 12+, no Activity is passed into .UseAdMob(). The library resolves the current Activity from lifecycle callbacks:
protected override AppBuilder CustomizeAppBuilder(AppBuilder builder)
{
return base.CustomizeAppBuilder(builder)
...
.UseAdMob(adMobOptions);
}
Events
| Event | Notes |
|---|---|
| OnAdLoaded | |
| OnAdFailedToLoad | |
| OnAdPresented | |
| OnAdFailedToPresent | |
| OnAdImpression | |
| OnAdClicked | |
| OnAdClosed | |
| OnUserEarnedReward | Contains a RewardItem record |
RewardItem
The reward item record is comprised of the following properties:
| Property | Description |
|---|---|
| Amount | The amount rewarded. |
| Type | The type of reward earned. |
Rewarded
| iOS | Android |
|---|---|
| <img alt="iOS Banner" src="img/iOS Rewarded Interstitial.jpeg" width="250" /> | <img alt="iOS Banner" src="img/Android Rewarded Interstitial.jpg" width="250" /> |
Interstitial ads can be used by calling Rewarded.Create(unitId) on the AdMob singleton.
This call will load and the return the interstitial ad with an OnAdLoaded event once it's finished loaded. .Show() can then be called to display the ad.
The reward event OnUserEarnedReward can then be used to know when a reward can be given to the user.
public ICommand ShowRewardedAdCommand { get; }
public MainViewModel()
{
ShowRewardedAdCommand = ReactiveCommand.Create(ShowRewardedAd);
}
private void ShowRewardedAd()
{
var rewarded = AdMob.Current.Rewarded.Create();
rewarded.OnAdLoaded += (_, _) => rewarded.Show();
rewarded.OnUserEarnedReward += (_, reward) => Debug.WriteLine($"User earned reward: {reward.Amount} {reward.Type}");
}
Android
For Android on Avalonia 12+, no Activity is passed into .UseAdMob(). The library resolves the current Activity from lifecycle callbacks:
protected override AppBuilder CustomizeAppBuilder(AppBuilder builder)
{
return base.CustomizeAppBuilder(builder)
...
.UseAdMob(adMobOptions);
}
Events
| Event | Notes |
|---|---|
| OnAdLoaded | |
| OnAdFailedToLoad | |
| OnAdPresented | |
| OnAdFailedToPresent | |
| OnAdImpression | |
| OnAdClicked | |
| OnAdClosed | |
| OnUserEarnedReward | Contains a RewardItem record |
App Open
| iOS | Android |
|---|---|
| <img alt="iOS Banner" src="img/iOS App Open.png" width="250" /> | <img alt="iOS Banner" src="img/Android App Open.jpg" width="250" /> |
App Open ads can be used by calling AppOpen.Create(unitId) on the AdMob singleton.
This call will load and the return the app load ad with an OnAdLoaded event once it's finished loaded. .Show() can then be called to display the ad.
public ICommand ShowAppOpenAdCommand { get; }
public MainViewModel()
{
ShowAppOpenAdCommand = ReactiveCommand.Create(ShowAppOpenAd);
}
private void ShowAppOpenAd()
{
var appOpenAd = AdMob.Current.AppOpen.Create();
appOpenAd.OnAdLoaded += (_, _) => appOpenAd.Show();
}
Android
For Android on Avalonia 12+, no Activity is passed into .UseAdMob(). The library resolves the current Activity from lifecycle callbacks:
protected override AppBuilder CustomizeAppBuilder(AppBuilder builder)
{
return base.CustomizeAppBuilder(builder)
...
.UseAdMob(adMobOptions);
}
Events
| Event | Notes |
|---|---|
| OnAdLoaded | |
| OnAdFailedToLoad | |
| OnAdPresented | |
| OnAdFailedToPresent | |
| OnAdImpression | |
| OnAdClicked | |
| OnAdClosed |
Preparing for Apple Privacy Manifests
I have yet to get around to testing any of this, but now the library is using a different iOS native binding package, the number of frameworks it depends on is significantly lower, and it should just be a case of updating to the latest when I find the time.
In the mean time, this project leverages NSUserDefaults and so should just require the reason code CA92.1:
<dict>
<key>NSPrivacyAccessedAPIType</key>
<string>NSPrivacyAccessedAPICategoryUserDefaults</string>
<key>NSPrivacyAccessedAPITypeReasons</key>
<array>
<string>CA92.1</string>
</array>
</dict>
You can read more about the Apple Privacy Manifest here.
Troubleshooting
The Google Mobile Ads SDK was initialized without AppMeasurement
If you receive an error similar to the following:
GADInvalidInitializationException Reason: The Google Mobile Ads SDK was initialized without AppMeasurement. Google AdMob publishers, follow instructions here: https://googlemobileadssdk.page.link/admob-ios-update-plist to include the AppMeasurement framework and set the -ObjC linker flag. Google Ad Manager publishers, follow instructions here: https://googlemobileadssdk.page.link/ad-manager-ios-update-plist
Try adding the following to your Info.plist:
<key>GADIsAdManagerApp</key>
<true/>
The current iOS bindings are a little out of date and are still using an older version of the Google Mobile Ads SDK. While the version is now no longer a sunset version by Google, this is actively being worked on here.
Avalonia 12.x Migration
If you are migrating an existing Avalonia 11 project, there are several required changes.
Update to Jc.AdMob.Avalonia >=5.x.x
The 5.0.0 release adds support for Avalonia 12.
Android Changes
Avalonia 12 changed Android startup. Your main activity must inherit from the non-generic AvaloniaMainActivity, and your app class must inherit from AvaloniaAndroidApplication<TApp>.
[Application]
public class AndroidApp : AvaloniaAndroidApplication<App>
{
protected AndroidApp(IntPtr javaReference, JniHandleOwnership transfer)
: base(javaReference, transfer)
{
}
protected override AppBuilder CustomizeAppBuilder(AppBuilder builder)
{
return base.CustomizeAppBuilder(builder)
...
.UseAdMob(new AdMobOptions
{
TestDeviceIds = [],
TagForUnderAgeOfConsent = false,
TagForChildDirectedTreatment = false,
AppId = "{YOUR_ADMOB_APP_ID}",
})
.AfterSetup(_ =>
{
AdMob.Current.Consent.OnConsentInitialized += (_, _) => AdMob.Current.Consent.ShowConsent();
});
}
}
[Activity(...)]
public class MainActivity : AvaloniaMainActivity
{
}
2) UseAdMob no longer takes Activity on Android
For Avalonia 12, call:
.UseAdMob(adMobOptions)
instead of:
.UseAdMob(this, adMobOptions)
The library now resolves the current Android Activity from lifecycle callbacks internally for fullscreen ad types and consent flows.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- Avalonia (>= 12.0.1)
NuGet packages (2)
Showing the top 2 NuGet packages that depend on Jc.AdMob.Avalonia:
| Package | Downloads |
|---|---|
|
Jc.AdMob.Avalonia.Android
Google AdMob for Avalonia on Android. |
|
|
Jc.AdMob.Avalonia.iOS
Google AdMob for Avalonia on iOS. |
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 5.0.0 | 99 | 11/20/2026 |
| 5.0.0-preview200 | 192 | 6/13/2026 |
| 5.0.0-preview100 | 144 | 4/21/2026 |
| 4.0.1 | 253 | 11/29/2025 |
| 4.0.1-preview100 | 199 | 11/29/2025 |
| 4.0.0 | 256 | 11/25/2025 |
| 3.3.0 | 483 | 11/18/2025 |
| 3.3.0-preview100 | 478 | 11/18/2025 |
| 3.2.0 | 328 | 9/14/2025 |
| 3.1.0 | 342 | 8/5/2025 |
| 3.0.2 | 301 | 4/22/2025 |
| 3.0.1 | 265 | 3/12/2025 |
| 3.0.0 | 235 | 2/16/2025 |
| 3.0.0-preview200 | 208 | 2/16/2025 |
| 3.0.0-preview100 | 210 | 2/16/2025 |
| 2.0.1 | 246 | 1/26/2025 |
| 2.0.1-preview400 | 185 | 1/21/2025 |
| 2.0.1-preview300 | 184 | 1/21/2025 |
| 2.0.1-preview200 | 192 | 1/21/2025 |
| 2.0.1-preview100 | 196 | 1/20/2025 |