Skip to main content

Apk Cleaner

The apk cleaner is designed to find and clear files with the '.apk' extension on the device.

important

Starting with Android 10, devices use a new type of Scoped Storage. For the apk cleaner to work within Scoped Storage, you need a MANAGE_EXTERNAL_STORAGE permission

Analysis#

The following code example demonstrates how to use the analysis operation:

// Get the apk cleaner through the cleaner managerval apkCleaner = CleanerManagerFactory.cleanerManager.getCleaner(CleanerType.APK)
// Get coroutine scope to call cleaner library functionsval cleanerScope = CleanerManagerFactory.cleanerManager.scope
// Run all operations related to libraries only in the cleaner librarycleanerScope.launch {        // Start the analysis process with the following call    apkCleaner.analyze(        isFullRefresh = true    )}

The cleaner is represented by an object that implements the interface Cleaner, the method analyze which takes isFullRefresh: Boolean:

  • true - when analyzing, the cleaner scans the system again;
  • false - the method returns the cached result of the parsing (only non-deleted items).

Cleaning#

Use the following code example to clear items.

// Get the apk cleaner through the cleaner managerval apkCleaner = CleanerManagerFactory.cleanerManager.getCleaner(CleanerType.APK)
// Get coroutine scope to call cleaner library functionsval cleanerScope = CleanerManagerFactory.cleanerManager.scope
// Get a list of items to removeval trashItemList: List<TrashItem>
// Run all operations related to libraries only in the cleaner librarycleanerScope.launch {    // Start the analysis process with the following call    apkCleaner.clean(        content = trashItemList    )}

A cleaner is represented by an object that implements the Cleaner interface, whose clean method accepts content: List<TrashItem> (list of items to remove)

Getting results#

In order to get the state of the cleaner and the results of its work, you need to subscribe to its state as follows:

// Get the apk cleaner through the cleaner managerval apkCleaner = CleanerManagerFactory.cleanerManager.getCleaner(CleanerType.APK)
// Get coroutine scope to call cleaner library functionsval cleanerScope = CleanerManagerFactory.cleanerManager.scope
// Run all operations related to libraries only in the cleaner librarycleanerScope.launch {    // Subscribe to the states of the apk cleaner    apkCleaner.stateFlow.collect { cleanerState -> // cleanerState: CleanerState        // Do something...    }}

Apk cleaner inherits Cleaner interface that provides stateFlow: StateFlow (StateFlow) which emits CleanerState objects containing information about the condition of the cleaner. Here are the following possible states of the cleaner:

Inactive - this is the initial state of the cleaner before starting work.

Analyzing - this state indicates that the cleaner is in the process of analyzing data. It includes the following fields:

  • itemCount: Int - number of items found
  • size: Long - size of found elements in bits

Analysed - this state indicates that the cleaner has finished analyzing the data. It includes the following fields:

  • items: List<ApkItem> - list of found items
  • error: Trowable? - an error occurred during the analysis process

Cleaning -this state indicates that the cleaner is in the process of deleting data. It includes the following fields:

  • itemCount: Int - number of items found
  • size: Long - size of found elements in bits

Cleaned - this state indicates that the cleaner has completed the data cleaning process. It includes the following fields:

  • items: List<ApkItem> - list of deleted items
  • error: Trowable? - an error occurred during the cleaning process

Links - CleanerState, ApkItem, Cleaner.