Easy to use abstraction over Android packages install and uninstall functionality leveraging Kotlin coroutines (API 16+)

Overview

SimpleInstaller

Build Status Maven Central

Overview

SimpleInstaller is an Android library which provides easy to use abstraction over Android packages install and uninstall functionality leveraging Kotlin coroutines.

It supports Android versions starting from 4.1 Jelly Bean. Package installer provides a Kotlin SharedFlow property which can be collected for installation progress updates. Split packages installation is also supported (note that this is only available on Android versions starting from 5.0 Lollipop).

SimpleInstaller was developed with background execution in mind. You can launch an install or uninstall session from background, for example, while foreground service is running and your application was removed from recents. The way it works is that the user is shown a high-priority notification which launches a standard Android confirmation by clicking on it, or full-screen intent with confirmation (depends on firmware).

Gradle dependencies

All versions are available here.

implementation("io.github.solrudev:simpleinstaller:x.y.z")

Replace "x.y.z" with the latest version.

Usage

First, you need to initialize SimpleInstaller. To do this, add the following line to your Application class' onCreate() method:

SimpleInstaller.initialize(this)

Optionally, you can provide an icon for SimpleInstaller notifications:

SimpleInstaller.initialize(this, R.drawable.your_notification_icon)

Installation

There are two methods for installation, each of them receives either Uri, AssetFileDescriptor, File or custom ApkSource. URIs must have file: or content: scheme.

suspend fun PackageInstaller.installPackage(apkFile): InstallResult
@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
suspend fun PackageInstaller.installSplitPackage(vararg apkFiles): InstallResult

These methods return an InstallResult object, which can be either Success or Failure. Failure object may contain a cause of failure in its cause property.

They also have non-suspend variants which accept PackageInstallerCallback callback as a second parameter.

fun PackageInstaller.installPackage(apkFile, callback: PackageInstallerCallback)
@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
fun PackageInstaller.installSplitPackage(vararg apkFiles, callback: PackageInstallerCallback)

Install permission

On Oreo and higher PackageInstaller sets an install reason PackageManager.INSTALL_REASON_USER, so on first install there should be a prompt from Android to allow installation. But relying on this is not recommended, because your app will be restarted if user chooses Always allow, so the result and progress won't be received anymore (this is the case for MIUI). There's InstallPermissionContract in activityresult package which you should use to request user to turn on install from unknown sources for your app.

In your Activity:

val requestInstallPermissionLauncher = registerForActivityResult(InstallPermissionContract()) { booleanResult ->
    if (booleanResult) { /* do something */ }
}

...

requestInstallPermissionLauncher.launch(Unit)

ApkSource

SimpleInstaller provides an abstract ApkSource class with the following public interface:

val progress: SharedFlow<ProgressData>
abstract val length: Long
abstract fun openInputStream(): InputStream?
abstract suspend fun getUri(): Uri
open fun clearTempFiles()

SimpleInstaller has implementations for Uri, AssetFileDescriptor and File. You can provide your own implementation and pass it to PackageInstaller.installPackage or PackageInstaller.installSplitPackage.

Uninstallation

suspend fun PackageUninstaller.uninstallPackage(packageName: String): Boolean

Returns true if uninstall succeeded, false otherwise.

This method also has non-suspend variant which accepts PackageUninstallerCallback callback as a second parameter.

fun PackageUninstaller.uninstallPackage(packageName: String, callback: PackageUninstallerCallback)

Java interoperability

SimpleInstaller.initialize() is available statically and has a separate overload with notification icon resource ID as a parameter.

There are static methods in PackageInstaller and PackageUninstaller which accept callbacks as a second parameter. They are wrappers around Kotlin suspend functions. Callbacks have the following interfaces:

interface PackageInstallerCallback {
    fun onSuccess()
    fun onFailure(cause: InstallFailureCause?)
    fun onException(exception: Throwable)
    fun onCanceled()
    fun onProgressChanged(progress: ProgressData)
}
interface PackageUninstallerCallback {
    fun onFinished(success: Boolean)
    fun onException(exception: Throwable)
    fun onCanceled()
}

hasActiveSession property can be accessed statically:

PackageInstaller.getHasActiveSession()
PackageUninstaller.getHasActiveSession()

Current install/uninstall session can be canceled through a static cancel() method.

Sample app

There's a simple sample app available. It can install chosen APK file and uninstall an application selected from the installed apps list. Go here to see sources.

