To automatically delete an APK file after it has been installed in Android, you typically cannot track the exact completion status of a system-level APK installation directly inside a standard third-party or host app because Android hands off the installation process to the system installer (Intent.ACTION_INSTALL_PACKAGE or Intent.ACTION_VIEW).
However, if your app downloads and triggers the installation of an APK (such as in an in-app updater), you can handle the deletion file reference or use a workaround like checking installation status when the app restarts. [1, 2]
Conceptual Approach (Java / Kotlin Example)
An installer intent launches the system package installer. Because
startActivity does not reliably return a direct "installation finished" callback in modern Android versions, standard practice involves either:- Deleting the file if you manage a custom installer flow.
- Checking via a file path or background routine upon the next app launch.
java
// Example: Launching APK installation
File apkFile = new File(getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), "update.apk");
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri apkUri;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
apkUri = FileProvider.getUriForFile(context, context.getPackageName() + ".fileprovider", apkFile);
intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
} else {
apkUri = Uri.fromFile(apkFile);
intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
}
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
// Deletion logic (Note: Deleting immediately here will fail the install
// because the installer needs to read the file during the process).
Use code with caution.
Important Limitations & Workarounds
- Timing issue: If you delete the
apkFile.delete()right after callingstartActivity(), the Android package installer will crash or throw a parsing error because the file is deleted before the system reads it. [1] - No direct callback: Standard Android security sandbox rules do not notify your app the exact millisecond the user finishes tapping "Install" and "Done" in the package installer window.
- Alternative Workaround: Developers usually store the path to the downloaded APK in
SharedPreferences. On the next launch of your main activity, your code checks if the target package is now installed viaPackageManager, and if so, deletes the leftover.apkfile from storage. [1]
java
// On next app startup: check if app is installed and clean up APK
File apkFile = new File(path_to_apk);
if (apkFile.exists()) {
if (isPackageInstalled("com.example.targetpackage", packageManager)) {
apkFile.delete();
}
}
Use code with caution.
If you are building a specific feature (like an in-app updater or a custom app store/manager), let me know:
- Are you targeting Android 10+ (Scoped Storage)?
- Are you using Java or Kotlin?
I can provide a complete file-cleanup implementation tailored to your setup.
0 Comments