MuntashirAkon/AppManager · error · ApkFile.ApkFileException

Could not cache the APK file

Error message

Could not cache the APK file

What it means

When direct manifest extraction fails, getManifestFromApk falls back to caching the input stream to a temp file via FileCache before re-parsing. If getCachedFile throws IOException, this ApkFileException 'Could not cache the APK file' is thrown.

Source

Thrown at app/src/main/java/io/github/muntashirakon/AppManager/apk/ApkUtils.java:201

                    continue;
                }
                ByteArrayOutputStream buffer = new ByteArrayOutputStream();
                byte[] buf = new byte[IoUtils.DEFAULT_BUFFER_SIZE];
                int n;
                while (-1 != (n = zipInputStream.read(buf))) {
                    buffer.write(buf, 0, n);
                }
                return ByteBuffer.wrap(buffer.toByteArray());
            }
        } catch (IOException e) {
            Log.w(TAG, "Could not fetch AndroidManifest.xml from APK stream, trying an alternative...", e);
        }
        // This could be due to a Zip error, try caching the APK
        File cachedApk;
        try {
            cachedApk = FileCache.getGlobalFileCache().getCachedFile(apkInputStream, "apk");
        } catch (IOException e) {
            throw new ApkFile.ApkFileException("Could not cache the APK file", e);
        }
        ByteBuffer byteBuffer;
        try {
            byteBuffer = getManifestFromApk(cachedApk);
        } finally {
            FileCache.getGlobalFileCache().delete(cachedApk);
        }
        return byteBuffer;
    }

    @NonNull
    public static HashMap<String, String> getManifestAttributes(@NonNull ByteBuffer manifestBytes)
            throws ApkFile.ApkFileException {
        try (BlockReader reader = new BlockReader(manifestBytes.array())) {
            HashMap<String, String> manifestAttrs = new HashMap<>();
            ResXmlDocument xmlBlock = new ResXmlDocument();
            try {
                xmlBlock.readBytes(reader);

View on GitHub (pinned to 0152f468fc)

Solutions

  1. Check free disk space where FileCache stores global cache files
  2. Verify the cache directory exists and is writable
  3. Ensure the InputStream is readable and not closed prematurely
  4. Initialize/open the global FileCache before calling this API
  5. Read the wrapped IOException cause for the exact filesystem error

Example fix

// before
InputStream apkStream = uriToStream(uri);
ApkUtils.getManifestFromApk(apkStream);
// after
File tmp = File.createTempFile("apk", ".apk", ensureWritableCacheDir());
try (InputStream in = uriToStream(uri); OutputStream out = new FileOutputStream(tmp)) {
    IoUtils.copy(in, out);
    ApkUtils.getManifestFromApk(tmp);
}
Defensive patterns

Strategy: try-catch

Validate before calling

File cacheDir = new File(context.getCacheDir(), "apk-cache");
if (!cacheDir.isDirectory() && !cacheDir.mkdirs()) throw new IOException("Cache dir unwritable");
if (cacheDir.getUsableSpace() < 64 * 1024 * 1024L) throw new IOException("Low disk space");

Try / catch

try (InputStream in = apkInputStream) {
    return ApkUtils.getManifestFromApk(in);
} catch (ApkFile.ApkFileException e) {
    if (e.getMessage() != null && e.getMessage().contains("Could not cache")) {
        Log.e(TAG, "Cache write failed: check disk space/permissions", e);
    }
    throw e;
}

Prevention

When it happens

Trigger: Calling a getManifestFromApk overload that takes an InputStream, where the global FileCache cannot write the cached file — getCachedFile(apkInputStream, "apk") throws IOException.

Common situations: No free disk space in the cache directory, cache directory not writable (permissions/SELinux), stream IO error while copying, or storage volume unmounted.

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