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

最近需要把以前做的一个完整工程打包成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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值