{"record":{"id":"a398c97fe122071f","repo":"Tencent/matrix","slug":"zipalign-apk-occur-error","errorCode":null,"errorMessage":"zipalign apk occur error!","messagePattern":"zipalign apk occur error!","errorType":"exception","errorClass":"GradleException","httpStatus":null,"severity":"error","filePath":"matrix/matrix-android/matrix-gradle-plugin/src/main/java/com/tencent/matrix/shrinker/ApkUtil.java","lineNumber":383,"sourceCode":"        commandList.add(apkFilePath);\n        processBuilder.command(commandList);\n        //Log.i(TAG, \"%s\", processBuilder.command())\n        Process process = processBuilder.start();\n//        process.waitForProcessOutput(System.out, System.err);\n        waitForProcessOutput(process);\n        if (process.exitValue() != 0) {\n            throw new GradleException(\"sign apk occur error!\");\n        }\n    }\n\n    public static void zipAlignApk(String inputFile, String outputFile, String zipAlignPath) throws GradleException, IOException, InterruptedException {\n        ProcessBuilder processBuilder = new ProcessBuilder();\n        processBuilder.command(zipAlignPath, \"-f\", \"4\", inputFile, outputFile);\n        Process process = processBuilder.start();\n//        process.waitForProcessOutput(System.out, System.err);\n        waitForProcessOutput(process);\n        if (process.exitValue() != 0) {\n            throw new GradleException(\"zipalign apk occur error!\");\n        }\n    }\n\n}","sourceCodeStart":365,"sourceCodeEnd":387,"githubUrl":"https://github.com/Tencent/matrix/blob/3b8293bd65d47eeea7caf1f32a3a5d4d5eab60e7/matrix/matrix-android/matrix-gradle-plugin/src/main/java/com/tencent/matrix/shrinker/ApkUtil.java#L365-L387","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Run the same zipalign command manually to see the tool's actual error message","Verify zipalignPath points to an existing executable from a build-tools version compatible with your target SDK","Ensure the input APK exists and the output path's parent directory is writable","Make sure input and output file paths are different and valid"],"exampleFix":"// before\nzipAlignApk(input, output, 'zipalign')  // not on PATH\n// after\nzipAlignApk(input, output, '${android.sdkDirectory}/build-tools/33.0.0/zipalign')","handlingStrategy":"validation","validationCode":"def z = new File(zipAlignPath)\nif (!z.exists() || !z.canExecute()) throw new GradleException('zipalign not found: ' + zipAlignPath)\nif (!new File(inputFile).exists()) throw new GradleException('input APK missing')","typeGuard":null,"tryCatchPattern":"try {\n    ApkUtil.zipAlignApk(input, output, zipAlignPath)\n} catch (GradleException e) {\n    logger.error('zipalign failed; check zipalign path and build-tools compatibility', e)\n    throw e\n}","preventionTips":["Resolve zipalign from android.sdkDirectory build-tools instead of a hardcoded path","Keep build-tools version consistent across developer machines and CI","Confirm input/output paths differ and output dir is writable"],"tags":["gradle-plugin","zipalign","external-process","android-build-tools"],"backgroundTag":"command-not-found","analyzedSha":"3b8293bd65d47eeea7caf1f32a3a5d4d5eab60e7","analyzedAt":"2026-09-08T08:01:39.722Z","contentChangedAt":"2026-09-08T08:01:39.722Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}