PatriceVB
PatriceVB

Reputation: 1083

How to implement Google Play In-App Update and use Play Core Library with Xamarin

Google provides a new way to implement in-app update : https://developer.android.com/guide/app-bundle/in-app-updates that can be used to force users to update their app with the latest version available.

To be able to use this new feature, it requires the use of Play Core Library 1.5.0 or higher : https://developer.android.com/guide/app-bundle/playcore

It is possible to use it from Xamarin and to implement in-app update with this new capability ?

Regards

Upvotes: 4

Views: 2727

Answers (3)

Konstantin S.
Konstantin S.

Reputation: 1505

For MAUI, everything is now implemented in one line:

  • Add NuGet package to your project:
<PackageReference Include="Oscore.Maui.Android.InAppUpdates" Version="1.0.0" />
  • Add the following to your MauiProgram.cs CreateMauiApp method:
builder
    .UseMauiApp<App>()
    .UseAndroidInAppUpdates()
    .ConfigureFonts(fonts =>
    {
        fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
        fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
    });

Sources: https://github.com/oscoreio/Maui.Android.InAppUpdates

Upvotes: 1

CloudArch
CloudArch

Reputation: 341

This answer is for new developers of Xamarin Android as i was looking for solution and couldn't find anywhere so implemented it for C# based on java implementation suggested on android forums. Here is my example - install the Google play core library Xamarin.GooglePlayServices.Base from nuget manager. And add the namespaces to the MainActivity.cs or Any activity where you want to check for updates :

   using Com.Google.Android.Play.Core.Appupdate;
   using Com.Google.Android.Play.Core.Tasks;
   using Com.Google.Android.Play.Core.Install.Model;

Create a variable for IAppUpdateManager in your activity and Implement the IOnSuccessListener on that activity which has the method OnSuccess as under:

public void OnSuccess(Java.Lang.Object p0)
        {
            if ((p0 as AppUpdateInfo).UpdateAvailability() == UpdateAvailability.UpdateAvailable && (p0 as AppUpdateInfo).IsUpdateTypeAllowed(AppUpdateType.Immediate))
            {
                appUpdateManager.StartUpdateFlowForResult(p0 as AppUpdateInfo, this, AppUpdateOptions.NewBuilder(AppUpdateType.Immediate).Build(), TaskId);
            }
        }

You can change the AppUpdateType to FLEXIBLE instead of Immediate if that's your requirement. And check for update as under wherever you wish to in your activity.

appUpdateManager = AppUpdateManagerFactory.Create(this);
         // Returns an intent object that you use to check for an update.
        var appUpdateInfoTask = appUpdateManager.AppUpdateInfo;
        if (appUpdateInfoTask != null)
        {
            //Register the listener and you can check for the response of this listener at OnSuccess method implemented.
            appUpdateInfoTask.AddOnSuccessListener(this);
        }

There are more options available in the library that you can check here and implement as needed. Remember it is Java or Kotlin but above example will help for C# implementation : https://developer.android.com/guide/playcore/in-app-updates/kotlin-java#java

Upvotes: 1

FreakyAli
FreakyAli

Reputation: 16572

UPDATE: CHECK https://github.com/PatGet/XamarinPlayCoreUpdater

I was able to come up with the Xamarin binding for the play core library which can be found on my drive here

Will be packaging it to NuGet soon, will update here once I do

Upvotes: 2

Related Questions