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博客