3

I've inherited an old Xamarin project, that includes Xamarin.Insights, and have just tried updated all the packages. Now it won't build because the of the following problem:

`Xamarin.Insights.Initialize(string)' is obsolete: `Insights must be initialized from a platform specific assembly'

I'm running the Xamarin.Insights.Initialise("xxx"); in the Main() method of the Application class, just like it says to on the instructions.

Any help would be greatly appreciated.

Mick Byrne
  • 14,394
  • 16
  • 76
  • 91

1 Answers1

2

EDIT:

Xamarin.Insights will provide exception reporting from your core project without an explicit reference, but if you are doing any explicit Insights reporting calls in the core project, you'll leave that reference alone.

Insights did change a while back to have platform-specific initialization though, as the error indicates.

If you add a reference to your Xamarin.iOS and Xamarin.Android projects, things should work as before, once you move the Insights initialization into each platform-specific startup.

Here is an App class from a Xamarin.iOS project that shows the Xamarin.Insights initialization:

public class App
{
    private static void Main(string[] args)
    {
        // Initialize metrics and crash tracking.
        Xamarin.Insights.Initialize(Forms.App.XamarinInsightsApiKey);

        // Launch UI.
        UIApplication.Main(args, null, "AppDelegate");
    }
}

And the Xamarin.Android version:

[Application]
public class MainApplication : Application, Application.IActivityLifecycleCallbacks
{
    #region Constructors

    public MainApplication(IntPtr handle, JniHandleOwnership transer)
      :base(handle, transer)
    {
        // Do not remove this work-around for a Xamarin linker issue related to SSL/TLS certificate trust. This line of
        // code prevents the linker from stripping out AES capabilities needed but not obvious because they are accessed
        // via reflection. Issue https://bugzilla.xamarin.com/show_bug.cgi?id=13998.
        // ReSharper disable once UnusedVariable
        var b = new System.Security.Cryptography.AesCryptoServiceProvider();

        NotificationToken = null;
    }

    #endregion Constructors

    #region Methods

    public override void OnCreate()
    {
        base.OnCreate();
        RegisterActivityLifecycleCallbacks(this);

        // Initialize metrics and crash tracking.
        Xamarin.Insights.Initialize(Forms.App.XamarinInsightsApiKey, ApplicationContext);
    }
Mark Larter
  • 2,343
  • 1
  • 27
  • 34
  • 1
    You don't need to remove it from your Core project (as you can still report from your core). You just need to do the initialization in the platform specific projects. – Johan May 16 '16 at 09:33
  • @Johan Good point! I took this from an app where we aren't using Insights explicitly. I'll edit my answer, thanks for the comment. – Mark Larter May 16 '16 at 13:07
  • Thanks for the comprehensive answer - however, I'm definitely trying to get the Insights working from an iOS specific library, not in a PCL library (or even a shared project). Seems to only be an issue with the latest package 1.12.3 (was working before I upgraded). Weirdly, there's another (older) version of Xamarin.Insights available through the NuGet package manager, 1.8.3, which works just fine. – Mick Byrne May 17 '16 at 00:09
  • Strange. I've confirmed we're using 1.12.3 in this app from which I pulled the above. For grins, I added Xamarin.Insights reference to the PCL core project, and copied the Insights.Initialize call into the Forms App constructor, and got exactly the build error you report. Is is possible there is a stray Insights.Initialize "hiding" in a PCL project in your solution? – Mark Larter May 17 '16 at 13:58
  • Thanks for the follow up Mark. I've since discovered that the app builds using 1.8.3 but hangs for ever on the `Initialize()` method call. There's definitely no stray call in a PCL project. My suspicion is there's something awry with the configuration of this project (it's quite old, we've just inherited it) that is confusing the Insights code. If I ever discover it, I'll post an answer. – Mick Byrne May 18 '16 at 00:03