| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
import dalvik.system.InMemoryDexClassLoader; |
| 18 |
|
| 19 |
import java.io.InputStream; |
| 20 |
import java.lang.reflect.Method; |
| 21 |
import java.nio.ByteBuffer; |
| 22 |
import java.util.zip.ZipEntry; |
| 23 |
import java.util.zip.ZipFile; |
| 24 |
|
| 25 |
public class Main { |
| 26 |
|
| 27 |
public static void main(String[] args) throws Exception { |
| 28 |
|
| 29 |
String jarFilename = |
| 30 |
System.getenv("DEX_LOCATION") + "/656-annotation-lookup-generic-jni-ex.jar"; |
| 31 |
ZipFile zipFile = new ZipFile(jarFilename); |
| 32 |
ZipEntry zipEntry = zipFile.getEntry("classes.dex"); |
| 33 |
InputStream inputStream = zipFile.getInputStream(zipEntry); |
| 34 |
int dexFileSize = (int) zipEntry.getSize(); |
| 35 |
byte[] dexFileContents = new byte[dexFileSize]; |
| 36 |
inputStream.read(dexFileContents, 0, dexFileSize); |
| 37 |
|
| 38 |
|
| 39 |
ByteBuffer dexBuffer = ByteBuffer.wrap(dexFileContents); |
| 40 |
ClassLoader classLoader = createUnquickenedDexClassLoader(dexBuffer); |
| 41 |
|
| 42 |
|
| 43 |
Class<?> testClass = classLoader.loadClass("Test"); |
| 44 |
Method initialize = testClass.getMethod("initialize", String.class); |
| 45 |
initialize.invoke(null, args[0]); |
| 46 |
|
| 47 |
|
| 48 |
Method nativeMethodWithAnnotation = testClass.getMethod("nativeMethodWithAnnotation"); |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
nativeMethodWithAnnotation.invoke(null); |
| 53 |
|
| 54 |
zipFile.close(); |
| 55 |
System.out.println("passed"); |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
static ClassLoader createUnquickenedDexClassLoader(ByteBuffer dexBuffer) { |
| 64 |
InMemoryDexClassLoader cl = new InMemoryDexClassLoader(dexBuffer, getBootClassLoader()); |
| 65 |
return cl; |
| 66 |
} |
| 67 |
|
| 68 |
static ClassLoader getBootClassLoader() { |
| 69 |
ClassLoader cl = Main.class.getClassLoader(); |
| 70 |
while (cl.getParent() != null) { |
| 71 |
cl = cl.getParent(); |
| 72 |
} |
| 73 |
return cl; |
| 74 |
} |
| 75 |
|
| 76 |
} |