Tencent/matrix · error · GradleException

zipalign apk occur error!

Error message

zipalign apk occur error!

What it means

Thrown by ApkUtil.zipAlignApk after running the external zipalign tool with '-f 4 inputFile outputFile'; a non-zero exit status raises GradleException('zipalign apk occur error!'). The zipalign tool's own output, drained by waitForProcessOutput, contains the actual reason. It means the APK could not be 4-byte aligned.

Solutions

  1. Run the same zipalign command manually to see the tool's actual error message
  2. Verify zipalignPath points to an existing executable from a build-tools version compatible with your target SDK
  3. Ensure the input APK exists and the output path's parent directory is writable
  4. Make sure input and output file paths are different and valid

Example fix

// before
zipAlignApk(input, output, 'zipalign')  // not on PATH
// after
zipAlignApk(input, output, '${android.sdkDirectory}/build-tools/33.0.0/zipalign')
Defensive patterns

Strategy: validation

Validate before calling

def z = new File(zipAlignPath)
if (!z.exists() || !z.canExecute()) throw new GradleException('zipalign not found: ' + zipAlignPath)
if (!new File(inputFile).exists()) throw new GradleException('input APK missing')

Try / catch

try {
    ApkUtil.zipAlignApk(input, output, zipAlignPath)
} catch (GradleException e) {
    logger.error('zipalign failed; check zipalign path and build-tools compatibility', e)
    throw e
}

Prevention

When it happens

Trigger: Calling zipAlignApk(...) when the zipalign binary path is wrong or the binary doesn't match the build-tools version, the input APK is missing/corrupt, the output file can't be written, or the input and output paths collide.

Common situations: Developers pointing the plugin at a zipalign from an incompatible Android build-tools version; running on machines without Android SDK build-tools installed; read-only output directories on CI.

Related errors


AI-assisted analysis of Tencent/matrix@3b8293bd65 (2026-09-08). Data as JSON: /api/errors/a398c97fe122071f. Report an issue: GitHub.

Appendix: source

Thrown at matrix/matrix-android/matrix-gradle-plugin/src/main/java/com/tencent/matrix/shrinker/ApkUtil.java:383

        commandList.add(apkFilePath);
        processBuilder.command(commandList);
        //Log.i(TAG, "%s", processBuilder.command())
        Process process = processBuilder.start();
//        process.waitForProcessOutput(System.out, System.err);
        waitForProcessOutput(process);
        if (process.exitValue() != 0) {
            throw new GradleException("sign apk occur error!");
        }
    }

    public static void zipAlignApk(String inputFile, String outputFile, String zipAlignPath) throws GradleException, IOException, InterruptedException {
        ProcessBuilder processBuilder = new ProcessBuilder();
        processBuilder.command(zipAlignPath, "-f", "4", inputFile, outputFile);
        Process process = processBuilder.start();
//        process.waitForProcessOutput(System.out, System.err);
        waitForProcessOutput(process);
        if (process.exitValue() != 0) {
            throw new GradleException("zipalign apk occur error!");
        }
    }

}

View on GitHub (pinned to 3b8293bd65)