License

License

You might also like...
An easy-to-use wrapper for Lunar Client's game api.

An easy-to-use wrapper for Lunar Client's game api.

Endoscope lets you to stream live video between android devices over Wi-Fi! 📱📲
Endoscope lets you to stream live video between android devices over Wi-Fi! 📱📲

Endoscope - RTSP live video streamer for android devices via Wi-Fi. Project is no longer supported. Alternative solution is under development. Stay tu

An simple image gallery app utilizing Unsplash API to showcase modern Android development architecture (MVVM + Kotlin + Retrofit2 + Hilt + Coroutines + Kotlin Flow + mockK + Espresso + Junit)
An simple image gallery app utilizing Unsplash API to showcase modern Android development architecture (MVVM + Kotlin + Retrofit2 + Hilt + Coroutines + Kotlin Flow + mockK + Espresso + Junit)

Imagine App An simple image gallery app utilizing Unsplash API. Built with ❤︎ by Wajahat Karim and contributors Features Popular photos with paginatio

SimpleTodo - An android app that allows building a todo list and basic todo items management functionality
SimpleTodo - An android app that allows building a todo list and basic todo items management functionality

Pre-work - SimpleTodo SimpleTodo is an android app that allows building a todo l

A stock market app , with cached , search functionality , and market overview in the form of graph statics
A stock market app , with cached , search functionality , and market overview in the form of graph statics

A stock market app , with cached , search functionality , and market overview in the form of graph statics. The graph really looks cool (jetpack compose, SOLID priciples).

 🍲Foodium is a sample food blog Android application 📱 built to demonstrate the use of Modern Android development tools - (Kotlin, Coroutines, Flow, Dagger 2/Hilt, Architecture Components, MVVM, Room, Retrofit, Moshi, Material Components).
The Odysee Android app with wallet functionality

Odysee Android Release To create an APK file which can be installed on real devices, you will need to create a digital signature and then edit app/bui

The News App has been carried out within the framework of the MVVM architecture, information about news is obtained by consulting an API, it is built usisng Jetpack Copose, Coroutines, Dependency Injection with Hilt and Retrofit

Journalist The News App consists of an application that displays the latest news from EEUU from an API that provides official and updated information.

An app which displays questions from Stack Exchange from it's api. Can search questions with tags as well. Uses MVVM architecture, dependency injection, coroutines, retrofit2 for network calls

Stack Exchange app What the app does? Shows a list of trending questions from stack exchange api Can search for the desires question. Can add tags to

Comments
  • 3.0.0

    3.0.0

    Breaking API changes (for Java client code only):

    • PackageInstaller and PackageUninstaller are interfaces now, so to use these classes one must obtain a singleton instance through getInstance() static method
    • Callbacks are now inner interfaces of PackageInstaller and PackageUninstaller
    • In order to use PackageInstallerHelper methods a PackageInstaller instance must be passed as the first parameter
    opened by solrudev 0
