Android动态加载代码技术

在开发Android App的过程当中,可能希望实现插件式软件架构,将一部分代码以另外一个APK的形式单独发布,而在主程序中加载并执行这个APK中的代码。

实现这个任务的一般方法是:

// 加载类cls
Context pluginContext = mainContext.createPackageContext(PLUGIN_PKG, Context.CONTEXT_IGNORE_SECURITY | Context.CONTEXT_INCLUDE_CODE);
ClassLoader loader = pluginContext.getClassLoader();
Class<?> cls = loader.loadClass(CLASS_NAME);
// 通过反射技术,调用cls中的方法,下面是一个示例,实际代码因情况而定
Object obj = cls.newInstance();
Method method = cls.getDeclaredMethod("someMethod");
method.invoke(obj);

但是,这个方法在Android 4.1及之后的系统中存在一些问题:对于收费应用,Google Play会将其安装在一个加密目录之下(具体就是/data/app-asec),而不是一个普通目录之下(具体就是/data/app);安装在加密目 录中的应用,我们是无法使用上述方法来加载并执行代码的;而实际情况是,我们经常就是依靠插件应用来收费的。

解决上述问题的一个方案是:将插件的二进制代码拷贝到SD卡中,主程序从SD卡中加载并执行其代码。

实现这个任务的具体方法是:

Class<?> cls = null;
try {     // 尝试第一种方法
     cls = loadClass1(mainContext, pkg, entryCls);
} catch (Exception e) {     // 尝试第二种方法
     cls = loadClass2(mainContext, pkg, entryCls);
}// 示例代码Object obj = cls.newInstance();Method method = cls.getDeclaredMethod("someMethod");method.invoke(obj);
// 第一种加载方法private Class<?> loadClass1(Context mainContext, String pkg, String entryCls) throws Exception {    Context pluginContext = mainContext.createPackageContext(pkg, Context.CONTEXT_INCLUDE_CODE | Context.CONTEXT_IGNORE_SECURITY);    ClassLoader loader = pluginContext.getClassLoader();    return loader.loadClass(entryCls); }

// 第二种加载方法private Class<?> loadClass2(Context mainContext, String pkg, String entryCls) throws Exception {    Context pluginContext = mainContext.createPackageContext(pkg, Context.CONTEXT_INCLUDE_CODE | Context.CONTEXT_IGNORE_SECURITY);    String path = generatePluginDexPath(mainContext, pkg);    ensureFileExist(pluginContext, pkg, path);    // cacheDir必须是主程序的私有目录,否则DexClassLoader可能会拒绝加载    String cacheDir = mainContext.getApplicationInfo().dataDir;    ClassLoader parentLoader = pluginContext.getClassLoader();    DexClassLoader loader = new DexClassLoader(path, cacheDir, null, parentLoader);    return loader.loadClass(entryCls);}

// 获取程序版本号private int getVersionCode(Context context, String pkg) {    PackageInfo info = null;    int versionCode = 0;    try {         info = context.getPackageManager().getPackageInfo(pkg, PackageManager.GET_ACTIVITIES);         versionCode = info.versionCode;    } catch (Exception e) {}     return versionCode;}

// 获取插件二进制代码的存储位置,注意做好版本控制;路径必须是以.dex结束,否则加载会出问题private String generatePluginDexPath(Context context, String pkg) {    int version = getVersionCode(context, pkg);    String path = getMyAppPath() + ".classes/" + pkg + version + ".dex";    return path;}

// 主程序在SD卡上的数据目录private String getMyAppPath() {    return Environment.getExternalStorageDirectory().getAbsolutePath() + "/MyApp/";}
// 拷贝插件的二进制代码到SD卡private void ensureFileExist(Context pluginContext, String pkg, String path) throws Exception {    File file = new File(path);    if(file.exists()) return;    file.getParentFile().mkdirs();    Resources res = pluginContext.getResources();    int id = res.getIdentifier("classes", "raw", pkg);    InputStream in = res.openRawResource(id);    FileOutputStream out = new FileOutputStream(file);    try {         byte[] buffer = new byte[1024 * 1024];         int n = 0;         while((n = in.read(buffer)) > 0) {            out.write(buffer, 0, n);         } out.flush();    } catch (IOException e) {       in.close();       out.close();    }}

插件工程这边也需要做相应的修改:

1.编译插件工程;

2.将bin目录之下的classes.dex拷贝到/res/raw目录之下;

3.重新编译插件工程;

4.发布插件APK。

来自 :frydsh

最新文章

  1. 走进AngularJs(六) 服务
  2. Git_1基础操作,从安装到提交完成(windows)
  3. 黑马程序员——【Java基础】——网络编程
  4. Atitit .html5刮刮卡的gui实现总结
  5. IIS 7.5 配置10W高并发
  6. apk反编译(3)smali语法
  7. Debug 之 VS2010网站生成成功,但是发布失败
  8. ANR的一个实例分析
  9. 基于数据形式说明杜兰特的技术特点的分析(含Python实现讲解部分)
  10. 云如何让App开发更简单?
  11. Docker Hub
  12. Java集合--TreeSet详细解析
  13. 纯css3实现只适应的正方形
  14. pymysql操作
  15. 词向量编码 word2vec
  16. 装饰模式和它在JDK中的实现
  17. ubuntu16系统磁盘空间/dev/vda1占用满的问题
  18. 3.8 Templates -- Actions
  19. 一篇关于Redis的很不错的文章,转载保存下
  20. ubuntu 安装nodejs/npm

热门文章

  1. DOM 节点属性
  2. silverlight中使用Json读取数据
  3. List myList=new ArrayList()的理解
  4. 转载:常见EXE文件反编译工具
  5. HTML5实战之桌面通知
  6. 解决Sqlite UTF-8中文数据格式在DOS窗口下中文乱码
  7. Qt实现基于G.729A(G729A)的语音聊天
  8. Python Cookie HTTP获取cookie并处理
  9. VS快捷编码方式
  10. python - 文件