开发笔记:代码和资源分离发布SDK

SDK开发笔记—— 代码和资源分离发布 sdk

工作性质导致SDk不能以.aar形式发布,只能发布.jar和库依赖的方式,
但是代码不能开源,资源文件又复杂,没法简单的放进asset中打包进jar,怎么办呢?

我借鉴了MOB ShareSDK的SDK发布方式,将代码和资源文件分离
资源文件作为单独的依赖库
代码打包到.jar

这是我的代码结构

因为我们的库中不含代码,严格上是没有包名的(事实上还是得写一个空的带包名的mainfest,不然在打包时会编译失败)

这里我使用了利用java反射getIdentifier简化R文件的分包的问题

View rootFloatView = inflater.inflate(ResourceUtils.getLayoutId(context, "qd_widget_float_view"), null);

(FrameLayout) rootFloatView.findViewById(ResourceUtils.getId(context, "qd_float_view"));


具类:

ResourceUtils.java
/**
 * Created by wengyiming on 2015/12/20.
 */


import android.content.Context;

public class ResourceUtils {
    /**
     * 获取 layout 布局文件
     * @param context Context
     * @param resName  layout xml 的文件名
     * @return layout
     */
    public static int getLayoutId(Context context, String resName) {
        return context.getResources().getIdentifier(resName, "layout",
                context.getPackageName());
    }

/**
 * 获取 string 值 
 * @param context  Context
 * @param resName   string name的名称
 * @return string
 */
public static int getStringId(Context context, String resName) {
    return context.getResources().getIdentifier(resName, "string",
            context.getPackageName());
}

/**
 * 获取 drawable 布局文件 或者 图片的 
 * @param context  Context
 * @param resName drawable 的名称
 * @return drawable
 */
public static int getDrawableId(Context context, String resName) {
    return context.getResources().getIdentifier(resName,
            "drawable", context.getPackageName());
}


/**
 * 获取 style 
 * @param context Context 
 * @param resName  style的名称
 * @return style
 */
public static int getStyleId(Context context, String resName) {
    return context.getResources().getIdentifier(resName, "style",
            context.getPackageName());
}

/**
 * 获取 styleable
 * @param context  Context 
 * @param resName  styleable 的名称
 * @return styleable
 */
public static Object getStyleableId(Context context, String resName){
    return context.getResources().getIdentifier(resName, "styleable",
            context.getPackageName());
}


/**
 * 获取 anim 
 * @param context  Context 
 * @param resName  anim xml 文件名称
 * @return anim
 */
public static int getAnimId(Context context, String resName) {
    return context.getResources().getIdentifier(resName, "anim",
            context.getPackageName());
}

/**
 * 获取 id 
 * @param context Context
 * @param resName id 的名称
 * @return
 */
public static int getId(Context context, String resName) {
    return context.getResources().getIdentifier(resName, "id",
            context.getPackageName());
}

/**
 * color
 * @param context  Context
 * @param resName  color 名称
 * @return
 */
public static int getColorId(Context context, String resName) {
    return context.getResources().getIdentifier(resName, "color",
            context.getPackageName());
}

}

More info: 我的github博客

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值