android 第三方jar库 反射得到自己的资源ID

做jar,jar代码获取资源不能直接引用 R.xx.name,

有两种方法:

1.通过方法: int android.content.res.Resources.getIdentifier(String name, String defType, String defPackage)

 2.通过反射

第一种方法比较常见,主要看第二种方法:


public class ResoureExchange {
	private static final String TAG = ResoureExchange.class.getName();
	private static ResoureExchange self;

	private Context mContext;

	private static Class<?> CDrawable = null;

	private static Class<?> CLayout = null;

	private static Class<?> CId = null;

	private static Class<?> CAnim = null;

	private static Class<?> CStyle = null;

	private static Class<?> CString = null;

	private static Class<?> CArray = null;

	public static ResoureExchange getInstance(Context context){
		if(self == null){
			self = new ResoureExchange(context);
		}
		return self;
	}

	private ResoureExchange(Context context){
		this.mContext = context.getApplicationContext();
		try{
			CDrawable = Class.forName(this.mContext.getPackageName() + ".R$drawable");
			CLayout = Class.forName(this.mContext.getPackageName() + ".R$layout");
			CId = Class.forName(this.mContext.getPackageName() + ".R$id");
			CAnim = Class.forName(this.mContext.getPackageName() + ".R$anim");
			CStyle = Class.forName(this.mContext.getPackageName() + ".R$style");
			CString = Class.forName(this.mContext.getPackageName() + ".R$string");
			CArray = Class.forName(this.mContext.getPackageName() + ".R$array");

		}catch(ClassNotFoundException e){
			Log.i(TAG,e.getMessage());
		}
	}

	public int getDrawableId(String resName){
		return getResId(CDrawable,resName);
	}
	
	public int getLayoutId(String resName){
		return getResId(CLayout,resName);
	}
	
	public int getIdId(String resName){
		return getResId(CId,resName);
	}
	
	public int getAnimId(String resName){
		return getResId(CAnim,resName);
	}
	
	public int getStyleId(String resName){
		return getResId(CStyle,resName);
	}
	
	public int getStringId(String resName){
		return getResId(CString,resName);
	}
	
	public int getArrayId(String resName){
		return getResId(CArray,resName);
	}
	
	private int getResId(Class<?> resClass,String resName){
		if(resClass == null){
			Log.i(TAG,"getRes(null," + resName + ")");
			throw new IllegalArgumentException("ResClass is not initialized. Please make sure you have added neccessary resources. Also make sure you have " + this.mContext.getPackageName() + ".R$* configured in obfuscation. field=" + resName);
		}

		try {
			Field field = resClass.getField(resName);
			return field.getInt(resName);
		} catch (Exception e) {
			Log.i(TAG, "getRes(" + resClass.getName() + ", " + resName + ")");
			Log.i(TAG, "Error getting resource. Make sure you have copied all resources (res/) from SDK to your project. ");

			Log.i(TAG, e.getMessage());
		} 

		return -1;
	}
}

直接看代码,一目了然。[来源于反编译友盟统计jar]

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值