来源:http://www.cnblogs.com/rollenholt/archive/2011/09/02/2163758.html
练习:
Class<?> cls = Class.forName("java.lang.String"); Constructor<?> constructor = cls.getConstructor(String.class); Object obj = constructor.newInstance("adb"); Method method = cls.getMethod("toString"); Object sss = method.invoke(obj); System.out.println(sss);
android上的动态加载jar
public static int getLibVersion(Context context) { int version = 1; final File optimizedDexOutputPath = new File(Constant.VIDEO_LIB_PATH + File.separator + LIB_FILE_NAME); if (optimizedDexOutputPath.exists()) { try { DexClassLoader cl = new DexClassLoader(optimizedDexOutputPath.getAbsolutePath(), context. getCacheDir().getAbsolutePath(),null, context.getClassLoader()); Class<?> libProviderClazz = null; libProviderClazz = cl.loadClass(YOUTUBE_VERSION_CLASSNAME); Field field = libProviderClazz.getField("VERSION"); version = field.getInt(null); } catch (Exception ex) { LogUtil.e(ex); } } return version; }