A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
Hi @Vignesh Palthurai ,
I’ve tested this on my device and it works as expected. I have a few notes before sharing the code examples.
- I’m using the same version (4.0.0) for all three packages in the .csproj. I recommend doing the same.
<PackageReference Include="Plugin.Firebase" Version="4.0.0" />
<PackageReference Include="Plugin.Firebase.Analytics" Version="4.0.0" />
<PackageReference Include="Plugin.Firebase.Crashlytics" Version="4.0.0" />
- Because I encountered an SDK-related error, I changed the minimum Android API level from 21 to 23:
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'android'">23.0</SupportedOSPlatformVersion>
- You still need to keep strings.xml (
Platforms/Android/Resources/values/strings.xml), as mentioned earlier:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="com.google.firebase.crashlytics.mapping_file_id">none</string>
<string name="com.crashlytics.android.build_id">1.0</string>
</resources
- We must also include the following configuration in the .csproj file:
<ItemGroup Condition="'$(TargetFramework)' == 'net10.0-android'">
<GoogleServicesJson Include="Platforms\Android\google-services.json" />
</ItemGroup>
You can refer to following code examples:
This is MauiProgram.cs:
using Microsoft.Extensions.Logging;
using Microsoft.Maui.LifecycleEvents;
#if IOS || ANDROID
using Plugin.Firebase.Analytics;
using Plugin.Firebase.Crashlytics;
#endif
#if IOS
using Plugin.Firebase.Core.Platforms.iOS;
#elif ANDROID
using Plugin.Firebase.Core.Platforms.Android;
#endif
namespace MauiApp2;
public static class MauiProgram
{
public static MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp<App>()
.RegisterFirebaseServices()
.ConfigureFonts(fonts =>
{
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
});
#if DEBUG
builder.Logging.AddDebug();
#endif
return builder.Build();
}
private static MauiAppBuilder RegisterFirebaseServices(this MauiAppBuilder builder)
{
#if IOS || ANDROID
builder.ConfigureLifecycleEvents(events =>
{
#if IOS
events.AddiOS(iOS => iOS.WillFinishLaunching((_, _) =>
{
CrossFirebase.Initialize();
return false;
}));
#elif ANDROID
events.AddAndroid(android => android.OnCreate((activity, _) =>
{
CrossFirebase.Initialize(activity, () => Microsoft.Maui.ApplicationModel.Platform.CurrentActivity);
FirebaseAnalyticsImplementation.Initialize(activity);
}));
#endif
});
builder.Services.AddSingleton(_ => CrossFirebaseAnalytics.Current);
builder.Services.AddSingleton(_ => CrossFirebaseCrashlytics.Current);
#endif
return builder;
}
}
MainPage.xaml
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MauiApp2.MainPage">
<VerticalStackLayout Padding="30" Spacing="20" VerticalOptions="Center">
<Label Text="Firebase Test App" FontSize="Large" HorizontalOptions="Center" />
<Button Text="Log Analytics Event" Clicked="OnLogEventClicked" />
<Button Text="Force Test Crash (Crashlytics)"
Clicked="OnCrashClicked"
BackgroundColor="Red" TextColor="White" />
</VerticalStackLayout>
</ContentPage>
This is MainPage.xaml.cs:
using Plugin.Firebase.Analytics;
using Plugin.Firebase.Crashlytics;
namespace MauiApp2
{
public partial class MainPage : ContentPage
{
int count = 0;
public MainPage()
{
InitializeComponent();
}
private void OnLogEventClicked(object sender, EventArgs e)
{
CrossFirebaseAnalytics.Current.LogEvent("test_button_clicked",
new Dictionary<string, object> { { "button", "analytics_test" } });
DisplayAlert("Analytics", "Event sent to Firebase!", "OK");
}
private void OnCrashClicked(object sender, EventArgs e)
{
CrossFirebaseCrashlytics.Current.RecordException(new Exception("Test non-fatal exception"));
throw new Exception("This is a deliberate test crash for Crashlytics");
}
}
}
My .csprj file:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net10.0-android</TargetFrameworks>
<TargetFrameworks Condition="!$([MSBuild]::IsOSPlatform('linux'))">$(TargetFrameworks);net10.0-ios;net10.0-maccatalyst</TargetFrameworks>
<TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('windows'))">$(TargetFrameworks);net10.0-windows10.0.19041.0</TargetFrameworks>
<!-- Note for MacCatalyst:
The default runtime is maccatalyst-x64, except in Release config, in which case the default is maccatalyst-x64;maccatalyst-arm64.
When specifying both architectures, use the plural <RuntimeIdentifiers> instead of the singular <RuntimeIdentifier>.
The Mac App Store will NOT accept apps with ONLY maccatalyst-arm64 indicated;
either BOTH runtimes must be indicated or ONLY macatalyst-x64. -->
<!-- For example: <RuntimeIdentifiers>maccatalyst-x64;maccatalyst-arm64</RuntimeIdentifiers> -->
<OutputType>Exe</OutputType>
<RootNamespace>MauiApp2</RootNamespace>
<UseMaui>true</UseMaui>
<SingleProject>true</SingleProject>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<!-- Enable XAML source generation for faster build times and improved performance.
This generates C# code from XAML at compile time instead of runtime inflation.
To disable, remove this line.
For individual files, you can override by setting Inflator metadata:
<MauiXaml Update="MyPage.xaml" Inflator="Default" /> (reverts to defaults: Runtime for Debug, XamlC for Release)
<MauiXaml Update="MyPage.xaml" Inflator="Runtime" /> (force runtime inflation) -->
<MauiXamlInflator>SourceGen</MauiXamlInflator>
<!-- Display name -->
<ApplicationTitle>MauiApp2</ApplicationTitle>
<!-- App Identifier -->
<ApplicationId>com.companyname.mauiapp2</ApplicationId>
<!-- Versions -->
<ApplicationDisplayVersion>1.0</ApplicationDisplayVersion>
<ApplicationVersion>1</ApplicationVersion>
<!-- To develop, package, and publish an app to the Microsoft Store, see: https://aka.ms/MauiTemplateUnpackaged -->
<WindowsPackageType>None</WindowsPackageType>
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'ios'">15.0</SupportedOSPlatformVersion>
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'maccatalyst'">15.0</SupportedOSPlatformVersion>
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'android'">23.0</SupportedOSPlatformVersion>
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">10.0.17763.0</SupportedOSPlatformVersion>
<TargetPlatformMinVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">10.0.17763.0</TargetPlatformMinVersion>
</PropertyGroup>
<ItemGroup>
<!-- App Icon -->
<MauiIcon Include="Resources\AppIcon\appicon.svg" ForegroundFile="Resources\AppIcon\appiconfg.svg" Color="#512BD4" />
<!-- Splash Screen -->
<MauiSplashScreen Include="Resources\Splash\splash.svg" Color="#512BD4" BaseSize="128,128" />
<!-- Images -->
<MauiImage Include="Resources\Images\*" />
<MauiImage Update="Resources\Images\dotnet_bot.png" Resize="True" BaseSize="300,185" />
<!-- Custom Fonts -->
<MauiFont Include="Resources\Fonts\*" />
<!-- Raw Assets (also remove the "Resources\Raw" prefix) -->
<MauiAsset Include="Resources\Raw\**" LogicalName="%(RecursiveDir)%(Filename)%(Extension)" />
</ItemGroup>
<ItemGroup>
<None Remove="Platforms\Android\Resources\values\strings.xml" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Maui.Controls" Version="10.0.51" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="10.0.6" />
<PackageReference Include="Plugin.Firebase" Version="4.0.0" />
<PackageReference Include="Plugin.Firebase.Analytics" Version="4.0.0" />
<PackageReference Include="Plugin.Firebase.Crashlytics" Version="4.0.0" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net10.0-android'">
<GoogleServicesJson Include="Platforms\Android\google-services.json" />
</ItemGroup>
</Project>
After making all the changes, please clean and rebuild the project.
- Click Log Analytics Event, then check the Analytics dashboard to see the result.
- Click Force Test Crash, then restart the app and check the Crashlytics dashboard to see the crash report.
I hope this helps. Please give it a try and let me know whether it works. Thank you for your patience.