
某些时候,自己把代码打包成工具jar包,但jar包中可能引用到res中的资源,这时候不能将资源一起打包,只能通过反射机制动态获取
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
public static int getCompentID(String packageName, String className,String idName) { int id = 0; try { Class<?> cls = Class.forName(packageName + ".R$" + className); id = cls.getField(idName).getInt(cls); } catch (Exception e) { LogUtil.LogPrint(LogUtil.LOG_ERROR, "缺少" + idName + "文件!"); e.printStackTrace(); } return id; }
|
近期评论