iBotPeaches/Apktool · critical · AndrolibException

Could not open apk file: {apkFile}

Error message

Could not open apk file: {apkFile}

What it means

AndrolibException thrown by ResTable.load() when ExtFile.getDirectory() fails with a DirectoryException while trying to open the APK as a read-only zip directory. It means apktool could not open the file as a zip archive at all — the file may be missing, unreadable, not a zip/APK, or a zip with an unreadable central directory.

Source

Thrown at brut.apktool/apktool-lib/src/main/java/brut/androlib/res/table/ResTable.java:97

    }

    public Collection<Integer> getFramePackageIds() {
        return mFramePackageIds;
    }

    public void load() throws AndrolibException {
        if (mMainPackage != null) {
            return;
        }

        Log.i(TAG, "Loading resource table...");
        ExtFile apkFile = mApkInfo.getApkFile();

        ZipRODirectory zipDir;
        try {
            zipDir = (ZipRODirectory) apkFile.getDirectory();
        } catch (DirectoryException ex) {
            throw new AndrolibException("Could not open apk file: " + apkFile, ex);
        }

        loadPackagesFromApk(apkFile, zipDir, true);

        ResPackageGroup pkgGroup;
        if (mPackageGroups.isEmpty()) {
            // Empty resources.arsc, create a dummy package group.
            pkgGroup = new ResPackageGroup(this, 0, "");
            mPackageGroups.put(0, pkgGroup);
        } else if (mPackageGroups.containsKey(APP_PACKAGE_ID)) {
            // Prefer the standard app package group.
            pkgGroup = mPackageGroups.get(APP_PACKAGE_ID);
        } else {
            // Fall back to the first package group in the table.
            pkgGroup = mPackageGroups.values().iterator().next();
        }

        mMainPackage = pkgGroup.getBasePackage();

View on GitHub (pinned to 79b63384d7)

Solutions

  1. Confirm the path exists and is readable, and that it is truly a zip-format APK: run unzip -t or file on it.
  2. If you have an .xapk/.apks/.zip bundle, extract the base APK (and splits) first and load that.
  3. Re-download/re-copy the APK if zip validation fails.
  4. Catch AndrolibException at the load() call site and surface the file path to the user.

Example fix

// before
new ResTable(apkInfo, config).load(); // fails on non-zip input

// after
byte[] magic = new byte[2];
try (RandomAccessFile raf = new RandomAccessFile(apk, "r")) {
    raf.readFully(magic);
}
if (magic[0] != 'P' || magic[1] != 'K') {
    throw new IllegalArgumentException("Not a zip/apk file: " + apk);
}
new ResTable(apkInfo, config).load();
Defensive patterns

Strategy: validation

Validate before calling

if (!apkFile.isFile() || !apkFile.canRead()) throw new IllegalArgumentException("Missing/unreadable: " + apkFile);
try (ZipFile zf = new ZipFile(apkFile)) {
    // opening succeeds only for a structurally valid zip
} catch (IOException e) {
    throw new IllegalArgumentException("Not a valid zip/apk: " + apkFile, e);
}

Try / catch

try {
    resTable.load();
} catch (AndrolibException ex) {
    if (String.valueOf(ex.getMessage()).contains("Could not open apk file")) {
        // wrong input format / unreadable file: fix the path or input, do not retry
    }
}

Prevention

When it happens

Trigger: Passing a file that is not an APK/zip (e.g. an xapk, apks, aab, or plain file); a path that does not exist or lacks read permission; a zip with a corrupted central directory; a directory path instead of a file.

Common situations: Users pointing apktool/androlib at an .xapk or .apks bundle (which must be extracted first); downloaded files that are actually HTML error pages; permission issues in CI containers; APKs corrupted in transfer.

Related errors


AI-assisted analysis of iBotPeaches/Apktool@79b63384d7 (2026-08-14). Data as JSON: /api/errors/79aba8e0f7f06a54. Report an issue: GitHub.