Android Example Update APK

This is an example of updating app to newer version.
In real usage, the new apk should be downloaded from a server to File.DirInternal.

Note:
In this example, the apk file (version 1.01) which is compiled in project 2.zip is attached inside the assets folder of (version 1.00) of project 1.zip to simulate the situation.
The file 2.apk has some code commented and libraries removed to reduce file size for uploading to the forum.

GitHub: https://github.com/pyhoon/update-apk-b4a
 

Attachments

  • 1.zip
    215.5 KB · Views: 262
  • 2.zip
    40.3 KB · Views: 249
Last edited:

aeric

Expert
Licensed User
Longtime User
If it is still not obvious to you, you can modify the code as following:
B4X:
Private Sub BtnUpdate_Click
    If Application.VersionCode = latestVersion Then
        xui.MsgboxAsync("You already have the latest version", "")
        Return
    End If
    If File.Exists(File.DirInternal, FileToInstall) = False Then
        'File.Copy(File.DirAssets, FileToInstall, File.DirInternal, FileToInstall)
        Log("Downloading updates from remote server...")
        Dim job As HttpJob
        job.Initialize("", Me)
        job.Download("https://acme.com/files/2.apk") ' file uploaded to your server
        Wait For (job) JobDone(job As HttpJob)
        If job.Success Then
            Dim out As OutputStream = File.OpenOutput(File.DirInternal, FileToInstall, False)
            File.Copy2(job.GetInputStream, out)
            out.Close '<------ very important
            Log("Download complete")
        Else
            Log("Error: " & job.ErrorMessage)
        End If
        job.Release
    End If
    SendInstallIntent
End Sub
 

bskotu555

Active Member
If it is still not obvious to you, you can modify the code as following:
B4X:
Private Sub BtnUpdate_Click
    If Application.VersionCode = latestVersion Then
        xui.MsgboxAsync("You already have the latest version", "")
        Return
    End If
    If File.Exists(File.DirInternal, FileToInstall) = False Then
        'File.Copy(File.DirAssets, FileToInstall, File.DirInternal, FileToInstall)
        Log("Downloading updates from remote server...")
        Dim job As HttpJob
        job.Initialize("", Me)
        job.Download("https://acme.com/files/2.apk") ' file uploaded to your server
        Wait For (job) JobDone(job As HttpJob)
        If job.Success Then
            Dim out As OutputStream = File.OpenOutput(File.DirInternal, FileToInstall, False)
            File.Copy2(job.GetInputStream, out)
            out.Close '<------ very important
            Log("Download complete")
        Else
            Log("Error: " & job.ErrorMessage)
        End If
        job.Release
    End If
    SendInstallIntent
End Sub
After running the code, no installation window popped up。。。
1746328151697.png
 

aeric

Expert
Licensed User
Longtime User
You can also improve it further.

Example like adding a button to check for available version update.

It can also automatically check when the app is launched.

Remember the last time it was prompt and ignored so it won't pops up everytime.

When an update is available, show the label.

The checking can be query from a version control database table or just from reading a text file by calling httputils download.
 

Sabotto

Well-Known Member
Licensed User
Hi @aeric

I'm really enjoying your nice and simple app update routine. My question is: Is there a way to know if the user has answered "Install" or "Cancel" to the message that Android automatically generates (asking whether to install the update) when the SendInstallIntent sub finishes? Thanks
 

aeric

Expert
Licensed User
Longtime User
Hi @aeric

I'm really enjoying your nice and simple app update routine. My question is: Is there a way to know if the user has answered "Install" or "Cancel" to the message that Android automatically generates (asking whether to install the update) when the SendInstallIntent sub finishes? Thanks
Please start a new thread for your question.
I am not familiar with intent.
Maybe you can try to search for StartActivityForResult.
 

aeric

Expert
Licensed User
Longtime User
Code updated on GitHub to use OkHttpUtils2 to download the apk from an URL.
 
Top