androidmauiauto-update

MAUI: Update own application (Android)


How can I check my own app if there is a newer version available and then update itselfs automatically? The idea is to request an API for a new version and receives the apk file. Then the APK should be stored and the installation/update should start. I don't know how to create this. The request to the API is no big deal, but how can I store and execute the APK file on an Android device?

I only found this but there were too many compiling errors (Context, Java, Buld, Intent, FileProvider could not be found).

Can you gave me some hints how to solve this "problem"?

THANK YOU!


Solution

  • It is not necessary to uninstall the app. If it is build with the same keystore and signature, it is possible to install one version directly to overwrite the existing.

    var context = Android.App.Application.Context;                
    var path = Path.Combine(Android.App.Application.Context.GetExternalFilesDir("").AbsolutePath, "com.companyname.Testproject.apk");
    Java.IO.File file = new Java.IO.File(path);
    
    using (Android.Content.Intent install = new Android.Content.Intent(Android.Content.Intent.ActionView))
    {
         Android.Net.Uri apkURI = AndroidX.Core.Content.FileProvider.GetUriForFile(context, context.ApplicationContext.PackageName + ".provider", file);
         install.SetDataAndType(apkURI, "application/vnd.android.package-archive");
         install.AddFlags(Android.Content.ActivityFlags.NewTask);
         install.AddFlags(Android.Content.ActivityFlags.GrantReadUriPermission);
         install.AddFlags(Android.Content.ActivityFlags.ClearTop);
         install.PutExtra(Android.Content.Intent.ExtraNotUnknownSource, true);
    
         Platform.CurrentActivity.StartActivity(install);
    }
    

    In the AndroidManifext.xml i added:

    <uses-permission android:name="android.permission.INSTALL_PACKAGES" />
    <uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
    <uses-permission android:name="android.permission.REQUEST_DELETE_PACKAGES" />
    <uses-permission android:name="android.permission.DELETE_PACKAGES" />