{"record":{"id":"9e86cafa83b7d1f9","repo":"shwenzhang/AndResGuard","slug":"invalid-chunk-type-expected-d-got-d","errorCode":null,"errorMessage":"Invalid chunk type: expected=%d, got=%d","messagePattern":"Invalid chunk type: expected=(.+?), got=(.+?)","errorType":"exception","errorClass":"brut.androlib.AndrolibException","httpStatus":null,"severity":"error","filePath":"AndResGuard-core/src/main/java/com/tencent/mm/androlib/res/decoder/ARSCDecoder.java","lineNumber":993,"sourceCode":"         mHeader.type\n      ));\n    }\n  }\n\n  private void nextChunkCheckType(int expectedType) throws IOException, AndrolibException {\n    nextChunk();\n    checkChunkType(expectedType);\n  }\n\n  private Header writeNextChunk(int diffSize) throws IOException, AndrolibException {\n    mHeader = Header.readAndWriteHeader(mIn, mOut, diffSize);\n    return mHeader;\n  }\n\n  private Header writeNextChunkCheck(int expectedType, int diffSize) throws IOException, AndrolibException {\n    mHeader = Header.readAndWriteHeader(mIn, mOut, diffSize);\n    if (mHeader.type != expectedType) {\n      throw new AndrolibException(String.format(\"Invalid chunk type: expected=%d, got=%d\", expectedType, mHeader.type));\n    }\n    return mHeader;\n  }\n\n  /**\n   * 为了加速，不需要处理string,id,array，这几个是肯定不是的\n   */\n  private boolean isToResguardFile(String name) {\n    return (!name.equals(\"string\") && !name.equals(\"id\") && !name.equals(\"array\"));\n  }\n\n  public static class Header {\n    public final static short TYPE_NONE = -1, TYPE_TABLE = 0x0002, TYPE_PACKAGE = 0x0200, TYPE_TYPE = 0x0201,\n       TYPE_SPEC_TYPE = 0x0202, TYPE_LIBRARY = 0x0203;\n\n    public final short type;\n    public final int chunkSize;\n","sourceCodeStart":975,"sourceCodeEnd":1011,"githubUrl":"https://github.com/shwenzhang/AndResGuard/blob/e4df245d82f27d9a2d0dd108260a3510cbaba849/AndResGuard-core/src/main/java/com/tencent/mm/androlib/res/decoder/ARSCDecoder.java#L975-L1011","documentation":"In the write/encode pass, writeNextChunkCheck reads the next chunk header, writes it through with a size adjustment (diffSize), and asserts the chunk type matches what the encoder expects next. A mismatch means the source arsc structure deviates from the decode order, so the re-serialized table would be invalid and the tool aborts.","triggerScenarios":"During re-encoding, Header.readAndWriteHeader returns a header whose type != expectedType — the write-pass chunk sequence diverges from the expected table/package/typespec/config ordering, typically due to a modified or non-standard resources.arsc.","commonSituations":"Double-obfuscated APKs, arsc rewritten by repackers, arsc built by tools emitting chunks in a different order, or corruption in the input APK.","solutions":["Run AndResGuard on the original aapt-built APK only, never on already-processed output","Rebuild resources.arsc with the official Android build tools","Upgrade AndResGuard to the latest version","Diff the arsc chunk layout with aapt dump resources to identify the unexpected chunk"],"exampleFix":"// before\napply plugin: 'AndResGuard' // reprocessing a previously guarded apk\n// after: feed the plugin the original unsigned release apk from the build pipeline","handlingStrategy":"try-catch","validationCode":"if (!originalApkChecksumMatches(buildOutput)) throw new IllegalStateException(\"Input must be the original aapt-built APK\");","typeGuard":null,"tryCatchPattern":"try {\n  encodeArsc(in, out);\n} catch (AndrolibException e) {\n  if (e.getMessage().startsWith(\"Invalid chunk type\")) {\n    logger.error(\"Write pass hit unexpected chunk; input arsc is non-standard\");\n    fallbackToNonObfuscatedBuild();\n  } else throw e;\n}","preventionTips":["Feed the plugin only original build artifacts (checksum them in CI)","Do not chain multiple resource-processing tools","Rebuild arsc with aapt/aapt2 if the error persists","Keep AndResGuard updated for new arsc layouts"],"tags":["android","arsc","re-encoding","chunk-parsing"],"backgroundTag":"unexpected-response-shape","analyzedSha":"e4df245d82f27d9a2d0dd108260a3510cbaba849","analyzedAt":"2026-09-12T17:49:07.798Z","contentChangedAt":"2026-09-12T17:49:07.798Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}