android jar 反射,[置顶] android 第三方jar库 反射得到自己的资源ID

本文介绍了在无法直接引用R.xx.name的情况下,如何通过反射和Resources.getIdentifier()方法获取Android资源ID。展示了一个名为ResoureExchange的类,该类通过静态初始化各个资源类型的Class对象,并提供了获取不同资源类型ID的方法。这种方法适用于处理反编译后的jar文件中资源引用的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

做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]

原文作者:H-G-Y

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值