Skip to content

Android library

You do not need a library to use android/apk/updater: the update check is one POST and a text reply. If you would rather not write the client yourself, there is an open-source one: nicolabeghin/android-apk-updater on GitHub (Apache-2.0).

It is a fork and rewrite of the Auto-Update-APK client, so it speaks the same protocol. It checks for an update, downloads the build, verifies it, and opens the installer.

Note

The current release is a pre-release. The reply parsing and MD5 checks have unit tests and the library builds, but the Android parts have not yet been run on a device. Try it on a test device first.

The steps at a glance

  1. Upload your app so the server knows it.
  2. Add the library to your app's Gradle build.
  3. Start the updater when your app starts.
  4. Handle the two permissions Android asks for.
  5. Ship that build by hand to your users, once.
  6. Release updates: raise the versionCode, upload, and the apps update themselves.

1. Upload your app

The server needs at least one build of your app before an update check can find anything.

  1. Sign up and open Releases, then Add APK.
  2. Upload the release APK, the file you ship to users. See Releases.
  3. Check that the package name and versionCode shown for it are the ones in your app.

Your server address is https://www.android-apk-updater.com, the root of the site without /check.

2. Add the library

The library is published through JitPack. In settings.gradle, add the repository:

dependencyResolutionManagement {
    repositories {
        google()
        mavenCentral()
        maven { url 'https://jitpack.io' }
    }
}

In your app module's build.gradle, add the dependency:

dependencies {
    implementation 'com.github.nicolabeghin:android-apk-updater:0.1.3'
}

You need minSdk 21 or higher and Java 8 language level. OkHttp and WorkManager come in with the library, and so do the permissions and the file provider it needs. There is nothing to add to your manifest. Check the releases page for the latest version.

Keep the library on its latest version. When Android changes how apps install or update (permissions, the installer, background work), we release a library update quickly so that updates keep working. That is all you have to do on your side: update the dependency when a new version comes out. See the FAQ.

If your app uses R8 or ProGuard, nothing needs to be kept: the library uses no reflection.

3. Start the updater

The simplest setup checks in the background and shows a notification when a new build has been downloaded. Do it once, in your Application class:

public class MyApp extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        new ApkUpdater.Builder(this)
                .baseUrl("https://www.android-apk-updater.com")
                .notificationIcon(R.drawable.ic_stat_update)   // optional, a monochrome drawable
                .build()
                .schedule(12, TimeUnit.HOURS);
    }
}

Register the class in AndroidManifest.xml with android:name=".MyApp" on the <application> element.

  • schedule is safe to call on every start: it replaces the existing schedule instead of adding another.
  • The interval must be at least 1 hour. A shorter one throws an IllegalArgumentException.
  • Scheduled checks run on Wi-Fi or other unmetered networks only. Call .wifiOnly(false) on the builder to allow mobile data. Checks you start yourself always run.
  • To use your own OkHttpClient (certificates, a proxy, logging), pass it with .okHttpClient(client).
  • cancelSchedule() stops the background checks.

Checking when you choose

To check right now, for example from an "Check for updates" menu item, and show your own interface:

ApkUpdater updater = new ApkUpdater.Builder(this)
        .baseUrl("https://www.android-apk-updater.com")
        .build();

updater.checkNow(new ApkUpdater.Listener() {
    @Override public void onChecking()    { /* show a spinner */ }
    @Override public void onNoUpdate()    { /* "You are up to date" */ }
    @Override public void onDownloading() { /* a newer build was found */ }
    @Override public void onReady(File apk) {
        startActivity(Installer.intentFor(MainActivity.this, apk));
    }
    @Override public void onError(IOException e) { /* e.getMessage() says what went wrong */ }
});

All five callbacks arrive on the main thread, and you only need to override the ones you use. When the build has downloaded, onReady gives you the file. The library has already checked it against its MD5, so a damaged download is reported through onError and never reaches onReady.

4. Handle the permissions

Android asks the user for two things before an app can update itself.

