Auto-updating Hybrid Applications
Auto-updating hybrid applications, also called “self-updating” or even “sideloading”, are a way to push changes out to all of your users without the need to download a new version from the app store. This approach can allow you to add new features, fix defects, or change assets very easily. Keep in mind that pushes to the app store may still be required if the native code changes. Self-updating applications require most of the code be written outside of native code and an interpreter be used to process it. WebViews are a common approach. It should be noted that it is against Apple’s TOS so your application may not be approved (See: Section 2.4.5). So far, my apps have not been rejected by Apple, but others have reported differently.
This article will focus on the technique using WebViews on the Android platform but the concept is easily translated to others. A link to the source code can be found at the bottom of the article.
There are two simple concepts to understand when building an auto-updating hybrid application: rendering the updates and downloading the updates.
The URL you initially load should be constant and should fall through to the “shouldInterceptRequest” method of the WebViewClient. In my experience, a normal “http” URL (e.g. “http://app/index.html”) is the only kind of URL that work. You could use HTTPS but that would add some complexity since you’ll have to deal with certificates.
The WebViewClient allows the code to “intercept” requests coming from the WebView and do something else instead. We will want to override “shouldInterceptRequest” and change it to check to see if the incoming URL’s host matches the intercepted host (e.g. “app”). If it matches, that’s when you trigger the interception logic. If it doesn’t match, let it fall through and be handled by the WebView itself.
The interception logic is very straightforward. The first thing that needs to be done is to see if the file exists in the “downloaded area” on the device. If it does exist, you open an InputStream to that file and pass it back. If it does not exist, you open an InputStream to the bundled asset using the Context’s AssetManager.
There are other approaches to auto-updating hybrid applications, but this is one of the most straightforward.
If you have implemented an auto-updating hybrid application let me know how you did it! Please share this article if you liked it, or let me know what I can do to improve!