本文整理汇总了Java中com.android.dex.Dex类的典型用法代码示例。如果您正苦于以下问题:Java Dex类的具体用法?Java Dex怎么用?Java Dex使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Dex类属于com.android.dex包,在下文中一共展示了Dex类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: mergeDexes
import com.android.dex.Dex; //导入依赖的package包/类
private Dex mergeDexes() throws IOException {
mergeStringIds();
mergeTypeIds();
mergeTypeLists();
mergeProtoIds();
mergeFieldIds();
mergeMethodIds();
mergeAnnotations();
unionAnnotationSetsAndDirectories();
mergeClassDefs();
// write the header
contentsOut.header.off = 0;
contentsOut.header.size = 1;
contentsOut.fileSize = dexOut.getLength();
contentsOut.computeSizesFromOffsets();
contentsOut.writeHeader(headerOut);
contentsOut.writeMap(mapListOut);
// generate and write the hashes
dexOut.writeHashes();
return dexOut;
}
示例2: transformClassDef
import com.android.dex.Dex; //导入依赖的package包/类
/**
* Reads a class_def_item beginning at {@code in} and writes the index and
* data.
*/
private void transformClassDef(Dex in, ClassDef classDef, IndexMap indexMap) {
idsDefsOut.assertFourByteAligned();
idsDefsOut.writeInt(classDef.getTypeIndex());
idsDefsOut.writeInt(classDef.getAccessFlags());
idsDefsOut.writeInt(classDef.getSupertypeIndex());
idsDefsOut.writeInt(classDef.getInterfacesOffset());
int sourceFileIndex = indexMap.adjustString(classDef.getSourceFileIndex());
idsDefsOut.writeInt(sourceFileIndex);
int annotationsOff = classDef.getAnnotationsOffset();
idsDefsOut.writeInt(indexMap.adjustAnnotationDirectory(annotationsOff));
int classDataOff = classDef.getClassDataOffset();
if (classDataOff == 0) {
idsDefsOut.writeInt(0);
} else {
idsDefsOut.writeInt(classDataOut.getPosition());
ClassData classData = in.readClassData(classDef);
transformClassData(in, classData, indexMap);
}
int staticValuesOff = classDef.getStaticValuesOffset();
idsDefsOut.writeInt(indexMap.adjustStaticValues(staticValuesOff));
}
示例3: transformClassData
import com.android.dex.Dex; //导入依赖的package包/类
private void transformClassData(Dex in, ClassData classData, IndexMap indexMap) {
contentsOut.classDatas.size++;
ClassData.Field[] staticFields = classData.getStaticFields();
ClassData.Field[] instanceFields = classData.getInstanceFields();
ClassData.Method[] directMethods = classData.getDirectMethods();
ClassData.Method[] virtualMethods = classData.getVirtualMethods();
classDataOut.writeUleb128(staticFields.length);
classDataOut.writeUleb128(instanceFields.length);
classDataOut.writeUleb128(directMethods.length);
classDataOut.writeUleb128(virtualMethods.length);
transformFields(indexMap, staticFields);
transformFields(indexMap, instanceFields);
transformMethods(in, indexMap, directMethods);
transformMethods(in, indexMap, virtualMethods);
}
示例4: transformMethods
import com.android.dex.Dex; //导入依赖的package包/类
private void transformMethods(Dex in, IndexMap indexMap, ClassData.Method[] methods) {
int lastOutMethodIndex = 0;
for (ClassData.Method method : methods) {
int outMethodIndex = indexMap.adjustMethod(method.getMethodIndex());
classDataOut.writeUleb128(outMethodIndex - lastOutMethodIndex);
lastOutMethodIndex = outMethodIndex;
classDataOut.writeUleb128(method.getAccessFlags());
if (method.getCodeOffset() == 0) {
classDataOut.writeUleb128(0);
} else {
codeOut.alignToFourBytesWithZeroFill();
classDataOut.writeUleb128(codeOut.getPosition());
transformCode(in, in.readCode(method), indexMap);
}
}
}
示例5: IndexMap
import com.android.dex.Dex; //导入依赖的package包/类
public IndexMap(Dex target, TableOfContents tableOfContents) {
this.target = target;
this.stringIds = new int[tableOfContents.stringIds.size];
this.typeIds = new short[tableOfContents.typeIds.size];
this.protoIds = new short[tableOfContents.protoIds.size];
this.fieldIds = new short[tableOfContents.fieldIds.size];
this.methodIds = new short[tableOfContents.methodIds.size];
this.typeListOffsets = new HashMap<Integer, Integer>();
this.annotationOffsets = new HashMap<Integer, Integer>();
this.annotationSetOffsets = new HashMap<Integer, Integer>();
this.annotationSetRefListOffsets = new HashMap<Integer, Integer>();
this.annotationDirectoryOffsets = new HashMap<Integer, Integer>();
this.staticValuesOffsets = new HashMap<Integer, Integer>();
/*
* A type list, annotation set, annotation directory, or static value at
* offset 0 is always empty. Always map offset 0 to 0.
*/
this.typeListOffsets.put(0, 0);
this.annotationSetOffsets.put(0, 0);
this.annotationDirectoryOffsets.put(0, 0);
this.staticValuesOffsets.put(0, 0);
}
示例6: mergeLibraryDexBuffers
import com.android.dex.Dex; //导入依赖的package包/类
/**
* Merges the dex files in library jars. If multiple dex files define the
* same type, this fails with an exception.
*/
private static byte[] mergeLibraryDexBuffers(byte[] outArray) throws IOException {
for (byte[] libraryDex : libraryDexBuffers) {
if (outArray == null) {
outArray = libraryDex;
continue;
}
Dex a = new Dex(outArray);
Dex b = new Dex(libraryDex);
Dex ab = new DexMerger(a, b, CollisionPolicy.FAIL).merge();
outArray = ab.getBytes();
}
return outArray;
}
示例7: findAssignableTypes
import com.android.dex.Dex; //导入依赖的package包/类
/**
* Returns the set of types that can be assigned to {@code typeIndex}.
*/
private Set<Integer> findAssignableTypes(Dex dex, int typeIndex) {
Set<Integer> assignableTypes = new HashSet<Integer>();
assignableTypes.add(typeIndex);
for (ClassDef classDef : dex.classDefs()) {
if (assignableTypes.contains(classDef.getSupertypeIndex())) {
assignableTypes.add(classDef.getTypeIndex());
continue;
}
for (int implemented : classDef.getInterfaces()) {
if (assignableTypes.contains(implemented)) {
assignableTypes.add(classDef.getTypeIndex());
break;
}
}
}
return assignableTypes;
}
示例8: defineClass
import com.android.dex.Dex; //导入依赖的package包/类
/**
* {@inheritDoc}
*/
@Override
public Class<?> defineClass(String name, byte[] data) {
try {
DexOptions dexOptions = new DexOptions();
DexFile dexFile = new DexFile(dexOptions);
DirectClassFile classFile = new DirectClassFile(data, name.replace('.', '/') + ".class", true);
classFile.setAttributeFactory(StdAttributeFactory.THE_ONE);
classFile.getMagic();
dexFile.add(CfTranslator.translate(classFile, null, new CfOptions(), dexOptions, dexFile));
Dex dex = new Dex(dexFile.toDex(null, false));
Dex oldDex = getLastDex();
if (oldDex != null) {
dex = new DexMerger(new Dex[]{dex, oldDex}, CollisionPolicy.KEEP_FIRST).merge();
}
return loadClass(dex, name);
} catch (IOException | ClassNotFoundException e) {
throw new FatalLoadingException(e);
}
}
示例9: loadClass
import com.android.dex.Dex; //导入依赖的package包/类
/**
* Try to load a class. This will search all defined classes, all loaded jars and the parent class loader.
*
* @param name the name of the class to load
* @param resolve ignored
* @return the class
* @throws ClassNotFoundException if the class could not be found in any of the locations
*/
@Override
public Class<?> loadClass(String name, boolean resolve)
throws ClassNotFoundException {
Class<?> loadedClass = findLoadedClass(name);
if (loadedClass == null) {
Dex dex = getLastDex();
if (dex != null) {
loadedClass = loadClass(dex, name);
}
if (loadedClass == null) {
loadedClass = getParent().loadClass(name);
}
}
return loadedClass;
}
示例10: mergeDexes
import com.android.dex.Dex; //导入依赖的package包/类
private Dex mergeDexes() throws IOException {
mergeStringIds();
mergeTypeIds();
mergeTypeLists();
mergeProtoIds();
mergeFieldIds();
mergeMethodIds();
mergeAnnotations();
unionAnnotationSetsAndDirectories();
mergeClassDefs();
// write the header
contentsOut.header.off = 0;
contentsOut.header.size = 1;
contentsOut.fileSize = dexOut.getLength();
contentsOut.computeSizesFromOffsets();
contentsOut.writeHeader(headerOut, mergeApiLevels());
contentsOut.writeMap(mapListOut);
// generate and write the hashes
dexOut.writeHashes();
return dexOut;
}
示例11: loadFromClassFile
import com.android.dex.Dex; //导入依赖的package包/类
private static Dex loadFromClassFile(File file) throws IOException, DecodeException {
File outFile = File.createTempFile("jadx-tmp-", System.nanoTime() + ".jar");
outFile.deleteOnExit();
FileOutputStream out = null;
JarOutputStream jo = null;
try {
out = new FileOutputStream(outFile);
jo = new JarOutputStream(out);
String clsName = AsmUtils.getNameFromClassFile(file);
if (clsName == null) {
throw new IOException("Can't read class name from file: " + file);
}
FileUtils.addFileToJar(jo, file, clsName + ".class");
} finally {
if (jo != null) {
jo.close();
}
if (out != null) {
out.close();
}
}
return loadFromJar(outFile);
}
示例12: processDexEntry
import com.android.dex.Dex; //导入依赖的package包/类
private void processDexEntry(ZipFile zip, ZipEntry entry) throws IOException {
String filename = entry.getName();
checkState(filename.endsWith(".class.dex"),
"%s isn't a dex archive: %s", zip.getName(), filename);
checkState(entry.getMethod() == ZipEntry.STORED, "Expect to process STORED: %s", filename);
try (InputStream entryStream = zip.getInputStream(entry)) {
// We don't want to use the Dex(InputStream) constructor because it closes the stream,
// which will break the for loop, and it has its own bespoke way of reading the file into
// a byte buffer before effectively calling Dex(byte[]) anyway.
// TODO(kmb) since entry is stored, mmap content and give to Dex(ByteBuffer) and output zip
byte[] content = new byte[(int) entry.getSize()];
ByteStreams.readFully(entryStream, content); // throws if file is smaller than expected
checkState(entryStream.read() == -1,
"Too many bytes in jar entry %s, expected %s", entry, entry.getSize());
Dex dexFile = new Dex(content);
if (tracker.track(dexFile)) {
nextShard();
tracker.track(dexFile);
}
curOut.writeAsync(entry, content);
}
}
示例13: processDexFiles
import com.android.dex.Dex; //导入依赖的package包/类
private static void processDexFiles(
ZipFile zip, Iterable<ZipEntry> filesToProcess, HashSet<String> seen, DexFileAggregator out)
throws IOException {
for (ZipEntry entry : filesToProcess) {
String filename = entry.getName();
checkState(filename.endsWith(".dex"), "Input shouldn't contain .class files: %s", filename);
if (!seen.add(filename)) {
continue; // pick first occurrence of each file to match how JVM treats dupes on classpath
}
try (InputStream content = zip.getInputStream(entry)) {
// We don't want to use the Dex(InputStream) constructor because it closes the stream,
// which will break the for loop, and it has its own bespoke way of reading the file into
// a byte buffer before effectively calling Dex(byte[]) anyway.
out.add(new Dex(ByteStreams.toByteArray(content)));
}
}
}
示例14: testMultidex_underLimitWritesOneShard
import com.android.dex.Dex; //导入依赖的package包/类
@Test
public void testMultidex_underLimitWritesOneShard() throws Exception {
DexFileAggregator dexer =
new DexFileAggregator(
new DxContext(),
dest,
newDirectExecutorService(),
MultidexStrategy.BEST_EFFORT,
/*forceJumbo=*/ false,
DEX_LIMIT,
WASTE,
DexFileMergerTest.DEX_PREFIX);
Dex dex2 = DexFiles.toDex(convertClass(ByteStreams.class));
dexer.add(dex);
dexer.add(dex2);
verify(dest, times(0)).addFile(any(ZipEntry.class), any(Dex.class));
dexer.close();
verify(dest).addFile(any(ZipEntry.class), written.capture());
assertThat(Iterables.size(written.getValue().classDefs())).isEqualTo(2);
}
示例15: testMultidex_overLimitWritesSecondShard
import com.android.dex.Dex; //导入依赖的package包/类
@Test
public void testMultidex_overLimitWritesSecondShard() throws Exception {
DexFileAggregator dexer =
new DexFileAggregator(
new DxContext(),
dest,
newDirectExecutorService(),
MultidexStrategy.BEST_EFFORT,
/*forceJumbo=*/ false,
2 /* dex has more than 2 methods and fields */,
WASTE,
DexFileMergerTest.DEX_PREFIX);
Dex dex2 = DexFiles.toDex(convertClass(ByteStreams.class));
dexer.add(dex); // classFile is already over limit but we take anything in empty shard
dexer.add(dex2); // this should start a new shard
// Make sure there was one file written and that file is dex
verify(dest).addFile(any(ZipEntry.class), written.capture());
assertThat(written.getValue()).isSameAs(dex);
dexer.close();
verify(dest).addFile(any(ZipEntry.class), eq(dex2));
}