Write the correct header version based on the api level

This commit is contained in:
Ben Gruver 2013-04-30 20:43:57 -07:00
parent 4c431a7ce7
commit df8e480211
4 changed files with 27 additions and 12 deletions

View File

@ -80,6 +80,8 @@ public abstract class DexWriter<
public static final int NO_INDEX = -1; public static final int NO_INDEX = -1;
public static final int NO_OFFSET = 0; public static final int NO_OFFSET = 0;
protected final int api;
protected int stringIndexSectionOffset = NO_OFFSET; protected int stringIndexSectionOffset = NO_OFFSET;
protected int typeSectionOffset = NO_OFFSET; protected int typeSectionOffset = NO_OFFSET;
protected int protoSectionOffset = NO_OFFSET; protected int protoSectionOffset = NO_OFFSET;
@ -120,7 +122,8 @@ public abstract class DexWriter<
protected final AnnotationSection<StringKey, TypeKey, AnnotationKey, AnnotationElement, EncodedValue> annotationSection; protected final AnnotationSection<StringKey, TypeKey, AnnotationKey, AnnotationElement, EncodedValue> annotationSection;
protected final AnnotationSetSection<AnnotationKey, AnnotationSetKey> annotationSetSection; protected final AnnotationSetSection<AnnotationKey, AnnotationSetKey> annotationSetSection;
protected DexWriter(InstructionFactory<? extends Insn, BaseReference> instructionFactory, protected DexWriter(int api,
InstructionFactory<? extends Insn, BaseReference> instructionFactory,
StringSection<StringKey, StringRef> stringSection, StringSection<StringKey, StringRef> stringSection,
TypeSection<StringKey, TypeKey, TypeRef> typeSection, TypeSection<StringKey, TypeKey, TypeRef> typeSection,
ProtoSection<StringKey, TypeKey, ProtoKey, TypeListKey> protoSection, ProtoSection<StringKey, TypeKey, ProtoKey, TypeListKey> protoSection,
@ -132,6 +135,7 @@ public abstract class DexWriter<
AnnotationSection<StringKey, TypeKey, AnnotationKey, AnnotationElement, AnnotationSection<StringKey, TypeKey, AnnotationKey, AnnotationElement,
EncodedValue> annotationSection, EncodedValue> annotationSection,
AnnotationSetSection<AnnotationKey, AnnotationSetKey> annotationSetSection) { AnnotationSetSection<AnnotationKey, AnnotationSetKey> annotationSetSection) {
this.api = api;
this.instructionFactory = instructionFactory; this.instructionFactory = instructionFactory;
this.stringSection = stringSection; this.stringSection = stringSection;
this.typeSection = typeSection; this.typeSection = typeSection;
@ -1089,8 +1093,11 @@ public abstract class DexWriter<
} }
private void writeHeader(@Nonnull DexDataWriter writer, int dataOffset, int fileSize) throws IOException { private void writeHeader(@Nonnull DexDataWriter writer, int dataOffset, int fileSize) throws IOException {
// TODO: need to determine which magic value to write if (api < 14) {
writer.write(HeaderItem.MAGIC_VALUES[0]); writer.write(HeaderItem.MAGIC_VALUES[0]);
} else {
writer.write(HeaderItem.MAGIC_VALUES[1]);
}
// checksum placeholder // checksum placeholder
writer.writeInt(0); writer.writeInt(0);

View File

@ -62,11 +62,16 @@ public class DexBuilder extends DexWriter<BuilderStringReference, BuilderStringR
public static DexBuilder makeDexBuilder() { public static DexBuilder makeDexBuilder() {
BuilderContext context = new BuilderContext(); BuilderContext context = new BuilderContext();
return new DexBuilder(context); return new DexBuilder(15, context);
} }
private DexBuilder(@Nonnull BuilderContext context) { public static DexBuilder makeDexBuilder(int api) {
super(BuilderInstructionFactory.INSTANCE, context.stringPool, context.typePool, context.protoPool, BuilderContext context = new BuilderContext();
return new DexBuilder(api, context);
}
private DexBuilder(int api, @Nonnull BuilderContext context) {
super(api, BuilderInstructionFactory.INSTANCE, context.stringPool, context.typePool, context.protoPool,
context.fieldPool, context.methodPool, context.classPool, context.typeListPool, context.annotationPool, context.fieldPool, context.methodPool, context.classPool, context.typeListPool, context.annotationPool,
context.annotationSetPool); context.annotationSetPool);
this.context = context; this.context = context;

View File

@ -54,6 +54,10 @@ public class DexPool extends DexWriter<CharSequence, StringReference, CharSequen
EncodedValue, AnnotationElement, DebugItem, Instruction, ExceptionHandler> { EncodedValue, AnnotationElement, DebugItem, Instruction, ExceptionHandler> {
public static DexPool makeDexPool() { public static DexPool makeDexPool() {
return makeDexPool(15);
}
public static DexPool makeDexPool(int api) {
StringPool stringPool = new StringPool(); StringPool stringPool = new StringPool();
TypePool typePool = new TypePool(stringPool); TypePool typePool = new TypePool(stringPool);
FieldPool fieldPool = new FieldPool(stringPool, typePool); FieldPool fieldPool = new FieldPool(stringPool, typePool);
@ -65,16 +69,15 @@ public class DexPool extends DexWriter<CharSequence, StringReference, CharSequen
ClassPool classPool = new ClassPool(stringPool, typePool, fieldPool, methodPool, annotationSetPool, ClassPool classPool = new ClassPool(stringPool, typePool, fieldPool, methodPool, annotationSetPool,
typeListPool); typeListPool);
return new DexPool(stringPool, typePool, protoPool, fieldPool, methodPool, classPool, typeListPool, return new DexPool(api, stringPool, typePool, protoPool, fieldPool, methodPool, classPool, typeListPool,
annotationPool, annotationSetPool); annotationPool, annotationSetPool);
} }
private DexPool(StringPool stringPool, TypePool typePool, ProtoPool protoPool, FieldPool fieldPool, private DexPool(int api, StringPool stringPool, TypePool typePool, ProtoPool protoPool, FieldPool fieldPool,
MethodPool methodPool, ClassPool classPool, TypeListPool typeListPool, MethodPool methodPool, ClassPool classPool, TypeListPool typeListPool,
AnnotationPool annotationPool, AnnotationSetPool annotationSetPool) { AnnotationPool annotationPool, AnnotationSetPool annotationSetPool) {
super(ImmutableInstructionFactory.INSTANCE, stringPool, typePool, protoPool, fieldPool, methodPool, classPool, super(api, ImmutableInstructionFactory.INSTANCE, stringPool, typePool, protoPool, fieldPool, methodPool,
typeListPool, annotationPool, annotationSetPool); classPool, typeListPool, annotationPool, annotationSetPool);
} }
public static void writeTo(@Nonnull String path, @Nonnull org.jf.dexlib2.iface.DexFile input) throws IOException { public static void writeTo(@Nonnull String path, @Nonnull org.jf.dexlib2.iface.DexFile input) throws IOException {

View File

@ -185,7 +185,7 @@ public class main {
boolean errors = false; boolean errors = false;
final DexBuilder dexBuilder = DexBuilder.makeDexBuilder(); final DexBuilder dexBuilder = DexBuilder.makeDexBuilder(apiLevel);
ExecutorService executor = Executors.newFixedThreadPool(jobs); ExecutorService executor = Executors.newFixedThreadPool(jobs);
List<Future<Boolean>> tasks = Lists.newArrayList(); List<Future<Boolean>> tasks = Lists.newArrayList();