开发SDK时,若项目中需要引用到res中的资源,那提供对接文件给其他项目引用时,Jar代码获取资源不能直接引用 R.xx.xx,这时候不能将资源一起打包,只能通过反射机制动态获取资源。
- 1.通过 Resources.getIdentifierint 方法来进行资源ID的获取:
android.content.res.Resources.getIdentifier(String name, String defType, String defPackage)
封装成一个工具类
package com.cp.utils; import android.content.Context; public class CPResourceUtil { public static int getLayoutId(Context paramContext, String paramString) { return paramContext.getResources().getIdentifier(paramString, "layout", paramContext.getPackageName()); } public static int getStringId(Context paramContext, String paramString) { return paramContext.getResources().getIdentifier(paramString, "string", paramContext.getPackageName()); } public static int getDrawableId(Context paramContext, String paramString) { return paramContext.getResources().getIdentifier(paramString, "drawable", paramContext.getPackageName()); } public static int getStyleId(Context paramContext, String paramString) { return paramContext.getResources().getIdentifier(paramString, "style", paramContext.getPackageName()); } public static int getId(Context paramContext, String paramString) { return paramContext.getResources().getIdentifier(paramString,"id", paramContext.getPackageName()); } public static int getColorId(Context paramContext, String paramString) { return paramContext.getResources().getIdentifier(paramString, "color", paramContext.getPackageName()); } public static int getArrayId(Context paramContext, String paramString) { return paramContext.getResources().getIdentifier(paramString, "array", paramContext.getPackageName()); } }
资源的获取
<string name="version_update_schedule">已下载%1$s请稍等…</string>
String text=mContext.getResources().getString(CPResourceUtil.getStringId(mContext, "version_update_schedule"));
TextView.setText(String.format(text, p+"%"));
version_update_schedule 是string文件中name值
- 2.通过反射来读取包名路径下的R$系列文件进行资源ID的获取:
- 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;
- }
- }