要将一些class存放在某固定的包名下,通过反射实例化出来,
private List getClasses(Context mContext, String packageName) {
ArrayList classes = new ArrayList<>();
try {
String packageCodePath = mContext.getPackageCodePath();
DexFile df = new DexFile(packageCodePath);
String regExp = "^" + packageName + ".\\w+$";
for (Enumeration iter = df.entries(); iter.hasMoreElements(); ) {
String className = iter.nextElement();
if (className.matches(regExp)) {
classes.add(className);
}
}
} catch (IOException e) {
e.printStackTrace();
}
return classes;
}
网友评论