publicclass Main { publicstaticvoid main(String[] args) throws Exception {
System.loadLibrary(args[0]);
// Feature only enabled for target SDK version Q and later.
setTargetSdkVersion(/* Q */ 29);
if (isDebuggable()) { // Background verification is disabled in debuggable mode. This test makes // no sense then. return;
}
if (!hasOatFile()) { // We only generate vdex files if the oat directories are created. return;
}
setProcessDataDir(DEX_LOCATION);
finalint maxCacheSize = getVdexCacheSize(); finalint numDexFiles = DEX_BYTES_CHECKSUMS.length; if (numDexFiles <= maxCacheSize) { thrownew IllegalStateException("Not enough dex files to test cache eviction");
}
// Simply load each dex file one by one.
check(0, getCurrentCacheSize(), "There should be no vdex files in the beginning"); for (int i = 0; i < numDexFiles; ++i) {
ClassLoader loader = loadDex(i);
waitForVerifier();
check(true, hasVdexFile(loader), "Loading dex file should have produced a vdex");
check(Math.min(i + 1, maxCacheSize), getCurrentCacheSize(), "Unexpected number of cache entries");
}
// More complicated pattern where some dex files get reused. for (int s = 1; s < numDexFiles; ++s) { for (int i = 0; i < maxCacheSize; ++i) {
ClassLoader loader = loadDex(i);
waitForVerifier();
check(true, hasVdexFile(loader), "Loading dex file should have produced a vdex");
check(maxCacheSize, getCurrentCacheSize(), "Unexpected number of cache entries");
}
}
}
privatestaticint getCurrentCacheSize() { int count = 0;
File folder = new File(DEX_LOCATION, "oat");
File[] subfolders = folder.listFiles(); if (subfolders.length != 1) { thrownew IllegalStateException("Expect only one subfolder - isa");
}
folder = subfolders[0]; for (File f : folder.listFiles()) { if (f.isFile() && isAnonymousVdexBasename(f.getName())) {
count++;
}
} return count;
}
privatestaticbyte[] createDex(int index) { if (index >= 100) { thrownew IllegalArgumentException("Not more than two decimals");
}
// Clone the base dex file. This is the dex file for index 0 (class ID "01"). byte[] dex = DEX_BYTES_BASE.clone();
// Overwrite the checksum and sha1 signature.
System.arraycopy(DEX_BYTES_CHECKSUMS[index], 0, dex, DEX_BYTES_CHECKSUM_OFFSET,
DEX_BYTES_CHECKSUM_SIZE);
// Check that the class ID offsets match expectations - they should contains "01". if (dex[DEX_BYTES_CLASS_ID_OFFSET1 + 0] != 0x30 ||
dex[DEX_BYTES_CLASS_ID_OFFSET1 + 1] != 0x31 ||
dex[DEX_BYTES_CLASS_ID_OFFSET2 + 0] != 0x30 ||
dex[DEX_BYTES_CLASS_ID_OFFSET2 + 1] != 0x31) { thrownew IllegalStateException("Wrong class name values");
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.