Class Dex


  • public final class Dex
    extends Object
    The bytes of a dex file in memory for reading and writing. All int offsets are unsigned.
    • Constructor Detail

      • Dex

        public Dex​(byte[] data)
            throws IOException
        Creates a new dex that reads from data. It is an error to modify data after using it to create a dex buffer.
        Throws:
        IOException
      • Dex

        public Dex​(int byteCount)
            throws IOException
        Creates a new empty dex of the specified size.
        Throws:
        IOException
    • Method Detail

      • create

        public static Dex create​(ByteBuffer data)
                          throws IOException
        Creates a new dex from the contents of bytes. This API supports both .dex and .odex input. Calling this constructor transfers ownership of bytes to the returned Dex: it is an error to access the buffer after calling this method.
        Throws:
        IOException
      • appendSection

        public Dex.Section appendSection​(int maxByteCount,
                                         String name)
      • getLength

        public int getLength()
      • getNextSectionStart

        public int getNextSectionStart()
      • getBytes

        public byte[] getBytes()
        Returns a copy of the the bytes of this dex.
      • readTypeList

        public TypeList readTypeList​(int offset)
      • computeSignature

        public byte[] computeSignature()
                                throws IOException
        Returns the signature of all but the first 32 bytes of this dex. The first 32 bytes of dex files are not specified to be included in the signature.
        Throws:
        IOException
      • computeChecksum

        public int computeChecksum()
                            throws IOException
        Returns the checksum of all but the first 12 bytes of dex.
        Throws:
        IOException
      • writeHashes

        public void writeHashes()
                         throws IOException
        Generates the signature and checksum of the dex file out and writes them to the file.
        Throws:
        IOException
      • nameIndexFromFieldIndex

        public int nameIndexFromFieldIndex​(int fieldIndex)
        Look up a field id name index from a field index. Cheaper than: fieldIds().get(fieldDexIndex).getNameIndex();
      • findStringIndex

        public int findStringIndex​(String s)
      • findTypeIndex

        public int findTypeIndex​(String descriptor)
      • findFieldIndex

        public int findFieldIndex​(FieldId fieldId)
      • findMethodIndex

        public int findMethodIndex​(MethodId methodId)
      • findClassDefIndexFromTypeIndex

        public int findClassDefIndexFromTypeIndex​(int typeIndex)
      • typeIndexFromFieldIndex

        public int typeIndexFromFieldIndex​(int fieldIndex)
        Look up a field id type index from a field index. Cheaper than: fieldIds().get(fieldDexIndex).getTypeIndex();
      • declaringClassIndexFromMethodIndex

        public int declaringClassIndexFromMethodIndex​(int methodIndex)
        Look up a method id declaring class index from a method index. Cheaper than: methodIds().get(methodIndex).getDeclaringClassIndex();
      • nameIndexFromMethodIndex

        public int nameIndexFromMethodIndex​(int methodIndex)
        Look up a method id name index from a method index. Cheaper than: methodIds().get(methodIndex).getNameIndex();
      • parameterTypeIndicesFromMethodIndex

        public short[] parameterTypeIndicesFromMethodIndex​(int methodIndex)
        Look up a parameter type ids from a method index. Cheaper than: readTypeList(protoIds.get(methodIds().get(methodDexIndex).getProtoIndex()).getParametersOffset()).getTypes();
      • returnTypeIndexFromMethodIndex

        public int returnTypeIndexFromMethodIndex​(int methodIndex)
        Look up a method id return type index from a method index. Cheaper than: protoIds().get(methodIds().get(methodDexIndex).getProtoIndex()).getReturnTypeIndex();
      • descriptorIndexFromTypeIndex

        public int descriptorIndexFromTypeIndex​(int typeIndex)
        Look up a descriptor index from a type index. Cheaper than: open(tableOfContents.typeIds.off + (index * SizeOf.TYPE_ID_ITEM)).readInt();
      • typeIndexFromClassDefIndex

        public int typeIndexFromClassDefIndex​(int classDefIndex)
        Look up a type index index from a class def index.
      • annotationDirectoryOffsetFromClassDefIndex

        public int annotationDirectoryOffsetFromClassDefIndex​(int classDefIndex)
        Look up an annotation directory offset from a class def index.
      • interfaceTypeIndicesFromClassDefIndex

        public short[] interfaceTypeIndicesFromClassDefIndex​(int classDefIndex)
        Look up interface types indices from a return type index from a method index. Cheaper than: ...getClassDef(classDefIndex).getInterfaces();