Class DexFileFactory

java.lang.Object
org.jf.dexlib2.DexFileFactory

public final class DexFileFactory extends Object
  • Constructor Details

    • DexFileFactory

      private DexFileFactory()
  • Method Details

    • loadDexFile

      @Nonnull public static DexBackedDexFile loadDexFile(@Nonnull String path, @Nullable Opcodes opcodes) throws IOException
      Throws:
      IOException
    • loadDexFile

      @Nonnull public static DexBackedDexFile loadDexFile(@Nonnull File file, @Nullable Opcodes opcodes) throws IOException
      Loads a dex/apk/odex/oat file. For oat files with multiple dex files, the first will be opened. For zip/apk files, the "classes.dex" entry will be opened.
      Parameters:
      file - The file to open
      opcodes - The set of opcodes to use
      Returns:
      A DexBackedDexFile for the given file
      Throws:
      DexFileFactory.UnsupportedOatVersionException - If file refers to an unsupported oat file
      DexFileFactory.DexFileNotFoundException - If file does not exist, if file is a zip file but does not have a "classes.dex" entry, or if file is an oat file that has no dex entries.
      DexFileFactory.UnsupportedFileTypeException - If file is not a valid dex/zip/odex/oat file, or if the "classes.dex" entry in a zip file is not a valid dex file
      IOException
    • loadDexEntry

      public static MultiDexContainer.DexEntry<? extends DexBackedDexFile> loadDexEntry(@Nonnull File file, @Nonnull String dexEntry, boolean exactMatch, @Nullable Opcodes opcodes) throws IOException
      Loads a dex entry from a container format (zip/oat) This has two modes of operation, depending on the exactMatch parameter. When exactMatch is true, it will only load an entry whose name exactly matches that provided by the dexEntry parameter. When exactMatch is false, then it will search for any entry that dexEntry is a path suffix of. "path suffix" meaning all the path components in dexEntry must fully match the corresponding path components in the entry name, but some path components at the beginning of entry name can be missing. For example, if an oat file contains a "/system/framework/framework.jar:classes2.dex" entry, then the following will match (not an exhaustive list): "/system/framework/framework.jar:classes2.dex" "system/framework/framework.jar:classes2.dex" "framework/framework.jar:classes2.dex" "framework.jar:classes2.dex" "classes2.dex" Note that partial path components specifically don't match. So something like "work/framework.jar:classes2.dex" would not match. If dexEntry contains an initial slash, it will be ignored for purposes of this suffix match -- but not when performing an exact match. If multiple entries match the given dexEntry, a MultipleMatchingDexEntriesException will be thrown
      Parameters:
      file - The container file. This must be either a zip (apk) file or an oat file.
      dexEntry - The name of the entry to load. This can either be the exact entry name, if exactMatch is true, or it can be a path suffix.
      exactMatch - If true, dexE
      opcodes - The set of opcodes to use
      Returns:
      A DexBackedDexFile for the given entry
      Throws:
      DexFileFactory.UnsupportedOatVersionException - If file refers to an unsupported oat file
      DexFileFactory.DexFileNotFoundException - If the file does not exist, or if no matching entry could be found
      DexFileFactory.UnsupportedFileTypeException - If file is not a valid zip/oat file, or if the matching entry is not a valid dex file
      DexFileFactory.MultipleMatchingDexEntriesException - If multiple entries match the given dexEntry
      IOException
    • loadDexContainer

      public static MultiDexContainer<? extends DexBackedDexFile> loadDexContainer(@Nonnull File file, @Nullable Opcodes opcodes) throws IOException
      Loads a file containing 1 or more dex files If the given file is a dex or odex file, it will return a MultiDexContainer containing that single entry. Otherwise, for an oat or zip file, it will return an OatFile or ZipDexContainer respectively.
      Parameters:
      file - The file to open
      opcodes - The set of opcodes to use
      Returns:
      A MultiDexContainer
      Throws:
      DexFileFactory.DexFileNotFoundException - If the given file does not exist
      DexFileFactory.UnsupportedFileTypeException - If the given file is not a valid dex/zip/odex/oat file
      IOException
    • writeDexFile

      public static void writeDexFile(@Nonnull String path, @Nonnull DexFile dexFile) throws IOException
      Writes a DexFile out to disk
      Parameters:
      path - The path to write the dex file to
      dexFile - a DexFile to write
      Throws:
      IOException
    • fullEntryMatch

      private static boolean fullEntryMatch(@Nonnull String entry, @Nonnull String targetEntry)
      Matches two entries fully, ignoring any initial slash, if any
    • partialEntryMatch

      private static boolean partialEntryMatch(String entry, String targetEntry)
      Performs a partial match against entry and targetEntry. This is considered a partial match if targetEntry is a suffix of entry, and if the suffix starts on a path "part" (ignoring the initial separator, if any). '/' and ':' and '!' are considered separators for this. So entry="/blah/blah/something.dex" and targetEntry="lah/something.dex" shouldn't match, but both targetEntry="blah/something.dex" and "/blah/something.dex" should match.