{"record":{"id":"ea62c2846effdb78","repo":"MuntashirAkon/AppManager","slug":"failed-to-encrypt","errorCode":null,"errorMessage":"Failed to encrypt ","messagePattern":"Failed to encrypt ","errorType":"exception","errorClass":"BackupException","httpStatus":null,"severity":"error","filePath":"app/src/main/java/io/github/muntashirakon/AppManager/backup/BackupOp.java","lineNumber":335,"sourceCode":"            // APK located inside /data/app directory\n            // Backup only the apk file (no split apk support for this type of apk)\n            try {\n                sourceDir = sourceDir.findFile(mMetadata.metadata.apkName);\n            } catch (FileNotFoundException e) {\n                throw new BackupException(mMetadata.metadata.apkName + \" not found at \" + sourceDir);\n            }\n        }\n        Path[] sourceFiles;\n        try {\n            sourceFiles = TarUtils.create(mMetadata.info.tarType, sourceDir, mBackupItem.getUnencryptedBackupPath(), sourceBackupFilePrefix,\n                    /* language=regexp */ new String[]{\".*\\\\.apk\"}, null, null, false).toArray(new Path[0]);\n        } catch (Throwable th) {\n            throw new BackupException(\"APK files backup is requested but no source directory has been backed up.\", th);\n        }\n        try {\n            sourceFiles = mBackupItem.encrypt(sourceFiles);\n        } catch (IOException e) {\n            throw new BackupException(\"Failed to encrypt \" + Arrays.toString(sourceFiles), e);\n        }\n        for (Path file : sourceFiles) {\n            mChecksum.add(file.getName(), DigestUtils.getHexDigest(mMetadata.info.checksumAlgo, file));\n        }\n    }\n\n    private void backupData() throws BackupException {\n        for (int i = 0; i < mMetadata.metadata.dataDirs.length; ++i) {\n            Path[] dataFiles;\n            String backupDataDir = mMetadata.metadata.dataDirs[i];\n            if (backupDataDir.equals(BackupManager.DATA_BACKUP_SPECIAL_ADB)) {\n                // ADB backup\n                dataFiles = backupAdb(i);\n            } else {\n                // Regular directory backup\n                dataFiles = backupDirectory(backupDataDir, i);\n            }\n            try {","sourceCodeStart":317,"sourceCodeEnd":353,"githubUrl":"https://github.com/MuntashirAkon/AppManager/blob/0152f468fc9463ee02dc2ca83f6fe4989a2c4ca5/app/src/main/java/io/github/muntashirakon/AppManager/backup/BackupOp.java#L317-L353","documentation":"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.","triggerScenarios":"mBackupItem.encrypt(Path[]) throws IOException during APK-file backup — encryption enabled with bad credentials/crypto setup, or destination write failure.","commonSituations":"Mistyped backup password or unavailable keystore (encryption requires root); insufficient space to write the encrypted duplicate of large APKs; destination I/O errors.","solutions":["Verify the encryption password and that crypto is available (root required for keystore access).","Check free space — encrypted duplicates need room for the full APK set.","Read the chained IOException for the failing file path.","Retry the backup; uncommitted partials are cleaned up."],"exampleFix":"// before\nmBackupItem.encrypt(sourceFiles); // password never verified\n// after\nif (Prefs.Crypto.isEncryptionEnabled() && !CryptoUtils.canAccessKeystore()) {\n    throw new IllegalStateException(\"Grant root to use backup encryption\");\n}\nmBackupItem.encrypt(sourceFiles);","handlingStrategy":"try-catch","validationCode":"if (Prefs.Crypto.isEncryptionEnabled() && !CryptoUtils.canAccessKeystore()) { requestRoot(); }\nif (backupDest.getUsableSpace() < totalApkSizeBytes) { freeSpace(); }","typeGuard":"boolean encryptionReady() { return !Prefs.Crypto.isEncryptionEnabled() || CryptoUtils.canAccessKeystore(); }","tryCatchPattern":"try {\n    backupOp.runBackup(progress);\n} catch (BackupException e) {\n    if (e.getMessage().startsWith(\"Failed to encrypt \")) {\n        Log.e(TAG, \"APK encryption failed\", (IOException) e.getCause());\n    }\n}","preventionTips":["Validate the backup password before starting.","Keep free space >= total APK size when encrypting.","Ensure root is granted — keystore crypto fails without it.","Retry is safe; uncommitted files are cleaned up."],"tags":["android","backup","encryption","apk"],"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-14T16:17:12.679Z"}