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,195
  • AppUpdating_126.zip
    11.3 KB · Views: 1,259
  • AppUpdating_126_src.zip
    10.8 KB · Views: 1,014
  • AppUpdating_130.zip
    11.4 KB · Views: 1,229
  • AppUpdating_130_src.zip
    15.7 KB · Views: 1,144
  • AppUpdating_130_ex.zip
    33.4 KB · Views: 1,353
Last edited:

MarcoRome

Expert
Licensed User
Longtime User
Hi Marco,
the version currently published is compiled with the Httputils2's release available BEFORE okHttp introduction.
I'm planning a newer version as soon as I have some spare time.
Thank you for your interest in AppUpdating lib.

udg

Mhh.. ok after different try this is result.

1.If you compile with this combination ( okHttp + HttpUtils 2.10 Library ) you havent error.
But when start App the value apkupdt.ReadWebVN return "" ( if you see in debug you have: sending message to waiting queue of uninitialized activity (submitjob))

2. If you take AppUpdateExample compile without problem but when start you have this error ( if you have new library HttpUtils 2.10 in folder library) :
B4X:
Installing file.
Installing file.
PackageAdded: package:b4a.example.appupdate
maininitializeProcessGlobals (java line: 609)
java.lang.NoClassDefFoundError: anywheresoftware.b4h.okhttp.OkHttpClientWrapper
    at anywheresoftware.b4a.samples.httputils2.httputils2service._process_globals(httputils2service.java:144)
    at b4a.example.appupdate.main.initializeProcessGlobals(main.java:609)
    at b4a.example.appupdate.main.afterFirstLayout(main.java:98)
    at b4a.example.appupdate.main.access$000(main.java:17)
    at b4a.example.appupdate.main$WaitForLayout.run(main.java:80)
    at android.os.Handler.handleCallback(Handler.java:733)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:146)
    at android.app.ActivityThread.main(ActivityThread.java:5692)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
    at dalvik.system.NativeStart.main(Native Method)

So with new library okHttp + HttpUtils 2.10 dont work
 

MarcoRome

Expert
Licensed User
Longtime User
Ok. Resolve i recompile the library with the new version HttpUtils 2.10 + okHttp.
Tried and tested.
You no longer need to eliminate from the libraries as HttpUtils2 see in picture.

Screenshot 2015-08-31 15.15.25.png


Dont forget this combination ( okHttp + HttpUtils 2.10 Library ).

In attachment Rel. 1.28 ( AppUpdating1.28-okHttp.zip )

Thank's again for this library ( the library 1.28 is only ricompilate, nothing in source is modified, so the source 1.26 present in 1# is same source )
 

Attachments

  • AppUpdating1.28-okHttp.zip
    12 KB · Views: 485
Last edited:

nibbo

Active Member
Licensed User
Longtime User
Just to confirm that I have tried the latest version (1.28) and it works perfectly.
Thanks udg for this lib.
 

Harris

Expert
Licensed User
Longtime User
Can I use the code module source without having to use the lib for AppUpdating?
I have modified Http2Utils.bas source to handle an error that sometimes occurs in my app - so I cannot use the lib (un-modified).

Sometimes for some unknown reason, the JobDone fails after I have transferred records to server for insertion. They get posted on server but my app cannot mark these as SENT. So next attempt will include already posted records and a Key Violation results... The mod below corrects this with the FixDupe method where the response_err is the primary key of the record that needs to be updated to SENT...

B4X:
Sub CompleteJob(TaskId As Int, success As Boolean, errorMessage As String)
    Dim job As HttpJob
    job = TaskIdToJob.Get(TaskId)
    TaskIdToJob.Remove(TaskId)
    job.success = success
    job.errorMessage = errorMessage
    job.Complete(TaskId)

' my mod....

    If job.success = False Then
       Log(" Job Name from HTTP: "&job.JobName&"  Tag: "&job.Tag&" err: "&errorMessage)
       If job.Tag = "AddLogs" Then
          Log("Response error message: "&resposne_err)
          CallSubDelayed2(  LogServmod,"FixDupe",resposne_err)
       End If
    Else
         ' ToastMessageShow(" Job was completed: "&TaskId,False)    

    End If 
     
End Sub
 

udg

Expert
Licensed User
Longtime User
Hi Harris,

you can use any part of the lib's code to best suit your needs. That's why I published the source code!

udg
 

