Meghna Singh
Meghna Singh

Reputation: 11

In-app updates in Android using .net maui c#

I tried implementing in-app updates for android using this library <PackageReference Include="Xamarin.Google.Android.Play.Core" Version="1.10.3.15" />

and here is the code

using Xamarin.Google.Android.Play.Core.AppUpdate;
using Xamarin.Google.Android.Play.Core.Install.Model;
protected override void OnCreate(Bundle savedInstanceState)
{
    appUpdateManager = AppUpdateManagerFactory.Create(this);
    StartInAppUpdate();

    base.OnCreate(savedInstanceState);
}
private void StartInAppUpdate()
{
    var appUpdateInfoTask = appUpdateManager.AppUpdateInfo;
    appUpdateInfoTask.AddOnCompleteListener(new OnCompleteListener(task =>
    {
        if (task.IsSuccessful)
        {
            var appUpdateInfo = task.GetResult(Java.Lang.Class.FromType(typeof(AppUpdateInfo))) as AppUpdateInfo;
            if (appUpdateInfo != null)
            {
                if (appUpdateInfo.UpdateAvailability() == UpdateAvailability.UpdateAvailable && appUpdateInfo.IsUpdateTypeAllowed(AppUpdateType.Immediate))
                {                                        
                    appUpdateManager.StartUpdateFlowForResult(
                                appUpdateInfo,
                                AppUpdateType.Immediate,
                                this,
                                123);
                }
            }
        }
        else
        {
            System.Diagnostics.Debug.WriteLine("Failed to retrieve app update info.");
        }
    }));
}
class OnCompleteListener : Java.Lang.Object, Xamarin.Google.Android.Play.Core.Tasks.IOnCompleteListener
{
    private readonly Action<Xamarin.Google.Android.Play.Core.Tasks.Task> _callback;

    public OnCompleteListener(Action<Xamarin.Google.Android.Play.Core.Tasks.Task> callback)
    {
        _callback = callback;
    }

    public void OnComplete(Xamarin.Google.Android.Play.Core.Tasks.Task task)
    {
        _callback(task);
    }
}
    protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
        {
            base.OnActivityResult(requestCode, resultCode, data);
    
            if (requestCode == 123)
            {
                if (resultCode == Result.Ok)
                {
                    // Update was successful, notify the user or handle the result
                    Toast.MakeText(this, "Update complete!", ToastLength.Short).Show();
                }
                else
                {
                    // Update failed or was canceled, notify the user
                    Toast.MakeText(this, "Update failed or canceled.", ToastLength.Short).Show();
                }
            }
        }

I tried testing using Internal App Sharing, giving 2 builds, first version 1.0 and second as 2.0. Reference -> https://developer.android.com/guide/playcore/in-app-updates/test I installed 1.0 (using Internal App Sharing) and then I clicked on the app sharing url of version 2.0 but didn't install and went back to the 1.0 application (already installed one), killed and opened the app. The update dialog is not coming.

task.IsSuccessful is coming as false task.IsComplete is true

here is the exception Install Error(-3): The API is not available on this device. (https://developer.android.com /reference/com/google/android/play/core /install/model/Install ErrorCode#ERROR_API _NOT_AVAILABLE)

Upvotes: 1

Views: 197

Answers (1)

Jessie Zhang -MSFT
Jessie Zhang -MSFT

Reputation: 13919

You can troubleshoot the problem from the following aspects:

1.Please check the app you are testing has the same package name which is available on the play store.

2.Please check whether the signature between the two versions you have installed on the device and the one that Play Store would deliver.

Note:

To run it again, please clear the cache and storage of Play Store again.

Upvotes: 0

Related Questions