How to Uninstall Android Apps Programmatically?
In this article, we are going to see how we can programmatically uninstall any Android device from your Android device programmatically by specifying the App name. A sample video is given below to get an idea about what we are going to do in this article.
Step-by-Step Implementation
Step 1: Create a New Project in Android Studio
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Kotlin as the programming language.
Step 2:
Open Your Mainfest File Navigate to app->mainfest->AndroidManifest.xml and add below two permissions in your mainfest file.
<uses-permission android:name="android.permission.QUERY_ALL_PACKAGES"
tools:ignore="QueryAllPackagesPermission" />
<uses-permission android:name="android.permission.REQUEST_DELETE_PACKAGES"/>
- android.permission.QUERY_ALL_PACKAGES: This permission allows the app to query information about all installed packages on the device. It iss used to retrieve a list of installed applications programmatically.
- android.permission.REQUEST_DELETE_PACKAGES: This permission grants the app the ability to request the deletion of packages (apps) on the device. It is used to programmatically initiate the uninstallation process of specified applications.
Step 3:
We have created one basic UI with one edit-text(for user input which package(app) to uninstall),Button(to let user submit) and one Text-View if package is uninstalled sucessfully then it’ll display one message indicating uninstallation is successful.
activity_main3.xml:
XML
<? xml version = "1.0" encoding = "utf-8" ?> < LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android" xmlns:tools = "http://schemas.android.com/tools" android:layout_width = "match_parent" android:background = "@color/white" android:layout_height = "match_parent" android:orientation = "vertical" android:padding = "16dp" tools:context = ".MainActivity3" > < EditText android:id = "@+id/input" android:layout_width = "match_parent" android:layout_height = "wrap_content" android:hint = "Enter app name" android:textColor = "@color/black" android:inputType = "text" /> < Button android:id = "@+id/btn" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:text = "Uninstall" android:textColor = "@color/black" android:layout_gravity = "center" android:layout_marginTop = "16dp" /> < TextView android:id = "@+id/Output" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_marginTop = "16dp" android:textColor = "@color/black" android:textSize = "18sp" android:textStyle = "bold" /> </ LinearLayout > |
Step 4: Working with the MainActivity.kt file
Go to the MainActivity.kt file and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.
Kotlin
package com.ayush.gfgapp import android.content.Intent import android.content.pm.PackageManager import android.net.Uri import android.os.Bundle import android.widget.Button import android.widget.EditText import android.widget.TextView import androidx.appcompat.app.AppCompatActivity class MainActivity3 : AppCompatActivity() { private lateinit var namePkg: EditText private lateinit var textView: TextView private lateinit var btnUni:Button override fun onCreate(savedInstanceState: Bundle?) { super .onCreate(savedInstanceState) setContentView(R.layout.activity_main3) textView = findViewById(R.id.Output) namePkg = findViewById(R.id.input) btnUni = findViewById(R.id.btn) btnUni.setOnClickListener { // Calling Function on button click UninstallPkg() } } private fun UninstallPkg() { // Geting the entered package name from the EditText // and convert it to lowercase val stringAppName = namePkg.text.toString().toLowerCase() // Geting a list of all installed applications on the device val applicationInfoList = packageManager.getInstalledApplications(PackageManager.GET_META_DATA) // Initializing a variable to hold the package name var stringPackageName: String? = null // Loop through the list of installed applications for (applicationInfo in applicationInfoList) { // Check if the package name contains the entered app name if (applicationInfo.packageName.contains(stringAppName)) { // If a match is found assigning the package name // to the variable and exit the loop stringPackageName = applicationInfo.packageName break } } // Checking if a matching package name was found if (stringPackageName == null ) { // If no match was found displaying a message // indicating unsuccessful deletion textView.text = "Deleting unsuccessful" return // Exit the function } // Creating an intent to request the deletion // of the specified package val intent = Intent(Intent.ACTION_DELETE) // Seting the data of the intent to specify the // package to be uninstalled intent.data = Uri.parse( "package:$stringPackageName" ) // Staringt the uninstallation // process by sending the intent startActivity(intent) // Display a success message textView.text = "Uninstalled Success:- $stringPackageName" } } |
Notes:
- Make Sure There is no any whitespace when you’re entering the App named
- This can uninstall system apps so code it wisely
Output: