当前位置: 移动技术网 > IT编程>移动开发>Android > Android 插件化,qihoo360插件方案配置教程

Android 插件化,qihoo360插件方案配置教程

2018年09月14日  | 移动技术网IT编程  | 我要评论

陕视网,相声胡同,圆白菜炒饼

android 插件化,qihoo360插件方案

目录

android 插件化,qihoo360插件方案


宿主app插件化: 插件app插件配置: 宿主调用插件app:

新建一个项目,开始配置

宿主插件化配置:

在项目的根目录build.gradle中配置

classpath 'com.qihoo360.replugin:replugin-host-gradle:2.2.4'

在app module的build.gradle中引入


apply plugin: 'replugin-host-gradle'

其中repluginhostconfig 要看你是否使用appcompat 来进行个性化配置


repluginhostconfig {
 /**
  * 是否使用 appcompat 库
  * 不需要个性化配置时,无需添加
  */
 useappcompat = true
}

3.添加依赖:


compile 'com.qihoo360.replugin:replugin-host-lib:2.2.4'

4.配置 application 类

让工程的 application 直接继承自 repluginapplication。

如果您的工程已有application类,则可以将基类切换到repluginapplication即可。或者您也可以用“非继承式”接入。


public class mainapplication extends repluginapplication {
}

既然声明了application,自然还需要在androidmanifest中配置这个application


 

备选:“非继承式”配置application

若您的应用对application类继承关系的修改有限制,或想自定义replugin加载过程(慎用!),则可以直接调用相关方法来使用replugin。


public class mainapplication extends application {

 @override
 protected void attachbasecontext(context base) {
  super.attachbasecontext(base);

  replugin.app.attachbasecontext(this);
  ....
 }

 @override
 public void oncreate() {
  super.oncreate();
  
  replugin.app.oncreate();
  ....
 }

 @override
 public void onlowmemory() {
  super.onlowmemory();

  /* not need to be called if your application's minsdkversion > = 14 */
  replugin.app.onlowmemory();
  ....
 }

 @override
 public void ontrimmemory(int level) {
  super.ontrimmemory(level);

  /* not need to be called if your application's minsdkversion > = 14 */
  replugin.app.ontrimmemory(level);
  ....
 }

 @override
 public void onconfigurationchanged(configuration config) {
  super.onconfigurationchanged(config);

  /* not need to be called if your application's minsdkversion > = 14 */
  replugin.app.onconfigurationchanged(config);
  ....
 }
}

针对“非继承式”的注意点

所有方法必须在ui线程来“同步”调用。切勿放到工作线程,或者通过post方法来执行 所有方法必须一一对应,例如 replugin.app.attachbasecontext 方法只在application.attachbasecontext中调用 请将replugin.app的调用方法,放在“仅次于super.xxx()”方法的后面

插件app的配置:

在根目录下的build.gradle中添加依赖:


 classpath 'com.qihoo360.replugin:replugin-plugin-gradle:2.2.4'

在app module中的builder.gradle中引入:


apply plugin: 'replugin-plugin-gradle'

添加dependencies依赖:


 compile 'com.qihoo360.replugin:replugin-plugin-lib:2.2.4'

4.配置别名信息:


/*插件名配置*/
repluginpluginconfig {
 //插件名
 pluginname = "androidgo"
 //宿主app的包名
 hostapplicationid = "com.newdicooker.tempetek.hostreplugin"
 //宿主app的启动activity
 hostapplauncheractivity = "com.newdicooker.tempetek.hostreplugin.mainactivity"
}

以上步骤完后才能就是见证奇迹的时刻了:

宿主调用插件app:

调用内置插件 调用外置插件

调用内置插件:

插件app生成apk,将apk改名为:[插件名].jar 将[插件名].jar放入主程序的assets/plugins目录下 可通过别名:”


 replugin.startactivity(mainactivity.this,
 replugin.createintent("androidgo", "com.newdicooker.tempetek.androidgo.mainactivity"));

androidgo:是插件app的别名
com.newdicooker.tempetek.androidgo.mainactivity:要启动app的启动页

可通过包名来进行访问:


replugin.startactivity(mainactivity.this,
 replugin.createintent("com.newdicooker.tempetek.androidgo", "com.newdicooker.tempetek.androidgo.mainactivity"));

调用外置插件:通过网络下载插件apk调用replugin.install(path)安装;

通过开启服务来进行异步下载任务的:


replugin.isplugininstalled("image")是通过别名来判断插件是否安装,如果未安装则下载安装,如果已经安装则直接进入。

 public void jumptoout(view view) {
  if (replugin.isplugininstalled("image")) {
replugin.startactivity(mainactivity.this,
  replugin.createintent("image", "com.xq.imageplugindemo.mainactivity"));
return;
  }

  // 插件下载地址
  string urlpath = "https://raw.githubusercontent.com/zhangzeqiao/imageplugindemo/7c5866db83b57c455302fac12ea72af30d9a5364/app/src/main/assets/image.apk";
  // 插件下载后的存放路径
  string downloaddir = environment.getexternalstoragedirectory().getabsolutepath();
  intent intent = new intent(this, downloadandupdateservice.class);
  intent.putextra("urlpath", urlpath);
  intent.putextra("downloaddir", downloaddir);
  startservice(intent);
 }

下载安装:


public class downloadandupdateservice extends intentservice {

 public downloadandupdateservice() {
  // 实现父类的构造方法,用于命名工作线程,只用于调试。
  super("downloadandupdateservice");
 }

 @override
 protected void onhandleintent(@nullable intent intent) {
  // intent是从activity发过来的,携带识别参数,根据参数不同执行不同的任务
  // 插件下载地址
  string urlpath = intent.getstringextra("urlpath");
  // 插件下载后的存放路径
  string downloaddir = intent.getstringextra("downloaddir");

  file file = null;
  try {
// 统一资源
url url = new url(urlpath);
// 连接类的父类,抽象类
urlconnection urlconnection = url.openconnection();
// http的连接类
httpurlconnection httpurlconnection = (httpurlconnection) urlconnection;
// 设定请求的方法,默认是get
httpurlconnection.setrequestmethod("get");
// 设置字符编码
httpurlconnection.setrequestproperty("charset", "utf-8");
// 打开到此 url 引用的资源的通信链接(如果尚未建立这样的连接)。
httpurlconnection.connect();

// 文件大小
int filelength = httpurlconnection.getcontentlength();
// 文件名
string filepathurl = httpurlconnection.geturl().getfile();
string filefullname = filepathurl.substring(filepathurl.lastindexof(file.separatorchar) + 1);

urlconnection con = url.openconnection();
bufferedinputstream bin = new bufferedinputstream(httpurlconnection.getinputstream());

string path = downloaddir + file.separatorchar + filefullname;
file = new file(path);
if (!file.getparentfile().exists()) {
 file.getparentfile().mkdirs();
}
outputstream out = new fileoutputstream(file);
int size = 0;
int len = 0;
byte[] buf = new byte[1024];
while ((size = bin.read(buf)) != -1) {
 len += size;
 out.write(buf, 0, size);
 // 下载百分比
 log.v("xq", "下载了-------> " + len * 100 / filelength);
}
bin.close();
out.close();
// 升级安装插件新版本
replugin.install(path);
log.v("xq", "下载完成 : " + path);
  } catch (malformedurlexception e) {
e.printstacktrace();
  } catch (ioexception e) {
e.printstacktrace();
  } finally {

  }
 }
}

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网