How to uninstall/delete APK Programmatically in Android

In this post we are going to learn how to uninstall apk programmatically in Android

Let's Start

Step 1: Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project

Step 2: Updated xml file with below code


 

Step 3: Updated Activity file with below code

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import kotlinx.android.synthetic.main.activity_un_install_apk.*
import android.content.Intent
import androidx.core.app.ComponentActivity.ExtraData
import androidx.core.content.ContextCompat.getSystemService
import android.icu.lang.UCharacter.GraphemeClusterBreak.T
import android.net.Uri
import android.util.Log


class UnInstallApkActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_un_install_apk)
        btnButton.setOnClickListener(object: View.OnClickListener{
            override fun onClick(v: View?) {
                val intent = Intent(Intent.ACTION_DELETE)
                Log.v("application.packageName","application.packageName "+application.packageName);
                intent.data = Uri.parse("package:"+application.packageName)
                startActivity(intent)
            }
        })
    }
}

 

Step 4: Run application

Uninstall APK Programatically