From 40bbf5c347fde4ccba4b1875e63e94a84354b7af Mon Sep 17 00:00:00 2001 From: Alex Light Date: Fri, 1 Apr 2016 14:24:48 -0700 Subject: [PATCH] Dex version 37 support Make dexlib able to read and write version 37 dex files. Bug: 27809626 Change-Id: I3d0ca6201c7abe7763d2fb925e9ee1edbef24230 --- .../jf/dexlib2/dexbacked/raw/HeaderItem.java | 24 +++++++++++++++++-- .../java/org/jf/dexlib2/writer/DexWriter.java | 4 ++-- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/raw/HeaderItem.java b/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/raw/HeaderItem.java index 3499eadca..531ff4614 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/raw/HeaderItem.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/raw/HeaderItem.java @@ -42,9 +42,14 @@ public class HeaderItem { public static final int ITEM_SIZE = 0x70; + /** + * The magic numbers for dex files. + * + * They are: "dex\n035\0" and "dex\n037\0". + */ public static final byte[][] MAGIC_VALUES= new byte[][] { new byte[]{0x64, 0x65, 0x78, 0x0a, 0x30, 0x33, 0x35, 0x00}, - new byte[]{0x64, 0x65, 0x78, 0x0a, 0x30, 0x33, 0x36, 0x00}}; + new byte[]{0x64, 0x65, 0x78, 0x0a, 0x30, 0x33, 0x37, 0x00}}; public static final int LITTLE_ENDIAN_TAG = 0x12345678; public static final int BIG_ENDIAN_TAG = 0x78563412; @@ -225,6 +230,21 @@ private static String getEndianText(int endianTag) { return "Invalid"; } + + /** + * Get the higest magic number supported by Android for this api level. + * @return The dex file magic number + */ + public static byte[] getMagicForApi(int api) { + if (api < 24) { + // Prior to Android N we only support dex version 035. + return HeaderItem.MAGIC_VALUES[0]; + } else { + // On android N and later we support dex version 037. + return HeaderItem.MAGIC_VALUES[1]; + } + } + private static int getVersion(byte[] buf, int offset) { if (buf.length - offset < 8) { return 0; @@ -241,7 +261,7 @@ private static int getVersion(byte[] buf, int offset) { } } if (matches) { - return i==0?35:36; + return i==0?35:37; } } return 0; 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 9d353982e..4e81f7fa6 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/writer/DexWriter.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/writer/DexWriter.java @@ -1221,8 +1221,8 @@ private void writeMapItem(@Nonnull DexDataWriter writer, int type, int size, int } private void writeHeader(@Nonnull DexDataWriter writer, int dataOffset, int fileSize) throws IOException { - // always write the 035 version, there's no reason to use the 036 version for now - writer.write(HeaderItem.MAGIC_VALUES[0]); + // Write the appropriate header. + writer.write(HeaderItem.getMagicForApi(opcodes.api)); // checksum placeholder writer.writeInt(0);