Update and Install Your Android App From Your Own Server Programmatically

In this post, we will see how to update and install your android app from your own server. I will discuss only important coding part. Suppose you are working in small organisation with member of 30 to 50 members. All members using  the company’s app. It’s not a good way to upload your app on Google Play Store for targeting only 30 to 50 devices. Also you have to pay money to Google play store to upload your app. You can use Google play Store if you are targeting large users to use your app. But , if you are working with small team then you have to use your own server if you want to save your money !

Simple logic and steps involved to update and install your android app from your own server:

  1. You must have your own server.
  2. Create HTTP connection between app client and server.
  3. Compare version of app client and and available app on server.
  4. If the version of app on server is higher then show alert popup message on client app to download and install latest app.

We will see here all steps in detail:

MainActivity.java :

I have created object ‘alarm’ of class SampleAlarmReceiver. It is used to check new version of app available or not on server. We are running this service for every 5 minutes.

SampleAlarmReceiver.java:

SampleBootReciver.java:

This BroadcastReceiver automatically restarts the alarm when the device is rebooted.

SampleSchedulingService.java:

onReceive SampleSchedulingService will start. Here it is the actual code :

Versioning is important to maintain your app upgrade strategy. BuildConfig.VERSION_CODE will show the version code of your app. You can see in build.grandle:

On every update you have to increase the  value of versionCode in build.grandle. Higher number indicates more recent version of app.

We are sending this version number using HTTP POST method to server to compare latest version availability on server. Here HttpURLConnection is used to perform HTTP data request and response. If the response string value is higher than current versionCode then new activity(ShowNote.java) will open using Intent.

ShowNote.java:

In above code , if latest update available on server then it will show AlertDialog. If user clicks on OK then it will download and installs the app. I will not discuss whole code . Code is itself explanatory.

AlertDialog After latest version available on server
AlertDialog After latest version available on server

Also I have mentioned rooting concept here in above code. Rooting means your device will allow to access to change  the software code on the device or install other software(specially custom ROM) that the manufacturer wouldn’t normally allow you to. Please don’t try to root your device . It may harm your device. Generally people root their device to upgrade latest android OS(marshmallow ,nougat or other activity on device).

Come on coding part, if your device is rooted then app will download and update automatically in background.

Above command will execute to direct installation of app without taking extra permissions from user.

If your device is not rooted then it will show to user a installation dialog with app permissions like normal app installation.

App Installation Request on non-rooted device
App Installation Request on non-rooted device

version.php :

Put your latest updated app on same directory of server.Note that you have to  increment versionCode on client app after each update.

Create a text file updateVersion.txt on same server directory. Write here same versionCode that you already mentioned on updated  app. I have created this file only send HTTP response to app client in JSON format.

It is simple way to Update and Install Your Android App From Your Own Server Programmatically. Later you can add your own functionality to track total downloads, how many user installed latest version. But you have to create a web interface to show all data on web page. Use any language like PHP, Node.js or Python to create web application to track this all record. If you have any question then don’t forget to comment below.

 

Edit:

AndroidManifest.xml

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *

22 comments

  1. HI its very nice example and thanks for it , i have folowing error
    java.lang.SecurityException: No permission to write to /storage/emulated/0/Download/user_manual.apk: Neither user 10159 nor current process has android.permission.WRITE_EXTERNAL_STORAGE.

    the reson must be on android sdk 25 that i am using, but i am very new in android world and struggling to implement run time permissions inside your example. meybe can you give me some tips or show me example with run time permissions with your code if you can ,
    Tanks

    1. When you update app then change ‘versionCode’ in build.grandle with higher integer number than previous value. Upload your app on server. Put version.php and updateVersion.txt file on same directory. Now change the versionCode in updateVersion.txt. Just enter integer number that matches with your updated version of app. for example see here http://prntscr.com/lthir0

      1. Thank you so much for the swift response.
        I did what you suggested. But, I run version.php on my xampp to see the output… I get an error “Trying to get property of non-object in version.php on line 5”

        1. :You will get ‘Notice’ if you are running code on local XAMPP. In this case just put @ symbol on line 5 : e.g:
          @$version=$obj->{‘version’};

          If you try code on actual server then it will work perfectly.

  2. Please Show me the Manifest File. Showing some errors.

    Caused by: java.lang.IllegalArgumentException:
    Component class com.vr.appupdate.SampleBootReciver does not exist in com.vr.appupdate
    at android.os.Parcel.readException(Parcel.java:2009)
    at android.os.Parcel.readException(Parcel.java:1951)
    at android.content.pm.IPackageManager$Stub$Proxy.setComponentEnabledSetting(IPackageManager.java:4785)
    at android.app.ApplicationPackageManager.setComponentEnabledSetting(ApplicationPackageManager.java:2299)
    at com.vr.appupdate.SampleAlarmReceiver.setAlarm(SampleAlarmReceiver.java:43)
    at com.vr.appupdate.MainActivity.onCreate(MainActivity.java:13)

    My Manifest file:

    1. Hello Tuna,

      Currently I don’t have separate source code. It’s working code. I tried in simple way how implement logic in this post. Please understand above steps, logic, code and build your ‘own’ code 🙂

  3. Strange thing :/ I’ve added everything to project, all this java files are in same package as rest of app files. But when I try to run on emulator app crashes instantly with exception java.lang.RunetimeException (IllegalArgumentException: Component class (myComponentName).SampleBootReceiver does not exist in (myComponentName).
    When I comment out in my MainActivity line ‘alarm.setAlarm(this);’ my app is working fine (as it should because no new code was invoked).
    Any idea why I got this ‘does not exist’ error ? File is exist, it’s name is correct..

    1. Hey, You must add section receiver to manifest file with name .SampleBootReceiver. (Look at dot at filename first character!)

          1. Hi There Sir Thank you so much for you efforts it is great to work through and understand how everything is working, I am having a little issue in my logcat i get the following

            04-18 21:03:12.649 26199-26240/com.angama_2019 W/System.err: java.net.UnknownHostException: Unable to resolve host “myserver.co.za”: No address associated with hostname

            Do you maybe know why this would be I have implemented all the code correctly with no errors but i am still not able to get update I am surely missing something obvious!

            Thank you for your time in advance!

          2. Hi There again Sir, I figured out it was a problem with the emulator and the wifi but have now encountered the following error
            java.lang.NumberFormatException: Invalid int: ” 1″

            No matter what i change the number to in the updateVersion.txt it still complains… Any thoughts would b appreciated