From df8e4802115fadd6bb67ba405aba2db885abbd55 Mon Sep 17 00:00:00 2001 From: Ben Gruver Date: Tue, 30 Apr 2013 20:43:57 -0700 Subject: [PATCH] Write the correct header version based on the api level --- .../main/java/org/jf/dexlib2/writer/DexWriter.java | 13 ++++++++++--- .../org/jf/dexlib2/writer/builder/DexBuilder.java | 11 ++++++++--- .../java/org/jf/dexlib2/writer/pool/DexPool.java | 13 ++++++++----- smali/src/main/java/org/jf/smali/main.java | 2 +- 4 files changed, 27 insertions(+), 12 deletions(-) diff --git a/dexlib2/src/main/java/org/jf/dexlib2/writer/DexWriter.java b/dexlib2/src/main/java/org/jf/dexlib2/writer/DexWriter.java index 63289fd9..7378673a 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/writer/DexWriter.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/writer/DexWriter.java @@ -80,6 +80,8 @@ public abstract class DexWriter< public static final int NO_INDEX = -1; public static final int NO_OFFSET = 0; + protected final int api; + protected int stringIndexSectionOffset = NO_OFFSET; protected int typeSectionOffset = NO_OFFSET; protected int protoSectionOffset = NO_OFFSET; @@ -120,7 +122,8 @@ public abstract class DexWriter< protected final AnnotationSection annotationSection; protected final AnnotationSetSection annotationSetSection; - protected DexWriter(InstructionFactory instructionFactory, + protected DexWriter(int api, + InstructionFactory instructionFactory, StringSection stringSection, TypeSection typeSection, ProtoSection protoSection, @@ -132,6 +135,7 @@ public abstract class DexWriter< AnnotationSection annotationSection, AnnotationSetSection annotationSetSection) { + this.api = api; this.instructionFactory = instructionFactory; this.stringSection = stringSection; this.typeSection = typeSection; @@ -1089,8 +1093,11 @@ public abstract class DexWriter< } private void writeHeader(@Nonnull DexDataWriter writer, int dataOffset, int fileSize) throws IOException { - // TODO: need to determine which magic value to write - writer.write(HeaderItem.MAGIC_VALUES[0]); + if (api < 14) { + writer.write(HeaderItem.MAGIC_VALUES[0]); + } else { + writer.write(HeaderItem.MAGIC_VALUES[1]); + } // checksum placeholder writer.writeInt(0); diff --git a/dexlib2/src/main/java/org/jf/dexlib2/writer/builder/DexBuilder.java b/dexlib2/src/main/java/org/jf/dexlib2/writer/builder/DexBuilder.java index c5777eee..913765f3 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/writer/builder/DexBuilder.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/writer/builder/DexBuilder.java @@ -62,11 +62,16 @@ public class DexBuilder extends DexWriter { public static DexPool makeDexPool() { + return makeDexPool(15); + } + + public static DexPool makeDexPool(int api) { StringPool stringPool = new StringPool(); TypePool typePool = new TypePool(stringPool); FieldPool fieldPool = new FieldPool(stringPool, typePool); @@ -65,16 +69,15 @@ public class DexPool extends DexWriter> tasks = Lists.newArrayList();