MuntashirAkon/AppManager · error · BackupException

Could not create package staging directory

Error message

Could not create package staging directory

What it means

restoreApkFiles() stages APK files in /data/local/tmp (PackageUtils.PACKAGE_STAGING_DIRECTORY) before installing. It probes the directory with canWrite(); if that check throws IOException and the fallback to the unencrypted backup path also cannot be evaluated, this BackupException wrapping the IOException is thrown. Without a writable staging area, the APKs cannot be prepared for PackageInstaller.

Source

Thrown at app/src/main/java/io/github/muntashirakon/AppManager/backup/RestoreOp.java:333

                if (installer.uninstall(mPackageName, mUserId, false)) {
                    throw new BackupException("An uninstallation was necessary but couldn't perform it.");
                }
            }
        }
        // Setup package staging directory
        Path packageStagingDirectory = Paths.get(PackageUtils.PACKAGE_STAGING_DIRECTORY);
        try {
            synchronized (sLock) {
                PackageUtils.ensurePackageStagingDirectoryPrivileged();
            }
        } catch (Exception ignore) {
        }
        try {
            if (!packageStagingDirectory.canWrite()) {
                packageStagingDirectory = mBackupItem.getUnencryptedBackupPath();
            }
        } catch (IOException e) {
            throw new BackupException("Could not create package staging directory", e);
        }
        synchronized (sLock) {
            // Setup apk files, including split apk
            final int splitCount = mBackupMetadata.splitConfigs.length;
            String[] allApkNames = new String[splitCount + 1];
            Path[] allApks = new Path[splitCount + 1];
            try {
                Path baseApk = packageStagingDirectory.createNewFile(mBackupMetadata.apkName, null);
                allApks[0] = baseApk;
                allApkNames[0] = mBackupMetadata.apkName;
                for (int i = 1; i < allApkNames.length; ++i) {
                    allApkNames[i] = mBackupMetadata.splitConfigs[i - 1];
                    allApks[i] = packageStagingDirectory.createNewFile(allApkNames[i], null);
                }
            } catch (IOException e) {
                throw new BackupException("Could not create staging files", e);
            }
            // Decrypt sources

View on GitHub (pinned to 0152f468fc)

Solutions

  1. Free space and fix permissions on /data/local/tmp (e.g. via root: chown/chmod or recreate the directory), then retry the restore.
  2. Run App Manager with root or shell (ADB) privileges so it can create/fix the staging directory (PackageUtils.ensurePackageStagingDirectoryPrivileged is attempted just before).
  3. Reboot the device — a stale tmpfs/stray mount on /data/local/tmp often clears after reboot.
  4. Ensure the backup location is writable and reachable as the fallback staging path (check scoped-storage grants for the folder).
Defensive patterns

Strategy: fallback

Validate before calling

// Pre-check staging directory writability before restore:
Path staging = Paths.get(PackageUtils.PACKAGE_STAGING_DIRECTORY);
try {
    if (!staging.canWrite()) { /* ensure privileged setup or pick fallback path */ }
} catch (IOException e) { /* storage problem: fix permissions/space first */ }

Try / catch

try { restoreOp.runRestore(); } catch (BackupException e) {
    if (e.getMessage().contains("Could not create package staging directory")) {
        // inspect cause: free space, permissions, SELinux; retry after remediation
    }
}

Prevention

When it happens

Trigger: packageStagingDirectory.canWrite() throws IOException during restoreApkFiles(); the exception is caught and rethrown as 'Could not create package staging directory'.

Common situations: /data/local/tmp is full, has wrong ownership/permissions, or was wiped into a bad state; SELinux denies access in non-privileged modes; storage is mounted read-only; the backup path fallback is unavailable on scoped-storage devices.

Understand the failure class

Background: "failed to write file", "Could not save figure", "Error saving remote file" — file write failed: causes and fixes across languages and libraries — this error's family across 38 libraries.

Related errors


AI-assisted analysis of MuntashirAkon/AppManager@0152f468fc (2026-09-12). Data as JSON: /api/errors/fff4a4a6f08d6c8e. Report an issue: GitHub.