MuntashirAkon/AppManager · error · BackupException

Failed to encrypt

Error message

Failed to encrypt 

What it means

After archiving APK files, backupApkFiles() encrypts them via mBackupItem.encrypt(sourceFiles); an IOException is wrapped in a BackupException prefixed 'Failed to encrypt ' followed by the file list. The APK tar archive exists but the encrypted copy could not be produced, aborting the backup before checksums are recorded.

Source

Thrown at app/src/main/java/io/github/muntashirakon/AppManager/backup/BackupOp.java:335

            // APK located inside /data/app directory
            // Backup only the apk file (no split apk support for this type of apk)
            try {
                sourceDir = sourceDir.findFile(mMetadata.metadata.apkName);
            } catch (FileNotFoundException e) {
                throw new BackupException(mMetadata.metadata.apkName + " not found at " + sourceDir);
            }
        }
        Path[] sourceFiles;
        try {
            sourceFiles = TarUtils.create(mMetadata.info.tarType, sourceDir, mBackupItem.getUnencryptedBackupPath(), sourceBackupFilePrefix,
                    /* language=regexp */ new String[]{".*\\.apk"}, null, null, false).toArray(new Path[0]);
        } catch (Throwable th) {
            throw new BackupException("APK files backup is requested but no source directory has been backed up.", th);
        }
        try {
            sourceFiles = mBackupItem.encrypt(sourceFiles);
        } catch (IOException e) {
            throw new BackupException("Failed to encrypt " + Arrays.toString(sourceFiles), e);
        }
        for (Path file : sourceFiles) {
            mChecksum.add(file.getName(), DigestUtils.getHexDigest(mMetadata.info.checksumAlgo, file));
        }
    }

    private void backupData() throws BackupException {
        for (int i = 0; i < mMetadata.metadata.dataDirs.length; ++i) {
            Path[] dataFiles;
            String backupDataDir = mMetadata.metadata.dataDirs[i];
            if (backupDataDir.equals(BackupManager.DATA_BACKUP_SPECIAL_ADB)) {
                // ADB backup
                dataFiles = backupAdb(i);
            } else {
                // Regular directory backup
                dataFiles = backupDirectory(backupDataDir, i);
            }
            try {

View on GitHub (pinned to 0152f468fc)

Solutions

  1. Verify the encryption password and that crypto is available (root required for keystore access).
  2. Check free space — encrypted duplicates need room for the full APK set.
  3. Read the chained IOException for the failing file path.
  4. Retry the backup; uncommitted partials are cleaned up.

Example fix

// before
mBackupItem.encrypt(sourceFiles); // password never verified
// after
if (Prefs.Crypto.isEncryptionEnabled() && !CryptoUtils.canAccessKeystore()) {
    throw new IllegalStateException("Grant root to use backup encryption");
}
mBackupItem.encrypt(sourceFiles);
Defensive patterns

Strategy: try-catch

Validate before calling

if (Prefs.Crypto.isEncryptionEnabled() && !CryptoUtils.canAccessKeystore()) { requestRoot(); }
if (backupDest.getUsableSpace() < totalApkSizeBytes) { freeSpace(); }

Type guard

boolean encryptionReady() { return !Prefs.Crypto.isEncryptionEnabled() || CryptoUtils.canAccessKeystore(); }

Try / catch

try {
    backupOp.runBackup(progress);
} catch (BackupException e) {
    if (e.getMessage().startsWith("Failed to encrypt ")) {
        Log.e(TAG, "APK encryption failed", (IOException) e.getCause());
    }
}

Prevention

When it happens

Trigger: mBackupItem.encrypt(Path[]) throws IOException during APK-file backup — encryption enabled with bad credentials/crypto setup, or destination write failure.

Common situations: Mistyped backup password or unavailable keystore (encryption requires root); insufficient space to write the encrypted duplicate of large APKs; destination I/O errors.

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/ea62c2846effdb78. Report an issue: GitHub.