android插件化入门 优化

如我的前一篇博客一样http://blog.csdn.net/shushangxiaocao/article/details/17240889,现在来做点小优化:

原来的代码是:

比较繁琐,不熟悉反射的同学可能有点看不明白。现在,针对这部分代码,来做点有意思的优化:

首先,在Home项目中添加一个包:

里面有一个ICommon接口类,类代码如下:

public interface ICommon {
	public int addOperation(int arg1,int arg2);
}
然后,将该类编译打包,编译打包命令:

执行此编译命令后,会有一个ICommon.class文件

执行此打包命令,将得到一个plugin.jar包

在Plugin中,将该jar包加上:

添加完毕后,然后AddOperation类实现ICommon:

import com.example.host.common.ICommon;


public class AddOperation implements ICommon{

	@Override
	public int addOperation(int arg1, int arg2) {
		// TODO Auto-generated method stub
		Log.i("AddOperation", "addOperation is called!");
		return arg1 + arg2;
	}
}
	
Plugin修改完毕。

Host修改,只需将文章开头的那部分代码改成下面的代码即可:

try {
			Class<?> cla = dexClassLoader.loadClass(packageName + ".AddOperation");
			ICommon common = (ICommon)cla.newInstance();
			int result = common.addOperation(10, 30);
			Log.i("Home", "result is: " + result);
		} catch (Exception e){
			e.printStackTrace();
		}

修改完毕,运行两个app,结果如下:


学习至android的内核剖析,作者:柯元旦

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值