Releases(4.2.2)
  • 4.2.2(Dec 14, 2022)

    • Fix issues with coroutine cancellation (e.g. if uninstallation coroutine was canceled, next uninstallPackage() call will hang indefinitely even after session is completed).

    Full Changelog: https://github.com/solrudev/SimpleInstaller/compare/4.2.1...4.2.2

    Source code(tar.gz)
    Source code(zip)
  • 4.2.1(Nov 7, 2022)

    • Remove unnecessary reflection when trying to get application label of an APK to install.

    Full Changelog: https://github.com/solrudev/SimpleInstaller/compare/4.2.0...4.2.1

    Source code(tar.gz)
    Source code(zip)
  • 4.2.0(Oct 31, 2022)

    • Update compileSdk and targetSdk to 33.
    • Set maxSdkVersion for WRITE_EXTERNAL_STORAGE permission.
    • Update AGP to 7.3.1.
    • Update Kotlin to 1.7.20.

    Full Changelog: https://github.com/solrudev/SimpleInstaller/compare/4.1.3...4.2.0

    Source code(tar.gz)
    Source code(zip)
  • 4.1.3(Sep 21, 2022)

    • Avoid possible progress conflicts if there are different concurrent android.content.pm.PackageInstaller sessions owned by app on API >= 21.
    • Docs for ApkSource implementations.
    • Some micro-optimizations.
    • Update AppCompat to 1.5.1.
    • Make AndroidX Startup a transitive dependency.

    Full Changelog: https://github.com/solrudev/SimpleInstaller/compare/4.1.2...4.1.3

    Source code(tar.gz)
    Source code(zip)
  • 4.1.2(Sep 18, 2022)

  • 4.1.1(Sep 10, 2022)

    • Retrieve app label for install notification lazily.
    • Update coroutines version to 1.6.4.

    Full Changelog: https://github.com/solrudev/SimpleInstaller/compare/4.1.0...4.1.1

    Source code(tar.gz)
    Source code(zip)
  • 4.1.0(Aug 30, 2022)

  • 4.0.0(Aug 30, 2022)

    Starting from 4.0.0 API is considered stable. Usage of earlier versions is discouraged.

    • Added customization options for install/uninstall sessions.
    • Removed PackageInstaller helpers for Java as they are quite cumbersome to use and their API is not that great. Use methods from PackageInstaller directly.
    • Removed deprecated SimpleInstaller.initialize() methods.
    • Removed SimpleInstaller.setNotificationIcon() method. Use SessionOptions to customize notification icon.
    • Internals refactoring.
    • Updated Kotlin to 1.7.10.
    • Updated dependencies.

    Full Changelog: https://github.com/solrudev/SimpleInstaller/compare/3.1.1...4.0.0

    Source code(tar.gz)
    Source code(zip)
  • 3.1.1(Jul 2, 2022)

    • Update message of ApplicationContextNotSetException to reflect changes of 3.1.x.
    • Override toString() in InstallResult and InstallFailureCause.
    • Update Okio version to 3.2.0.

    Full Changelog: https://github.com/solrudev/SimpleInstaller/compare/3.1.0...3.1.1

    Source code(tar.gz)
    Source code(zip)
  • 3.1.0(Jul 1, 2022)

    • Use Jetpack App Startup library. Deprecate initialize() method.
    • Fix InstallLauncherActivity not finishing on cancel.
    • Show name of the app to be installed (if available) or uninstalled in notifications.
    • Update Kotlin to 1.7.0.
    • Update dependencies.

    Full Changelog: https://github.com/solrudev/SimpleInstaller/compare/3.0.2...3.1.0

    Source code(tar.gz)
    Source code(zip)
  • 3.0.2(May 4, 2022)

    • Always check if continuations are active before resuming them.

    Full Changelog: https://github.com/solrudev/SimpleInstaller/compare/3.0.1...3.0.2

    Source code(tar.gz)
    Source code(zip)
  • 3.0.1(May 4, 2022)

    • Change PackageInstaller and PackageUninstaller implementations initialization from using more expensive lazy delegated properties to Kotlin objects.

    Full Changelog: https://github.com/solrudev/SimpleInstaller/compare/3.0.0...3.0.1

    Source code(tar.gz)
    Source code(zip)
  • 3.0.0(May 4, 2022)

    1. Source-incompatible (for Java client code only, Kotlin client code is not affected) and binary-incompatible API changes:
    • PackageInstaller and PackageUninstaller are interfaces now, so to use these classes one must obtain a singleton instance through getInstance() static method;
    • Callbacks are now inner interfaces of PackageInstaller and PackageUninstaller;
    • In order to use PackageInstallerHelper methods a PackageInstaller instance must be passed as the first parameter.
    1. Breaking: _progress property is now private in ApkSource. Subclasses should use progress() method to emit progress data.

    Full Changelog: https://github.com/solrudev/SimpleInstaller/compare/2.0.4...3.0.0

    Source code(tar.gz)
    Source code(zip)
  • 2.0.4(Apr 27, 2022)

    • Update notification channel title and description on configuration changes (such as locale change).
    • Update Kotlin version to 1.6.21.
    • Update coroutines version to 1.6.1.
    • Update Okio version to 3.1.0.

    Full Changelog: https://github.com/solrudev/SimpleInstaller/compare/2.0.3...2.0.4

    Source code(tar.gz)
    Source code(zip)
  • 2.0.3(Mar 26, 2022)

  • 2.0.2(Mar 18, 2022)

  • 2.0.1(Mar 11, 2022)

  • 2.0.0(May 4, 2022)

    • Breaking: move helper methods from PackageInstaller to PackageInstallerExt.kt (Kotlin extension methods for PackageInstaller, need an additional import to use) and PackageInstallerHelper. Please update usages of installPackage() and installSplitPackage() methods according to the updated README.
    • Update dependencies.

    Full Changelog: https://github.com/solrudev/SimpleInstaller/compare/1.2.4...2.0.0

    Source code(tar.gz)
    Source code(zip)
  • 1.2.4(Feb 12, 2022)

    • Use ACTION_UNINSTALL_PACKAGE intent action on Android API level lower than 29.
    • Update Gradle and dependencies.

    Full Changelog: https://github.com/solrudev/SimpleInstaller/compare/1.2.3...1.2.4

    Source code(tar.gz)
    Source code(zip)
  • 1.2.3(Jan 11, 2022)

    • Update Kotlin version to 1.6.10.
    • Update coroutines version to 1.6.0.

    Full Changelog: https://github.com/solrudev/SimpleInstaller/compare/1.2.2...1.2.3

    Source code(tar.gz)
    Source code(zip)
  • 1.2.2(Jan 2, 2022)

  • 1.2.1(Dec 12, 2021)

    • Small docs fix.
    • Hide internal members from Java sources.

    Full Changelog: https://github.com/solrudev/SimpleInstaller/compare/1.2.0...1.2.1

    Source code(tar.gz)
    Source code(zip)
  • 1.2.0(Dec 12, 2021)

  • 1.1.1(Dec 7, 2021)

  • 1.1.0(Dec 5, 2021)

    • New: implement different APK sources.
    • Remove unnecessary dependencies.

    Full Changelog: https://github.com/solrudev/SimpleInstaller/compare/1.0.1...1.1.0

    Source code(tar.gz)
    Source code(zip)
  • 1.0.1(Dec 3, 2021)

    • Fix coroutine not resuming when install fail not handled in installationEventsReceiver occurs.
    • Update dependencies.

    Full Changelog: https://github.com/solrudev/SimpleInstaller/compare/1.0.0...1.0.1

    Source code(tar.gz)
    Source code(zip)
  • 1.0.0(Nov 29, 2021)

