cordova-plugin-apkupdater
TypeScript icon, indicating that this package has built-in type declarations

5.0.0 • Public • Published

Cordova Apk Updater Plugin · License npm npm

Installation dialog

This plugin enables you to update your Android app completely without the Google Play Store.

👉 DEMO APP 👈



Plugin requirements

  • Android: 5+
  • cordova: 10.0.0+
  • cordova-android: 9.0.0+

Installation

Cordova

cordova plugin add cordova-plugin-apkupdater

Ionic + Cordova

ionic cordova plugin add cordova-plugin-apkupdater

Capacitor

npm install cordova-plugin-apkupdater

Basic example

Ionic 2+ with Typescript

Here is the simplest example: downloading and then installing the APK:

Sample Implementation:

import ApkUpdater from 'cordova-plugin-apkupdater';

export class HomePage {

    // .
    // .
    // .

    async update() {
        await ApkUpdater.download(
            'https://your-update-server.com/update.apk',
            {
                onDownloadProgress: console.log
            }
        );
        await ApkUpdater.install();
    }

}

(Complete code sample)

Cordova

The same example with callbacks:

ApkUpdater.download(
    'https://your-update-server.com/update.apk',
    {
        onDownloadProgress: console.log
    },
    function () {
        ApkUpdater.install(console.log, console.error);
    },
    console.error
);

API

The JavaScript API supports promises and callbacks for all methods.
The callback functions occupy the last two parameters.

// promise
await ApkUpdater.download('https://your-update-server.com/update.apk', options);

// alternative with callbacks
ApkUpdater.download('https://your-update-server.com/update.apk', options, success, failure);

On this page I only show Promise examples for simplicity.

In case of a failure you get an error object with two attributes: message and stack.

This example...

try {
    await ApkUpdater.download('https://wrong-url.com/update.apk');
} catch (e) {
    console.error(e.message + '\n' + e.stack);
}

... leads to the following output:

Download failed: Unable to resolve host "wrong-url.com": No address associated with hostname
de.kolbasa.apkupdater.exceptions.DownloadFailedException
  at de.kolbasa.apkupdater.downloader.FileDownloader.download(FileDownloader.java:111)
  at de.kolbasa.apkupdater.update.UpdateManager.downloadFile(UpdateManager.java:77)
  at de.kolbasa.apkupdater.update.UpdateManager.download(UpdateManager.java:133)
  at de.kolbasa.apkupdater.ApkUpdater.download(ApkUpdater.java:85)

download()

await ApkUpdater.download('https://your-update-server.com/update.apk', options);

You can also pass a zip file here. The zip file can even be encrypted with a password.
However, you should make sure that the archive contains only the APK file at root level, nothing else.
If you want to automate this, then you can also use my script.

Configuration (optional):

const options = {
    // ↓↓↓ If an encrypted zip file is used.
    zipPassword: 'aDzEsCceP3BPO5jy', 
    // ↓↓↓ Authorization token
    authorization: "Basic " + btoa('username:password'), // Example for basic access authentication
    onDownloadProgress: function (e) {
        console.log(
            'Downloading: ' + e.progress + '%',
            '(' + e.bytesWritten + '/' + e.bytes + ')'
        );
    },
    onUnzipProgress: function (e) {
        console.log(
            'Unzipping: ' + e.progress + '%',
            '(' + e.bytesWritten + '/' + e.bytes + ')'
        );
    }
}

If the download is successful, you will receive detailed information about the update file.

{
  "name": "update.apk",
  "path": "/data/user/0/de.kolbasa.apkupdater.demo/files/update",
  "size": 1982411,
  "app": {
    "name": "Apk Updater Demo",
    "package": "de.kolbasa.apkupdater.demo",
    "version": {
      "code": 10001,
      "name": "1.0.1"
    }
  }
}

stop()

Stops the download.

await ApkUpdater.stop();

getInstalledVersion()

Provides detailed information about the currently installed app version.

await ApkUpdater.getInstalledVersion();

Example output:

const result = {
    "name": "Apk Updater Demo",
    "package": "de.kolbasa.apkupdater.demo",
    "firstInstallTime": 1625415754434, // Unix timestamp
    "version": {
        "code": 10000,
        "name": "1.0.0"
    }
}

getDownloadedUpdate()

The downloaded update remains saved even after an app restart and can be queried as follows:

await ApkUpdater.getDownloadedUpdate();

The result uses the same format as the output from the download() method.


reset()

The reset method deletes all downloaded files.

It is mostly useful only for debugging purposes. The user himself has no access to the files.
The plugin deletes old updates automatically.

await ApkUpdater.reset();

install()

As soon as the download has been completed, you can use this method to ask the user to install the APK.

await ApkUpdater.install();

When the method is invoked for the first time, the user is asked to enable a setting for installing third-party applications (video).

You may want to ask the user for this permission before installing the first update.
The following two methods canRequestPackageInstalls and openInstallSetting are intended for this purpose.

canRequestPackageInstalls()

Queries whether the installation of updates has been allowed.

await ApkUpdater.canRequestPackageInstalls(); // -> true, false

openInstallSetting()

Opens the settings page (video).

await ApkUpdater.openInstallSetting(); // -> true, false

rootInstall()

If you have a rooted device, then you can even set up unattended app update installations (video).
If you want to test this on an emulator, I can recommend the following script: rootAVD

await ApkUpdater.rootInstall();

isDeviceRooted()

await ApkUpdater.isDeviceRooted(); // -> true, false

requestRootAccess()

Requests root access (video).

await ApkUpdater.requestRootAccess(); // -> true, false

ownerInstall()

Unattended updates can also be used by apps that are registered as device owners (video).

await ApkUpdater.ownerInstall();

Unlike root access, this can be easily set up on any Android device. Instructions can be found here.

isDeviceOwner()

await ApkUpdater.isDeviceOwner(); // -> true, false

Update versioning

The plugin itself does not make a version comparison.
You need to find a solution that best suits your use case.

If you always have Wi-Fi, you could download the entire apk at regular intervals and check whether the version has changed.
If you use mobile data, which is usually limited, then you should have a different strategy.
You may already have a remote API and can request the latest version there.

In my case, I simply place a small update.json file next to the update, which stores the latest version number.
I then simply compare this version with the internal one, which I request with the getInstalledVersion method.

This is also the case with the demo linked above.
Here is my script that I use to create the compressed update and info file.

Sample Implementation:

import ApkUpdater from 'cordova-plugin-apkupdater';

export class HomePage {

    // .
    // .
    // .

    remote = 'https://your-update-server.com'

    async update() {
        const manifest = await this.http.get<Update>(this.remote + '/update.json').toPromise();

        const remoteVersion = manifest.app.version.code;
        const installedVersion = (await ApkUpdater.getInstalledVersion()).version.code;

        if (remoteVersion > installedVersion) {
            await ApkUpdater.download(this.remote + '/update.apk');
            await ApkUpdater.install();
        }
    }
}

(Complete code sample)


License

MIT License

Copyright (c) 2021 Michael Jedich

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Dependents (2)

Package Sidebar

Install

npm i cordova-plugin-apkupdater

Weekly Downloads

149

Version

5.0.0

License

MIT

Unpacked Size

93.2 kB

Total Files

47

Last publish

Collaborators

  • kolbasa