1

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!

nicetomitja
  • 145
  • 10
  • 1
    It is complicated. You will need a lot of platform specific code. And depending on the version, you need different code. For example 24+ does not allow just file url, you need file provider. Android 10 you can walk away with "use legacy storage" permission. 11+, you need permission that Google allow on their store, only for specific list of app. Also you will need install apk permission. Every android version makes changes, that forces me to walk over this code and fix it. For "security" reasons. – H.A.H. Nov 18 '22 at 15:15
  • Are there somewhere tutorials for develping this? Where can I find a code example for this. If it is clear, which setting for which android version, i will implement. But I need to know how to do :( – nicetomitja Nov 21 '22 at 08:41
  • Did you check the official document about [updating in apps](https://developer.android.com/guide/playcore/in-app-updates)? – Liyun Zhang - MSFT Nov 23 '22 at 09:57
  • @nicetomitja Tutorials - no. Search "programmatically install APK", and combine some of the answers. I warn you again - every version I have to tweak this a little. It may not be the best choice for your app. – H.A.H. Nov 24 '22 at 05:38
  • Thank you @LiyunZhang-MSFT. But there is no MAUI/.NET alternative, it's just Java, isn't it? – nicetomitja Nov 24 '22 at 11:54
  • @H.A.H. Do you have any other ideas? How can I make it so that I can run the software on a device and update itself if there is a new version. How can I represent this if I have an APK file installation and do not publish the app in the PlayStore? – nicetomitja Nov 24 '22 at 11:54
  • Make .NET server application that will act as store for you. First controller is used to answer for the current version, of the application you request. Then, if there is difference, ask the user if he wants to download the new version. Get zip archive with apk and patch notes. Show patch notes. And last - ask the user to install the new application. This is how I do things. – H.A.H. Nov 25 '22 at 07:14
  • 1
    You can check the [answer about install the apk](https://stackoverflow.com/a/72441693/17455524) in your app. – Liyun Zhang - MSFT Nov 25 '22 at 09:24
  • Thank you, @LiyunZhang-MSFT! This is exactly what I looked for. I will try this later and come back if it worked as expected. – nicetomitja Nov 28 '22 at 06:55
  • Did it work for you? – Liyun Zhang - MSFT Nov 29 '22 at 05:16
  • @LiyunZhang-MSFT: I tried to implement, but there are some errors in MAUI. [Screenshot](https://imgur.com/r9VZsbI) The "StartAcitivity" method is unknown and the Context & Autority parameter has to be set in another way. – nicetomitja Nov 30 '22 at 07:00
  • Did you put the code in the mainactivity? – Liyun Zhang - MSFT Nov 30 '22 at 07:05
  • Give me some time, I changed the code so that I do not have any compiling errors now. – nicetomitja Nov 30 '22 at 07:17
  • Is there an option to avoid the fact, that the user has to allow "install unknown apps" from the source? In addition I have the problem that I get the message "app is not installed.". I have uninstall the application manually, than the apk works. Is this possibly because I have not incremented the version number? Or do I always have to uninstall the application first and then reinstall it? – nicetomitja Nov 30 '22 at 13:17
  • That's the system dialog which is used to prevent mistakenly uninstall. When you install the same app which has a new version on your device, you need to uninstall the old version at first. – Liyun Zhang - MSFT Dec 01 '22 at 09:16
  • Do you have any idea how to achieve this (uninstallation of app within app)? – nicetomitja Dec 01 '22 at 11:07
  • You can check this link which is [about uninstall the app in it](https://stackoverflow.com/questions/60716057/uninstall-application-programmatically-android-10). – Liyun Zhang - MSFT Dec 02 '22 at 03:12
  • Thank you! But there is again compiling errors in MAUI: [Screenshot](https://imgur.com/a/GHWul4b) – nicetomitja Dec 02 '22 at 09:34

2 Answers2

0

You can uninstall you app by the following code:

var name = this.ApplicationContext.PackageName;
Intent intent = new Intent(Intent.ActionDelete);
intent.SetData(Android.Net.Uri.Parse("package:" + name));
StartActivity(intent);

In addition, don't forget to add the permissions in the AndroidManifest.xml:

<uses-permission android:name="android.permission.DELETE_PACKAGES" />
<uses-permission android:name="android.permission.REQUEST_DELETE_PACKAGES" />
Liyun Zhang - MSFT
  • 8,271
  • 1
  • 2
  • 14
0

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" />
nicetomitja
  • 145
  • 10