Owner
Ilya Fomichev
An aspiring Android developer
Ilya Fomichev
Turtle Graphics 🐢 implementation for Android Platform with Code Editor, Preview Screen and packages

Turtle Graphics Download Turtle is an Android Application inspired from the original Turtle Graphics and Logo, Logo is an educational programming lang

Amr Hesham 15 Dec 30, 2022
A general purpose kotlin library that use kotlin coroutines, flows and channels to provide timer features with the most easy and efficient way

Timer Timer is a general purpose kotlin library that use kotlin coroutines, flows and channels to provide timer features with the most easy and effici

Amr Saraya 3 Jul 11, 2022
An android application where you can manage and install all versions of the spotify app.

An android application where you can manage and install all versions of the spotify app.

xManager (Spotify) 3.7k Dec 30, 2022
Android common lib demo, include ImageCache, HttpCache, DropDownListView, DownloadManager, install apk silent and so on, you can find description

android-demo 关于我,欢迎关注 微博:Trinea 主页:trinea.cn 邮箱:trinea.cn#gmail.com 微信:codek2 依赖:trinea-android-common android-auto-scroll-view-pager viewpager-indica

Trinea 1.1k Dec 30, 2022
android-delicious Delicious Android is an Android app which helps you access and save bookmarks via Delicious. It's available over at Google Play.

Delicious Android Delicious Android is an Android app which helps you access and save bookmarks via Delicious. It's available over at Google Play. Fea

Alexander Blom 137 Nov 20, 2022
SimpleToDo is an android app that allows building a todo list and basic todo items management functionality including adding new items, editing and deleting an existing item.

SimpleToDo is an android app that allows building a todo list and basic todo items management functionality including adding new items, editing and deleting an existing item.

null 0 Jan 3, 2022
Todo List Application is an android app that allows building a todo list and basic todo items management functionality including adding new items, editing and deleting an existing item

Todo List Application is an android app that allows building a todo list and basic todo items management functionality including adding new items, editing and deleting an existing item

null 0 Jan 22, 2022
An easy-to-use CSML API Client for Android.

CSML Android SDK An easy-to-use CSML API Client for Android. Overview CSML (Conversational Standard Meta Language) is an Open-Source, Domain-Specific

Owais 4 Aug 27, 2022
Calculator provides an easy to use API to evaluate mathematical expressions

Calculator Calculator provides an easy to use API to evaluate mathematical expressions created with following operators: + - * / ( ) For example: This

null 1 Nov 27, 2021