Install unknown apps (Android 8 and later). The user has to allow this for your app once. Until they do, Android ignores install requests without any message. Installer.intentFor(context, apk) takes care of it: it returns the installer when the permission is granted and the Settings page for it when it is not. The notification the library shows does the same. After the user has allowed it, they can tap the notification, or you can call checkNow again. The file is already downloaded and is reused, so nothing is fetched twice. Installer.canInstall(context) tells you which case applies, if you want to explain it first.

Notifications (Android 13 and later). Your app has to ask for POST_NOTIFICATIONS itself, for example in your main activity:

if (Build.VERSION.SDK_INT >= 33
        && checkSelfPermission(Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) {
    requestPermissions(new String[]{Manifest.permission.POST_NOTIFICATIONS}, 1);
}

Without it, the background check still downloads the update, but it cannot tell the user. checkNow does not need it.

The installer itself always asks the user to confirm. An update is never installed silently.

The notification text comes from five string resources. Define them in your app with the same names to translate or reword them: apkupdater_channel_name, apkupdater_ready_title, apkupdater_ready_text, apkupdater_permission_title and apkupdater_permission_text.

Warning

Do not ship this in a version of your app that is also published on Google Play. Play does not allow an app to update itself by any means other than Play. It is meant for apps distributed outside the Play Store.

5. Ship the first build by hand

The apps your users already have do not contain the library, so they cannot update themselves. Give them the first build that does, the way you distribute apps today, or with the stable link https://www.android-apk-updater.com/download/<your package name>, which always points at your newest build. Open it in the phone's browser and install the file.

From then on, that build checks for updates on its own.

If you are coming from Auto-Update-APK, this is the one build that points users at the new address. See Migrating from Auto-Update-APK.

6. Release an update

For every new version:

  1. Raise versionCode in your app's build.gradle. The server only offers a build whose versionCode is higher than the one installed.
  2. Build the release APK, signed with the same key as the version your users have. A different key makes Android refuse the update with "package conflicts with an existing package".
  3. Upload it in Releases. Upload an APK, not an app bundle (AAB).

Within the interval you chose, apps on Wi-Fi find it, download it, verify it, and show Update downloaded with a tap to install.

Test that it works

Before relying on it, check the server's answer for an older version of your app, from any computer:

curl -X POST https://www.android-apk-updater.com/check \
     -d pkgname=com.example.app -d version=1 -d md5=0

You should get have update and a link on the next line. no update means a condition on the update check is not met, most often that the uploaded versionCode is not higher than the version you sent.

Then, on a test device with an older build of your app installed, call checkNow. You should see the callbacks arrive in order, then the system installer.

When it does not work

What you see Likely cause
onError with "Unexpected reply from update server (is the base URL right?)" The server address is wrong, or it includes /check. Use the root address.
onNoUpdate although you uploaded a newer build The uploaded versionCode is not higher than the installed one, the package name differs, or your account has used its monthly download limit. See the FAQ.
onError mentioning HTTP 404 A required field is missing from the check. Use the library's own calls, not a hand-written request.
onError mentioning "download links expire" More than 20 minutes passed between the check and the download. Check again.
onError saying the download is corrupt The file was damaged on the way. It is deleted, and the next check fetches it again.
The notification never appears On Android 13 and later, your app has not been granted notifications. Check the permission in step 4.
Tapping the notification opens Settings The user has not allowed installing unknown apps for your app yet.
The installer says the package conflicts with an existing package The new APK is signed with a different key than the installed app.
Nothing happens in the background The device is not on an unmetered network and wifiOnly is on, or the interval has not elapsed yet.

Coming from Auto-Update-APK

Before After
aua = new AutoUpdateApk(getApplicationContext()); new ApkUpdater.Builder(ctx).baseUrl("https://…").build().schedule(12, TimeUnit.HOURS);
aua.setUpdateInterval(...) and addSchedule(...) The schedule(interval, unit) argument. WorkManager chooses the exact moment.
aua.enableMobileUpdates() .wifiOnly(false) on the builder. Wi-Fi only is the default.
aua.checkUpdatesManually() updater.checkNow(listener)
Status strings through Observable ApkUpdater.Listener callbacks

Delete the old AutoUpdateApk.java and its file provider entry. See Migrating from Auto-Update-APK for moving your users over.