An Android Application that can update itself if new releases in GitHub are available.
Based on Implement an in-app update function by Tom Seifert.
Works on Android API 21 (5.0, Lollipop) and above (because of OkHttp). Your releases' tags must be the code version of the app and an APK must be in the binaries.
It shows a dialog with the changelog and the name of the last release if a new release is available.
If the user clicks OK :
- Android 7 and above: Downloads and installs the APK
- Below Android 7 : Downloads the APK, the user has to go to the notifications to install it
The dialog isn't shown if the release doesn't have an APK.
Add this in your gradle dependencies :
implementation 'com.squareup.okhttp3:okhttp:4.6.0'
implementation 'com.squareup.moshi:moshi:1.9.3'
implementation 'com.squareup.okio:okio:2.8.0'
In the Manifest, add these permissions in the manifest tag :
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
And add this provider in the application tag :
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_provider_paths" />
</provider>
Add the entirety of the package selfupdate
.
In the onCreate of your activity, add this to check if there is an update at the start of the application :
if (savedInstanceState == null)
SelfUpdate.checkUpdate(this, "burgyl", "SelfUpdatingApp");
or this if you support API before 21 :
if (savedInstanceState == null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
SelfUpdate.checkUpdate(this, "burgyl", "SelfUpdatingApp");
You have to adapt the above username and repository.
Add the layouts for the dialogs :
layout/content_dialog_download.xml
layout/content_dialog_show_update.xml
Add the values :
values/dimens.xml
values/strings.xml
And add the file provider paths :
xml/file_provider_paths.xml
- Moshi by Square Open Source, licensed under the Apache 2.0 license
- OkHttp by Square Open Source, licensed under the Apache 2.0 license
- Okio by Square Open Source, licensed under the Apache 2.0 license