读取sd卡里的jar包并使用里面的类
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String path = Environment.getExternalStorageDirectory()+File.separator+"aa.jar";
dexClassLoader(path);
}
});
}
private void dexClassLoader(String path) {
File dex = getDir("dex", Context.MODE_PRIVATE);
DexClassLoader loader = new DexClassLoader(path, dex.getAbsolutePath(), null, this.getClass().getClassLoader());
try {
Class<?> loadClass = loader.loadClass("com.example.mylib.MyClass");
Constructor<?> constructor = loadClass.getDeclaredConstructor();
constructor.setAccessible(true);
Object o = constructor.newInstance();
Field grade = loadClass.getDeclaredField("grade");
grade.setAccessible(true);
double g = (double) grade.get(o);
//Toast.makeText(this, "" + g, Toast.LENGTH_SHORT).show();
Method sumGrade = loadClass.getMethod("sumGrade", double.class, double.class, double.class);
double sum= (double) sumGrade.invoke(o,100,100,100);
Toast.makeText(this, ""+sum, Toast.LENGTH_SHORT).show();
Method runRunnable = loadClass.getDeclaredMethod("runRunnable", Runnable.class);
runRunnable.setAccessible(true);
Runnable runnable = new Runnable() {
@Override
public void run() {
Log.i("abc", "run: "+1314);
}
};
runRunnable.invoke(o,runnable);
} catch (Exception e) {
e.printStackTrace();
}
}