Install or Update Android App Programmatically

To Install or Update android app programmatically is very simple and easy to implement. Android application can be updated without going through Google Play Store. We can download the apk file from the server and install it programmatically. An apk file will be downloaded by using HttpURLConnection class within AsyncTask. Once download is completed we will use Implicit Intent to install and open the downloaded apk file. We will create a sample project to demonstrate updating android app programmatically.

Download Source Code

Update Android App Programmatically – Implementation

Create a new Project in Android Studio with default settings and name it UpdateTutorial.

Add the Internet and WRITE_EXTERNAL_STORAGE permission in the MeniFest file. Internet permission is required for making Http call. We will store the downloaded apk on externet storage therefore we are adding WRITE_EXTERNAL_STORAGE permission.

Create a basic layout for MainActivity. Add a TextView and Button in activity_main.xml file. We will display app version in TextView and Button will be used to start the update process.

activity_main.xml

Add the below code in your main activity file.

main_activity.java

As you see in the above code that we have created an AsyncTask sub class to make Http call to download the apk file.

Setting the URL of apk file. I have already uploaded the apk file on the server having a version no 2.

Making Http call using HttpURLConnect class

Create a Download directory on external storage.

Creating the instance of File class and delete app-debug.apk file if already exist.

We will read the data using InputStream class and write the data to the file by using FileOutputStream class. A stream is a sequence of data. InputStream is used for reading and OutputStream is used for writing.

After completing download and writing the data to the file we will use Implicit Intent to instal and open the downloaded apk file.

Now run the application. You will see the following output. I have already uploaded the apk file having version 2 now we will run the application with version 1.

update android-app-programitically

Update Android App Programmitically – Video Demo

I hope you will like this tutorial. Subscribe to our Newsletter to get new articles and tutorials in your inbox as soon as they published. You can connect with us on Facebook and also subscribe to our Youtube Channel.

Check Also

asynctask kotlin example

Kotlin AsyncTask Tutorial with Example

Here is an example of implementing AsyncTask in Kotlin. I will create an Android application …

  • Tiara Gizka Septi

    helloo…
    thanks for your article. but how to handle done button?

    • Kamran Nisar

      Hi Tiara,

      I don’t find any solution yet for handling “Done” button. I will update the article if find any solution.

      Thx
      Kamran

      • Tiara Gizka Septi

        Thanks for your reply.

        May i ask something else?

  • Bojan Ursus

    I’m getting parsing error on all devices I tried 🙁

    • Screenshot of your error?

      • Matej Lakota

        just message on screen and saying: “There was error parsing the package”

        • Bojan Ursus

          the same here, I tried on many different devices from android 4.something to android 7.something

          • Kamran

            Both apks should be signed with same certificate. app-debug.apk is signed by my certificate. you need to generate apks with your certificate.

          • Both apks should be signed with same certificate. app-debug.apk is signed by my certificate. you need to generate apks with your certificate. May be apk file is not uploaded properly to the server.
            also try this https://www.youtube.com/watch?v=_oPsC4Q_94c

          • Bojan Ursus

            Thanks, but I wasn’t using your .apk, I used your code, but implemented it for my own app so they both had the same certificate. I put my .apk on my server and I was able to install it manually (after downloading with chrome) but not with code provide here.

          • Did you test the my apk? is it working on your device? Install the apk from the link then try “Update Now”. Try changing the location of apk use another server.

    • Matej Lakota

      Me to

    • Both apks should be signed with same certificate. app-debug.apk is signed by my certificate. you need to generate apks with your certificate. May be apk file is not uploaded properly to the server.
      also try this https://www.youtube.com/watch?v=_oPsC4Q_94c