Harris

Expert
Licensed User
Longtime User
Awesome!

I am sure it will go smoothly after all the effort you, with others help, put into it.
This will be a great way to keep the devices up to date without user interaction AND without requiring Play Store.

In fact, with my newest contract, internet nor cell exist! Remote Alaska where even WiFi using Access Points shall be challenging...
Have no fear - B4A and Android are here!


Thanks
 

BarryW

Active Member
Licensed User
Longtime User
AppUpdating - how to use it
As usual with any contributed library, download its zip (AppUpdating_118.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,"tab1")                   'tab1 has no meaning; use any name you like
     'this is yor app's package name (see "Project/Package name")
     apkupdt.PackageName = "eu.dgconsulting.tmb"
     'this is the complete path to the text file holding the newer version number  
     apkupdt.NewVerTxt = "http://umbetest.web44.net/p_apk/tmb.txt" 
     'this is the complete path to your newer apk
     apkupdt.NewVerApk = "http://umbetest.web44.net/p_apk/tmb.apk"
     apkupdt.UserName = "test"         'needed since folder p_apk has access restrictions
     apkupdt.UserPassword = "test"    'as above
     'not needed- just to show use of function GetCurVN
     Log("I am version: " & apkupdt.GetCurVN)
     'use only if you like to show a splash screen while update checking goes on
     apkupdt.SetAndStartSplashScreen(Activity,LoadBitmap(File.DirAssets, "tmb0.png"))
     'this function starts checking for any newer version of the current app
     apkupdt.LookForNewVersion                                                      
  End If
End Sub

3. Receive feedback from the lib
B4X:
Sub tab1_UpdateComplete
   If apkupdt.IsDone Then
     Log("New Ver available: " & apkupdt.NewVerAvailable)
     Log("Status: "  & apkupdt.Status)
     apkupdt.StopSplashScreen
     If apkupdt.Status = 6 Then
       Log(apkupdt.PackageName)
     End If
  End If
End Sub
Note: sub name above is made up by a first part ("tab1") 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 tmb.apk and tmb.txt) having one single line of text as follows:
ver=x.yz
where x.yz is the newer apk version. Due to my laziness, currently the lib takes for granted that the version number starts on the 5th char..
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.

Umberto

Can install new apk version in silent mode (not manuall install after download). Tnx
 

udg

Expert
Licensed User
Longtime User
Hi Barry,
unfortunately, AFAIK, silent operation is possible only on rooted devices.

udg
 

Harris

Expert
Licensed User
Longtime User
Just finished implementing using source code.

Works GREAT ! ! ! Automatically updating the apk is HUGE!!! This is awesome....

Now all I have to do it create a record with app version and a report to ensure all devices are using the latest version - and NOT pressing the cancel button when asked to install...

Thanks so much for this. I shall donate soon...
 
  • Like
Reactions: udg

udg

Expert
Licensed User
Longtime User
Hi BarryW,

I generally use 000webhost to experiment a bit with site-related stuff. Last year I also bought a Raspberry box and since then most in-house testing is done on it.

udg
 

Harris

Expert
Licensed User
Longtime User
Want your own server for cheap?

Check out:
https://www.weloveservers.net/?utm_...m=email&utm_campaign=name_super_fast_vps_sale

I have seven of these servers, for 19 bucks a year per each!


