2 * Copyright (C) 2011 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com.android.dx.dex;
20 * Constants that show up in and are otherwise related to {@code .dex}
21 * files, and helper methods for same.
23 public final class DexFormat {
24 private DexFormat() {}
27 * API level to target in order to produce the most modern file
30 public static final int API_CURRENT = 14;
32 /** API level to target in order to suppress extended opcode usage */
33 public static final int API_NO_EXTENDED_OPCODES = 13;
36 * file name of the primary {@code .dex} file inside an
37 * application or library {@code .jar} file
39 public static final String DEX_IN_JAR_NAME = "classes.dex";
41 /** common prefix for all dex file "magic numbers" */
42 public static final String MAGIC_PREFIX = "dex\n";
44 /** common suffix for all dex file "magic numbers" */
45 public static final String MAGIC_SUFFIX = "\0";
47 /** dex file version number for the current format variant */
48 public static final String VERSION_CURRENT = "036";
50 /** dex file version number for API level 13 and earlier */
51 public static final String VERSION_FOR_API_13 = "035";
54 * value used to indicate endianness of file contents
56 public static final int ENDIAN_TAG = 0x12345678;
59 * Returns the API level corresponding to the given magic number,
60 * or {@code -1} if the given array is not a well-formed dex file
63 public static int magicToApi(byte[] magic) {
64 if (magic.length != 8) {
68 if ((magic[0] != 'd') || (magic[1] != 'e') || (magic[2] != 'x') || (magic[3] != '\n') ||
73 String version = "" + ((char) magic[4]) + ((char) magic[5]) +((char) magic[6]);
75 if (version.equals(VERSION_CURRENT)) {
77 } else if (version.equals(VERSION_FOR_API_13)) {
85 * Returns the magic number corresponding to the given target API level.
87 public static String apiToMagic(int targetApiLevel) {
90 if (targetApiLevel >= API_CURRENT) {
91 version = VERSION_CURRENT;
93 version = VERSION_FOR_API_13;
96 return MAGIC_PREFIX + version + MAGIC_SUFFIX;