1. 程式人生 > >How To Update Android Apk Outside The Playstore

How To Update Android Apk Outside The Playstore

How to update android apk outside the playstore

Let’s learn how to update android apk outside the playstore in this short post! Perhaps you have a scenario where you don’t want to publish your app in the playstore. The reason behind your decision could be to have more control over who installs the app or even the size of the file itself among other things.

Without publishing your apps through the playstore however, you are left with the responsibility of updating your own apps, keeping track of total downloads, managing the app from start to finish, handling payments (for paid-for apps) among other good things that come with Google Playstore! In this post, I will show you how to update your apps outside the playstore system. That means the updates are pushed automatically to the users who currently have the app installed! Let us get started, shall we?

How to update android apk outside the playstore – Steps

  • You first upload your new apk to your own server somewhere in the north pole or your basement (if you still live with your parents and they have allowed you to have your server there)
  • Make an HTTP GET request to a URL that will return the data back to you for instance by visiting http://yoursite.com/apk

Show me the codez son…

For demonstrative purposes ONLY, I am going to use an AsyncTask in this post to show you how to update android apk outside the playstore. There are better ways to perform network operations in android like using Retrofit – my favorite library FYI! Let us take a look here:

ApkUpdateAsyncTask.java

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 publicclassApkUpdateAsyncTaskextendsAsyncTask{publicApkUpdateAsyncTask(){}protectedStringdoInBackground(String...urls){Stringpath=Environment.getExternalStorageDirectory()+"/awesomeapp.apk";//download the apk from your server and save to sdk card heretry{URL url=newURL(urls[0]);URLConnection connection=url.openConnection();connection.connect();// download the fileInputStream input=newBufferedInputStream(url.openStream());OutputStream output=newFileOutputStream(path);bytedata[]=newbyte[1024];intcount;while((count=input.read(data))!=-1){output.write(data,0,count);}output.flush();output.close();input.close();}catch(Exceptione){}returnpath;}@OverrideprotectedvoidonPostExecute(Stringpath){Process process=null;// call to superuser command, pipe install updated APK without writing over files/DBtry{process=Runtime.getRuntime().exec("su");DataOutputStream outs=newDataOutputStream(process.getOutputStream());Stringcmd="pm install -r "+path;outs.writeBytes(cmd+"\\n");}catch(IOExceptione){}}}

How to actually use this code

Once you have your AsyncTask cooked up and ready to serve, you can easily execute it like this:

1234567 Stringurl="http://myawesomesite.com/api/protected/apk";newApkUpdateAsyncTask().execute(url);

That is all you need to update your apk outside the playstore. Remember, you have to implement a way to check that the version codes differ before running updates. That means, you could check a value that gets updated whenever a new apk is uploaded remotely on your server to see if the version of the app has been updated, then make the second HTTP request to pull down the new apk file!

If you found this post helpful, please consider sharing with others using the buttons below and subscribing for more posts on different topics! Got questions? Comments? Suggestions? Let me know below!

Thanks again for reading my post – always stay awesome!