pxb1988/dex2jar · critical · RuntimeException
generated different file size, planned " + size + ", but is…
Error message
generated different file size, planned " + size + ", but is " + buffer.position()
What it means
DexFileWriter.toByteArray() pre-computes the dex file size, writes all sections, then asserts the output buffer position matches the plan. A mismatch means internal size bookkeeping (padding, offsets, item sizes) drifted from what was written, so it throws before fixing the header checksum.
Solutions
- Check that every custom item's calcSize/write and padding logic matches BaseItem conventions
- Update the dex-writer version — this is usually an internal invariant bug
- Dump the buffer (wrapDumpOut path) and compare section offsets against the plan to find the offending section
Defensive patterns
Strategy: try-catch
Try / catch
try { byte[] dex = dexFileWriter.toByteArray(); } catch (RuntimeException e) { if (e.getMessage().startsWith("generated different file size")) { /* enable dump, isolate offending item */ } throw e; } Prevention
- Never mutate items after planning offsets
- Follow BaseItem size/padding conventions for custom items
- Keep dex-writer up to date; regression-test full toDex() output
When it happens
Trigger: Calling toDex()/toByteArray() after adding items whose write() output does not match their calculated size (custom BaseItem subclasses, wrong alignment padding, duplicate/unregistered items).
Common situations: Extending the dex-writer with custom items or patched section writers; library bugs after adding unusual constructs (e.g. very large string/type pools, annotation with odd-length values).
Understand the failure class
Background: "This is a bug, please report it": internal invariant violations, unreachable panics, and SNH errors explained — this error's family across 47 libraries.
Related errors
- section.sectionType + " start with different position…
- Odex unsupported.
- Magic unsupported.
- Endian_tag unsupported
- Encountered RESTART_LOCAL on new v
AI-assisted analysis of pxb1988/dex2jar@b5bda4fb49 (2026-09-08).
Data as JSON: /api/errors/ac2972536b77b338.
Report an issue: GitHub.
Appendix: source
Thrown at dex-writer/src/main/java/com/googlecode/d2j/dex/writer/DexFileWriter.java:242
public byte[] toByteArray() {
// init structure for writing
buildMapListItem();
// place all item into file, we can know the size now
final int size = place();
ByteBuffer buffer = ByteBuffer.allocate(size);
DataOut out = new ByteBufferOut(buffer);
if (DEBUG) {
out = wrapDumpOut(out);
}
// write it
write(out);
if (size != buffer.position()) {
throw new RuntimeException("generated different file size, planned " + size + ", but is " + buffer.position());
}
// update the CRC/ sha1 checksum in dex header
updateChecksum(buffer, size);
return buffer.array();
}
public static void updateChecksum(ByteBuffer buffer, int size) {
byte[] data = buffer.array();
MessageDigest digest;
try {
digest = MessageDigest.getInstance("SHA-1");
} catch (NoSuchAlgorithmException e) {
throw new AssertionError();
}
digest.update(data, 32, size - 32);View on GitHub (pinned to b5bda4fb49)