This one is for 6 bucks per year... (six bucks - now that's just stupid....)

10 GB Disk Space Storage
1TB Bandwidth (100Mbps)
128MB Guaranteed RAM + 256 MB vSwap
1 IPv4 Address
SolusVM Access
Full Root Access
Choice of Linux OS's
Self Managed
OpenVZ Virtualization Technology

Locations: Los Angeles, Dallas, Buffalo
Powered by: 4x Enterprise-grade SATA Drives (Hardware RAID 10)

Warwound recommenced these guys and their service is pretty good...
Having a server has taught me alot about Linux, web, php and so much more...

PM me your email and I will forward the latest offering from WeLoveServers...
 
Last edited:

nibbo

Active Member
Licensed User
Longtime User
OK, I thought this was working but it is only doing so for me.
When the users attempt to install the update they get 'Error: Not Found'
The apk is there and it must have access as the ReadWebVN returned the latest version which is in the same server directory.
Both the txt and apk files have full control permissions. The MIME type is set u OK.

Any ideas?
 

udg

Expert
Licensed User
Longtime User
Hi Nibbo,

a first test could be to try the download directly from the browser.
This is useful to discover eventual problems with filename not exactly matching or permissions/mime types.

Since ReadWebVN returns the correct values, that means it can read the txt definition file. Next step should be call DownloadApk with same string but the extension changed from txt to apk. Do you show/log any error message?

If you want me to have a look at your code, PM me at any time.

udg
 

nibbo

Active Member
Licensed User
Longtime User
Hi Nibbo,

a first test could be to try the download directly from the browser.
This is useful to discover eventual problems with filename not exactly matching or permissions/mime types.

Since ReadWebVN returns the correct values, that means it can read the txt definition file. Next step should be call DownloadApk with same string but the extension changed from txt to apk. Do you show/log any error message?

If you want me to have a look at your code, PM me at any time.

udg

Thanks for the tip... I typed in the URL to the apk in the tablet's browser and was able to download the file OK.
The problem was the website is SSL and I had missed 's' from the https part of the URL. I think it is OK now.
Thanks for your time.
 

Harris

Expert
Licensed User
Longtime User
Just donated to your excellent efforts.
Keep up the great work!

Thanks
 

udg

Expert
Licensed User
Longtime User
Hi Harris,
thank you very much. A few donations more like yours and I'll have a ticket paid to Alaska. Is it a wonderful place to visit, right? A lot of years ago I had a flight-stop in Anchorage and so had the opportunity to spend a few hours there.
Glad to hear that your RD project is going well.

Umberto
 

Harris

Expert
Licensed User
Longtime User
Made it to Red Dog today. Flew from Anchorage on a 737-400. Tech Cominco charters with Alaskan Airways each Wed and Sat. Plane is always full. -5C at 71.5 latitude in the arctic - not bad really. Some snow in forecast.
Quite a big operation built on the permafrost. Everything is on steel piles driven deep into the ground. Concrete costs $1000 per yard (at this very remote location).

Over the next week I shall get much accomplished.
Need to create Geozones in critical areas on the haul road, establish wifi AP locations, install a couple test units in trucks and take runs with driver's to get their interpretation of what is being implemented for the fuel, ore (lead / zinc) and freight hauls - then adjust my activities to what REALLY goes on as opposed to what I have been told to date...

This is sent by the sat connection they installed in the camp. Slow but gets the job done... I can even connect to my VPS website (tripinspect.com) to send an receive test data we shall create (since the local server is not installed here yet).

My gain a few pounds since meals are a full buffet for breakfast, lunch and dinner. All you can eat of great healthy food - for free! These guys at RD have it good... Average low end wage is 100 G per year US. Work shifts are 4 week in - 2 week out ( or 2 in - 1 out ).

Wish me luck...

Thanks
 
  • Like
Reactions: udg

opus

Active Member
Licensed User
Longtime User
Hi, thanks for this good lib., it took me just a couple of houres to make it work.
However, I'm running into a problem, how the check for webserver not online or folder/files not on server WITHOUT showing an error message.
So far I'm using the .UpdateAPK method. When running the code without the webserver being online, an error message is shown even before I can check the .Status which will be -100 by then.
Edit:
Found the reason! The error message is a ToastMessage from the JobDone in the Lib. So I would only need this ToastMessage commented out. But, I've never compiled a Lib...Searching.
.... If everything in live would be that easy. Found everything, even the newly compiled lib-files. Problem solved!
 
Last edited:
  • Like
Reactions: udg

Anser

Well-Known Member
Licensed User
Longtime User
Hi,

Its a great lib. Thanks for this wonderful lib.

I just came across a bug in this lib. I am not sure whether this is a bug or is it something wrong in the way that I am doing.

If I call ApkUpdt.CurVN , it will never reach the status ApkUpdt.OK_CURVER in the Sub AppUpdateCheck_UpdateComplete, instead the app goes into an infinite loop.

The above call ie ApkUpdt.CurVN is supposed to return the Current version of the Apk running on the phone

Rest of the methods in this lib are working fine for me.

By the way, a suggestion from my side. When we call the ApkUpdt.DownloadApk / ApkUpdt.UpdateApk instead of showing a png/jpg picture saying that the Apk is downloading, it would be better if you can show the actual download progress using some progress bar, so that the user knows that the download is happening and the user is aware of the actual progress of the download process.

Regards
Anser
 
Status
Not open for further replies.
Top