c# – How to activate installation of an apk through my MAUI app


.Net 9.0 maui – 2026

AndroidManifest.xml

<!-- to App update apk -->
<provider
  android:name="androidx.core.content.FileProvider"
  android:authorities="com.companyname.xxxx.fileprovider"
  android:exported="false"
  android:grantUriPermissions="true">
  <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/file_path"/>
</provider>

add /Android/Resources/xml/file_path.xaml

<paths>  
    <files-path name="my_files" path="." />    
    <cache-path name="my_cache" path="." />   
    <external-path name="my_external" path="." />    
    <root-path name="root" path="." />
</paths>

add clsse /Android
//https://learn.microsoft.com/en-us/answers/questions/2141510/how-to-update-version-in-apk-maui-application-auto

public static class apkInstallHelper
{

     private const int RequestCode = 200;
       public static void InstallAPK(string filepath)
       {

        if (Build.VERSION.SdkInt >= BuildVersionCodes.R && !Platform.CurrentActivity.PackageManager.CanRequestPackageInstalls())

        {
            Intent intent = new Intent(Settings.ActionManageUnknownAppSources);

            intent.SetData(Uri.Parse("package:" + Platform.CurrentActivity.PackageName));

            Platform.CurrentActivity.StartActivityForResult(intent, RequestCode);
        }

        installApk(filepath);
    }

private static void installApk(string filepath)
{

        Java.IO.File file = new Java.IO.File(filepath);

        Intent intent = new Intent(Intent.ActionView);
        Uri apkUri = FileProvider.GetUriForFile(Platform.CurrentActivity, "com.companyname.micropresdev.fileprovider", file);

        intent.SetDataAndType(apkUri, "application/vnd.android.package-archive");
        intent.SetFlags(ActivityFlags.NewTask | ActivityFlags.GrantReadUriPermission);
        Platform.CurrentActivity.StartActivity(intent);
    }
}

go to update:

{
            string urlS3 = "https://your server/update.apk"; 
            string caminhoPrivado = Path.Combine(FileSystem.AppDataDirectory, "update.apk");

        try
        {
               using var client = new HttpClient();

   
               var data = await client.GetByteArrayAsync(urlS3);

              await File.WriteAllBytesAsync(caminhoPrivado, data);

              MicroPresDEV.Platforms.Droid.apkInstallHelper.InstallAPK(caminhoPrivado);
    


           }
            catch (Exception ex)
            {
               await Shell.Current.DisplayAlert("Erro S3", "Falha no download: " +  ex.Message, "OK");
         }


}

Leave a Reply

Your email address will not be published. Required fields are marked *