Android - 将 jar 文件动态添加到 ClassLoader,如:
String strJarfile = "/data/data/com.example.classloader/files/apps/Calc/SumAndSub.jar";
URLClassLoader sysloader = (URLClassLoader) ClassLoader
.getSystemClassLoader();
try
{
File f = new File(strJarfile);
URL u = f.toURI().toURL();
Class[] parameters = new Class[] { URL.class };
Method method = URLClassLoader.class.getDeclaredMethod("addURL",
parameters);
method.setAccessible(true);
method.invoke(sysloader, new Object[] { u });
}
catch (Exception e)
{
e.printStackTrace();
}
而且,错误:
Caused by: java.lang.ClassCastException: dalvik.system.PathClassLoader cannot be cast to java.net.URLClassLoader
此代码在 java-core 中运行良好,但不适用于 android。什么是解决方案?谢谢,