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.

Leave a Reply

6 comments

    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 🙂

  1. 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!)