B4A Library AppUpdating - automate apps updating from a webserver

Status
Not open for further replies.
Important note: since all the changes made to the Android OS the original code is to be considered outdated. Thanks to @Yayou49 you may find at post#290 a version of the class usable on Android 6+ versions.

Note: version 1.25 breaks existing code so, please, read changelog in post #60 below.


Hi all,

this is my first contributing lib to this great NG.
How do I dare to publish such a low-grade stuff in an area where generally you may find real masterpieces of programming? Well, to be honest, I hope in your help to upgrade my work to a status of "ready-made solution" for those who prefer to run their own webservers where to publish apps.
Secondly, I was asked (both privately and publicly) to overcome my legitimate embarassment and go on with the publication. So, here we are: AppUpdating 1.18!

Note: any valid code fragment comes from some other thread, while all the bad stuff is entirely mine ;-)

Please find attached both the library as is (yes, it works out of the box) and its source code.

On a following post I'm gonna describe the lib in greater detail and show how to use it.

Umberto

AppUpdating version history

* version 1.30 (read notes on post #228)
  • compiled with okHttp 1.01 and okHttpUtils 2.20
  • removed files for version 1.25

* version 1.26 (read notes on post #84)
  • added property WebChangeLog to read optional app's version changelog data
  • better management of versioning in the info file
  • removed files for version 1.18 to avoid confusion (still available on request)
* version 1.25
* version 1.18
  • initial release
 

Attachments

  • AppUpdating_126_ex.zip
    28.3 KB · Views: 1,102
  • AppUpdating_126.zip
    11.3 KB · Views: 1,126
  • AppUpdating_126_src.zip
    10.8 KB · Views: 890
  • AppUpdating_130.zip
    11.4 KB · Views: 1,125
  • AppUpdating_130_src.zip
    15.7 KB · Views: 1,035
  • AppUpdating_130_ex.zip
    33.4 KB · Views: 1,229
Last edited:

DonManfred

Expert
Licensed User
Longtime User
UDG today releases a new version of the lib. See Post #1 for new version and Code of Example.

Here is a working APK using AppUpdating 1.25 Release.

Install this apk to your device and this one can update itself. It is a precompiled example with lib 1.25 (#1)
 

Attachments

  • AppUpdateExample.apk
    180.5 KB · Views: 245

udg

Expert
Licensed User
Longtime User
Due to significant differences with previous version, please have a look at this post and eventually at the example code as published in post#1
AppUpdating 1.25 - how to use it
As usual with any contributed library, download its zip (AppUpdating_125.zip, you need just this one), unzip it and place both files (.xml and .jar) in your additional libraries path (see menu option "Tools/Configure paths" in your B4A installation).

Now, for any app that you'd like to use this lib with, go to its Manifest editor ("Project/Manifest Editor") and add the following code:
B4X:
AddReceiverText(eu.dgconsulting.appupdating.newinst2,
  <intent-filter>
  <action android:name="android.intent.action.PACKAGE_REPLACED" />
    <data android:scheme="package" />
  </intent-filter>)

In your app's Main you have to:

0. Put a version number in "#VersionName:" key in Module Attributes. As an example: #VersionName:1.14

1. dim a variable to access the lib's class; do this in Sub Globals
B4X:
Sub Globals
  ... 'your code here
  Dim apkupdt As cl_appupdate
End Sub

2. Add code to Activity_Create to access any desired function in the lib
B4X:
Sub Activity_Create(FirstTime As Boolean)
   If FirstTime Then
     ... 'any specific code for your app initialization
   End If
   Activity.LoadLayout("Main")
   .... 'more stuff
   If FirstTime Then
     apkupdt.Initialize(Me,"sep")                   'sep stands for SingleExitPoint but you can use any name you like
     apkupdt.Verbose = True  'this one affects the verbosity of the logs
     'this is yor app's package name (see "Project/Package name")
     apkupdt.PackageName = "com.test.myapp"
     'this is the complete path to the text file holding the newer version number
     apkupdt.NewVerTxt = "http://umbetest.web44.net/p_apk/myapp.txt"
     'this is the complete path to your newer apk
     apkupdt.NewVerApk = "http://umbetest.web44.net/p_apk/myapp.apk"
     'set credentials only if the above files are stored in a protected folder; otherwise skip following line
     setCredentials("test","test")  'needed since folder p_apk has access restrictions
     'use only if you like to show a splash screen while update checking goes on
     apkupdt.SetAndStartSplashScreen(Activity,LoadBitmap(File.DirAssets, "myapp.png"))
     'this function does all the work in a single step: check, download and install
     apkupdt.UpdateApk                                                
  End If
End Sub

3. Receive feedback from the lib
B4X:
Sub sep_UpdateComplete
   apkupdt.StopSplashScreen
   Log("Status: "  & apkupdt.Status)
   If apkupdt.Status = apkupdt.OK_INSTALL Then
       Log("myapp's user was asked to install the newer copy of that app")
   End If
End Sub
Note: sub name above is made up by a first part ("sep") choosen by you (see apkupdt.Initialize in Activity_Create) and a second mandatory substring ("_UpdateComplete") as coded in the class part of the lib.

That's all for blindingly using the lib as is (well, that's all for the coding aspect of it..)!

AppUpdating - set the webserver
This one is easy.
On a webserver (even any free shared service would be ok), create a folder (in the example above, I created folder p_apk on webserver umbetest.web44.net) and eventually restrict access to it (I used the .htaccess method).
Prepare a text file named after your apk (in the example above, you may find myapp.apk and myapp.txt) having one single line of text as follows:
ver=x.yz
where x.yz is the newer apk version.
I improved a little bit my lazy scheme from previous version. Now the lib expects a single row formatted as follows:
<whatever label you like>=<version number> (e.g. version=1.50, ver=1.50, myapp=1.50..).
What really matters is that the version code strictly follows the equal sign (no spaces allowed)
Then, using any FTP client, copy both your newer apk's version and its corresponding text file in the folder set before.

That's all.

What the lib does is simply to check whether the version written in the text file is greater then the one showed in the running copy of your app.
If it finds indication of a newer version, it downloads it, then ask the user to install it.
Since we can't know if the user agrees to update the app, we simply go on with our app, knowing that a service burned in the lib will fire when the OS signals that the user accepted to install the newly downloaded copy of the app. In this latter case, the same service will reload the app when ready.

A final note about versions compare. Since versions are recorded as strings (both in #VersionName and in text file row) and currently there's no conversion to a numeric data type, you are adviced to use a coherent scheme (e.g 1.00 to 1.20 is ok while 1.11 to 1.2 is not).

Umberto
 
Last edited:

TMa

Member
Licensed User
Longtime User
"If apkupdt.Status = OK_INSTALL Then"
error:
undeclared variable 'ok_install' is used before it was assigned any value
.... Cannot compile ?
 

TMa

Member
Licensed User
Longtime User
before? but that "apkupdt.Initialize(Me,"update")" is allready in create part .
I have read that example many many times... maybe it's me, not instructions ;)

If I edit that like it is in post #1
"if akpdt.Status = 6 then"

Then it skips error and move to next error
("src\AKHP\OY\keskukset.java:268: error: package eu.dgconsulting.appupdating does not exist
public eu.dgconsulting.appupdating.newinst2 _newinst2 = null;
^
1 error)"
 
Last edited:

udg

Expert
Licensed User
Longtime User
@TMa, @DonManfred: sorry guys, code in post #62 is wrong.

The line causing the error should read
B4X:
If apkupdt.Status = apkupdt.OK_INSTALL Then
since the constant is defined in the class part of the library.
I'm gonna edit post #62 right now. A more complete "_UpdateComplete" callback function could be find in the demo attached at post#1.
For anybody convenience I copy here a slightly modified version of it:
B4X:
Sub sep_UpdateComplete
  apkupdt.StopSplashScreen
   'too lazy to manage error conditions..check apkupdt.ERR_xxx codes if you like
   Select apkupdt.Status
    Case apkupdt.OK_CURVER
       Log("Running apk version: " & apkupdt.CurVN)
    Case apkupdt.OK_WEBVER
       Log("Webserver apk version: " & apkupdt.WebVN)
    Case apkupdt.OK_NEWERAPK
       Log("Newer version available")
    Case apkupdt.NO_NEWERAPK
       Log("No newer version available")
    Case apkupdt.OK_DOWNLOAD
       Log("Newer version downloaded")
    Case apkupdt.OK_INSTALL
       Log("User asked to install newer version")
    Case Else
       Log("Status: "&apkupdt.Status)
   End Select  
End Sub
This is useful mainly if you call AppUpdating functions one by one. The "do it all in one step" function UpdateApk just returns OK_INSTALL or indication of an ERROR condition.

Umberto
 

TMa

Member
Licensed User
Longtime User
Thanks :)
Still getting this
("src\AKHP\OY\keskukset.java:268: error: package eu.dgconsulting.appupdating does not exist
public eu.dgconsulting.appupdating.newinst2 _newinst2 = null;
^
1 error)"

can't understand why because that "keskukset" isnt even the MAIN module where this
uppdate code is

That code "eu.dgconsulting.appupdating" is in the manifest file
 
Last edited:

udg

Expert
Licensed User
Longtime User
Hi TMa,

I'm not so expert to help you with this, but have a look at thist thread. Maybe it can be of some help.

Umberto
 

udg

Expert
Licensed User
Longtime User
Hi tuhatinhvn,

AppUpdating library will never conflict with Google's policies simply because you use AppUpdating for those apps you store and distribute out of the Google Play world.
Publish your app on your own server and AppUpdating will help to keep your users aligned with most recent app's version.
Publish your app on Google's store and Google will take care of announcing any new version you will then publish.

Umberto
 

kiki78

Active Member
Licensed User
Longtime User
Hi udg,

Thank you for your great library :)
Is there a way to automatically restart application after updating ?

Regards
 

udg

Expert
Licensed User
Longtime User
Hi kiki78,

sorry it took me so long to reply but I was away from my dev PC and couldn't check the code.
AppUpdating 1.25 introduced a service in the lib that listen for a broadcast message (Package Replaced) and then reloads the app, so I'm inclined to say that the library shoud be able to restart the app after the user has opted for the newer version's installation.
Do you experience a different behaviour?

Umberto
 
Last edited:

inakigarm

Well-Known Member
Licensed User
Longtime User
Hi Umberto:

is there a simple way to have a change log history versions on the txt file accessing from your lib?
(Maybe i'll have to access with hhtputils to this file and read the content)

This way, you can inform the user about the changes in the new version apk and let the user make his choice.

Thanks
 

udg

Expert
Licensed User
Longtime User
Hi inakigarm,

giving a rapid look at the code I don't think it'll be at all difficult to integrate what you're asking for.
As is, the code simply reads the Job.GetString and looks for an equal sign to determine the version number (everything following the equal sign..).
So, to not break existing code, we could modify sub ExtractVN to better identify the version number and introduce a new sub ExtractChangeLog that will return what you deserve.
What do you think?

Umberto
 

inakigarm

Well-Known Member
Licensed User
Longtime User
Thanks, it would be very useful !!
One solution could be insert the last chanqes in the txt between an # character (or any other character) and ExtracChangeLog would get the string between #
(This way, we can use the txt as a changes history and display only de last change version)
 

udg

Expert
Licensed User
Longtime User
Do you mind if I take care of this on next weekend?
I am finishing a new app which needs a couple of days more of refining..

Umberto
 

Douglas Farias

Expert
Licensed User
Longtime User
@udg
i have this error

Module: eu.dgconsulting.appupdating.newinst2_br not found.


when i add this


AddReceiverText(eu.dgconsulting.appupdating.newinst2,
<intent-filter>
<action android:name="android.intent.action.PACKAGE_REPLACED" />
<data android:scheme="package" />
</intent-filter>)

why? *-*
 
Status
Not open for further replies.
Top