Android 引用完整工程包括Activity和资源文件

来源:互联网 发布:手机扫描条码软件 编辑:程序博客网 时间:2024/04/30 09:24
最近需要把以前做的一个完整工程打包成SDK.供别的工程调用.开发过程中,参考过一些其他的相关文章, 一点点心得,自己在此记录一下.1. 首先把需要打包成SDK的工程种, Activity的 layout, string, color, drawable等文件, 重新命名, 做到命名唯一.2. 由于主工程引用SDK工程,  自动生成SDK工程的R文件, 则在主工程Gen文件下面.3. 修改查找资源文件方法,见类Resource.

Resource

    public class Resource {    private static final String TAG = "Resource";    public static final String ID = "id";    public static final String LAYOUT = "layout";    public static final String DRAWABLE = "drawable";    public static final String STRING = "string";    public static final String COLOR = "color";    public static final String ANIM = "anim";    public static final String STYLE = "style";    public static final String DIMEN = "dimen";    @SuppressWarnings("rawtypes")    public static int getResId(Context context, String className, String name) {        String packageName = context.getPackageName();        Class r = null;        int id = 0;        try {            r = Class.forName(packageName + ".R");            Class[] classes = r.getClasses();            Class desireClass = null;            for (int i = 0; i < classes.length; ++i) {                if (classes[i].getName().split("\\$")[1].equals(className)) {                    desireClass = classes[i];                    break;                }            }            if (desireClass != null){                id = desireClass.getField(name).getInt(desireClass);                Log(TAG, "class name : " + desireClass.getName()+"    id : " + id);            }        } catch (ClassNotFoundException e) {            e.printStackTrace();        } catch (IllegalArgumentException e) {            e.printStackTrace();        } catch (SecurityException e) {            e.printStackTrace();        } catch (IllegalAccessException e) {            e.printStackTrace();        } catch (NoSuchFieldException e) {            e.printStackTrace();        }        return id;    }    /**     * id     * @param context     * @param name     * @return     */    public static int getId(Context context, String name){        return getResId(context, ID, name);    }    /**     * stringId     * @param context     * @param name     * @return     */    public static int getStringId(Context context, String name){        return getResId(context, STRING, name);    }    public static int getLayoutId(Context context, String name){        return getResId(context, LAYOUT, name);    }    public static int getColorId(Context context, String name){        return getResId(context, COLOR, name);    }    public static int getAnimId(Context context, String name){        return getResId(context, ANIM, name);    }    public static int getStyleId(Context context, String name){        Log(TAG, "getStyleId : " + getResId(context, STYLE, name)+" name : " + name);        return getResId(context, STYLE, name);    }    public static int getDimen(Context context, String name){        return getResId(context, DIMEN, name);    }    /**     * drawableId     * @param context     * @param name     * @return     */    public static int getDrawableId(Context context, String name){        return getResId(context, DRAWABLE, name);    }    private static void Log(String tag, String log) {        LogUtils.i(tag, log);    }}

找到相应的资源文件.

4.关于AndroidManifest.xml清单文件配置:
–>4.1. 把SDK中的权限,以及activity,receiver,service,拷贝到当前主工程下面.
–>4.2. project.properties文件中 manifestmerger.enabled=true 不过有个要求
(Commented content should be opened if your ADT version is lower than v20, or you remove the “manifestmerger.enabled=true” from project.properties)

0 0