I want to have the abilities to update my app from my application, so I can download my apk file ( each of them are signed ). And when the installation want to start: First of all: I have preparation of installation popup which appear Then: another popup appear and told me "A problem occurred while parsing the package" My error is like this: So my app, didn't update. Could you help me please ?
I use Xamarin Forms but especially for Android.
Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.MyCompany.clamexdroid" android:installLocation="preferExternal">
<uses-sdk android:minSdkVersion="21" android:targetSdkVersion="31" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
<uses-permission android:name="android.permission.INSTALL_PACKAGES" />
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
<application android:label="ClamexDroid" android:theme="@style/MainTheme" android:icon="@mipmap/ic_launcher">
</application>
<queries>
<intent>
<action android:name="android.media.action.IMAGE_CAPTURE" />
</intent>
</queries>
</manifest>
My function when I click on my update Button
private async Task update()
{
this.popupIsVisible = false;
this.IsBusy = true;
string uri = "myLink.apk";
HttpClient cli = new HttpClient();
await cli.DownloadBasicFile(uri, DependencyService.Get<IDeviceDependency>().GetExternalAPKPath());
DependencyService.Get<IDeviceDependency>().InstallApk();
this.IsBusy = false;
}
And my InstallApk() function ( I'm sure that the file is downloaded )
public void InstallApk()
{
Intent promptInstall = new Intent(Intent.ActionView).SetDataAndType(
Android.Net.Uri.Parse($"content:///{this.GetExternalAPKPath()}"),"application/vnd.android.package-archive");
promptInstall.SetFlags(ActivityFlags.NewTask);
promptInstall.AddFlags(ActivityFlags.GrantReadUriPermission);
Android.App.Application.Context.StartActivity(promptInstall);
}
public string GetExternalAPKPath()
{
try
{
return Path.Combine(Android.App.Application.Context.GetExternalFilesDir("").AbsolutePath, "com.MyCompany.clamexdroid.apk");
}
catch (Exception e) { Console.WriteLine(e.Message); }
return "";
}
CodePudding user response:
At first, you can check the apk file you had downloaded has any damage. And then you can try to the following steps to install the apk:
1.Add the provider in the AndroidManifest.xml, such as:
<application android:allowBackup="true" android:icon="@drawable/androidicon" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme">
<provider android:name="androidx.core.content.FileProvider" android:authorities="${applicationId}.provider" android:exported="false" android:grantUriPermissions="true">
<meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/provider_paths" />
</provider>
</application>
2.Create the provider_paths.xml in the folder named xml below the values folder in the android part.
<paths >
<external-path
name="external_files"
path="." />
</paths>
3.Use the following code to install the apk:
var path = Path.Combine(Android.App.Application.Context.GetExternalFilesDir("").AbsolutePath, "com.MyCompany.clamexdroid.apk");
Java.IO.File file = new Java.IO.File(path);
Intent install = new Intent(Intent.ActionView);
Android.Net.Uri apkURI = FileProvider.GetUriForFile(this, this.ApplicationContext.PackageName ".provider", file);
install.SetDataAndType(apkURI, "application/vnd.android.package-archive");
install.AddFlags(ActivityFlags.NewTask);
install.AddFlags(ActivityFlags.GrantReadUriPermission);
StartActivity(install);