{"record":{"id":"fff4a4a6f08d6c8e","repo":"MuntashirAkon/AppManager","slug":"could-not-create-package-staging-directory","errorCode":null,"errorMessage":"Could not create package staging directory","messagePattern":"Could not create package staging directory","errorType":"exception","errorClass":"BackupException","httpStatus":null,"severity":"error","filePath":"app/src/main/java/io/github/muntashirakon/AppManager/backup/RestoreOp.java","lineNumber":333,"sourceCode":"                if (installer.uninstall(mPackageName, mUserId, false)) {\n                    throw new BackupException(\"An uninstallation was necessary but couldn't perform it.\");\n                }\n            }\n        }\n        // Setup package staging directory\n        Path packageStagingDirectory = Paths.get(PackageUtils.PACKAGE_STAGING_DIRECTORY);\n        try {\n            synchronized (sLock) {\n                PackageUtils.ensurePackageStagingDirectoryPrivileged();\n            }\n        } catch (Exception ignore) {\n        }\n        try {\n            if (!packageStagingDirectory.canWrite()) {\n                packageStagingDirectory = mBackupItem.getUnencryptedBackupPath();\n            }\n        } catch (IOException e) {\n            throw new BackupException(\"Could not create package staging directory\", e);\n        }\n        synchronized (sLock) {\n            // Setup apk files, including split apk\n            final int splitCount = mBackupMetadata.splitConfigs.length;\n            String[] allApkNames = new String[splitCount + 1];\n            Path[] allApks = new Path[splitCount + 1];\n            try {\n                Path baseApk = packageStagingDirectory.createNewFile(mBackupMetadata.apkName, null);\n                allApks[0] = baseApk;\n                allApkNames[0] = mBackupMetadata.apkName;\n                for (int i = 1; i < allApkNames.length; ++i) {\n                    allApkNames[i] = mBackupMetadata.splitConfigs[i - 1];\n                    allApks[i] = packageStagingDirectory.createNewFile(allApkNames[i], null);\n                }\n            } catch (IOException e) {\n                throw new BackupException(\"Could not create staging files\", e);\n            }\n            // Decrypt sources","sourceCodeStart":315,"sourceCodeEnd":351,"githubUrl":"https://github.com/MuntashirAkon/AppManager/blob/0152f468fc9463ee02dc2ca83f6fe4989a2c4ca5/app/src/main/java/io/github/muntashirakon/AppManager/backup/RestoreOp.java#L315-L351","documentation":"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.","triggerScenarios":"packageStagingDirectory.canWrite() throws IOException during restoreApkFiles(); the exception is caught and rethrown as 'Could not create package staging directory'.","commonSituations":"/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.","solutions":["Free space and fix permissions on /data/local/tmp (e.g. via root: chown/chmod or recreate the directory), then retry the restore.","Run App Manager with root or shell (ADB) privileges so it can create/fix the staging directory (PackageUtils.ensurePackageStagingDirectoryPrivileged is attempted just before).","Reboot the device — a stale tmpfs/stray mount on /data/local/tmp often clears after reboot.","Ensure the backup location is writable and reachable as the fallback staging path (check scoped-storage grants for the folder)."],"exampleFix":null,"handlingStrategy":"fallback","validationCode":"// Pre-check staging directory writability before restore:\nPath staging = Paths.get(PackageUtils.PACKAGE_STAGING_DIRECTORY);\ntry {\n    if (!staging.canWrite()) { /* ensure privileged setup or pick fallback path */ }\n} catch (IOException e) { /* storage problem: fix permissions/space first */ }","typeGuard":null,"tryCatchPattern":"try { restoreOp.runRestore(); } catch (BackupException e) {\n    if (e.getMessage().contains(\"Could not create package staging directory\")) {\n        // inspect cause: free space, permissions, SELinux; retry after remediation\n    }\n}","preventionTips":["Keep free space on the volume hosting /data/local/tmp.","Run App Manager with root/ADB privileges so it can repair the staging directory.","Reboot if /data/local/tmp behaves oddly (stray mounts, stale permissions)."],"tags":["android","filesystem","staging","permissions"],"backgroundTag":"file-write-failed","analyzedSha":"0152f468fc9463ee02dc2ca83f6fe4989a2c4ca5","analyzedAt":"2026-09-12T14:03:37.243Z","contentChangedAt":"2026-09-12T14:03:37.243Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}