Søren Gjesse | 5b4ee0a | 2018-01-30 13:46:39 +0100 | [diff] [blame] | 1 | // Copyright (c) 2018, the R8 project authors. Please see the AUTHORS file |
| 2 | // for details. All rights reserved. Use of this source code is governed by a |
| 3 | // BSD-style license that can be found in the LICENSE file. |
| 4 | package classloader; |
| 5 | |
| 6 | import java.lang.reflect.Method; |
| 7 | |
| 8 | import dalvik.system.PathClassLoader; |
| 9 | |
| 10 | // Command line application which take three arguments: |
| 11 | // |
| 12 | // Parent dex file |
| 13 | // Child dex file |
| 14 | // Main class name |
| 15 | // |
| 16 | // The application will create a classloader hierachy with the parent dex file above the |
| 17 | // system class loader, and the child dex file above the parent dex file. The it will load the |
| 18 | // Main class from the child dex file class loader and run its main method. |
| 19 | public class Runner { |
| 20 | public static void main(String[] args) throws Exception { |
| 21 | String parentFile = args[0]; |
| 22 | String childFile = args[1]; |
| 23 | String childClassName = args[2]; |
| 24 | ClassLoader parentClassLoader = |
| 25 | new PathClassLoader(parentFile, ClassLoader.getSystemClassLoader()); |
| 26 | ClassLoader childClassLoader = new PathClassLoader(childFile, parentClassLoader); |
| 27 | |
| 28 | Class<?> childClass = childClassLoader.loadClass(childClassName); |
| 29 | runMain(childClass, new String[0]); |
| 30 | } |
| 31 | |
| 32 | private static void runMain(Class<?> clazz, String[] args) throws Exception { |
| 33 | Method m = clazz.getMethod("main", String[].class); |
| 34 | m.invoke(null, new Object[] { args }); |
| 35 | } |
| 36 | } |