Android框架RePlugin使用详解

0.先附上一篇原理讲解

https://www.nhooo.com/article/127353.htm

这应该算是给作者打的广告吧。如有涉及侵权,请通知我立刻删除。

1.应用场景

Android开发人员不多,又要求使用插件化开发的模式。(简单点讲就是自己需要写宿主APP,还有N多个插件需要开发)

如下图:

360官方给的Demo是宿主一个工程,插件一个工程,并且工程的build.gradle文件中添加宿主和插件对应所需要的依赖,多人开发时,每个人或多个人维护一个宿主工程或者插件工程,都没有什么问题,但是一个人需要同时维护宿主和多个插件时,来回切换工程调试以及打包就异常头疼了。。。

2.一个工程配置宿主和插件的所有依赖

2.1工程的build.gradle文件中添加

buildscript {
 ...
 dependencies {
  classpath 'com.android.tools.build:gradle:2.3.3'  
  //插件化框架 宿主工程所需依赖
  classpath 'com.qihoo360.replugin:replugin-host-gradle:2.2.0'
   //插件化框架 插件工程所需依赖
  classpath 'com.qihoo360.replugin:replugin-plugin-gradle:2.2.0'
  ...
 }
 ...
}

2.2宿主app的build.gradle文件中添加

//replugin插件化框架,这一行建议就放在dependencies 的上面,千万不要放在android{...}的上面
apply plugin: 'replugin-host-gradle'

dependencies {
 compile fileTree(include: ['*.jar'], dir: 'libs')
 ...
 releaseCompile project(path: ':library的moudle名字', configuration: 'release')
   debugCompile project(path: ':library的moudle名字', configuration: 'debug')
 compile 'com.qihoo360.replugin:replugin-host-lib:2.2.0'
 ...
}

2.3插件moudle对应的build.gradle文件中添加

//replugin插件化框架,这一行建议就放在dependencies 的上面,千万不要放在android{...}的上面

apply plugin: 'replugin-plugin-gradle'
repluginPluginConfig {
 pluginName = "你自己的插件名字"
 hostApplicationId = "宿主包名"
 hostAppLauncherActivity = "宿主包名.MainActivity"
}

dependencies {
 ...
 releaseCompile project(path: ':library的moudle名字', configuration: 'release')
 debugCompile project(path: ':library的moudle名字', configuration: 'debug')
 compile 'com.qihoo360.replugin:replugin-plugin-lib:2.2.0'
 ...
}

3.宿主及所有插件使用到的library,这里建议自己做成一个moudle,配置方法上面的代码理由了 (library的moudle名字)
如有依赖的冲突,在对应moudle中添加

android{
 ...
 configurations.all {
 resolutionStrategy.eachDependency { DependencyResolveDetails details ->
  def requested = details.requested
  if (requested.group == 'com.android.support') {
   if (!requested.name.startsWith("multidex")) {
    details.useVersion '25.3.1'//这里可根据个人需要改成自己想要的版本
   }
  }
 }
}
...
}

4.关于调试以及成品包。

调试时有几个moudle,就安装几个应用,调用的地方

if (!BuildConfig.DEBUG) {//非Debug,成品包
 if (RePlugin.isPluginInstalled("插件包名")) {
  //已安装
  Intent intentHasInstall = RePlugin.createIntent("插件包名" ,
    "插件包名.MainActivity");
  intentHasInstall.putExtras(bundle);
  RePlugin.startActivity(mContext, intentHasInstall);
  }else{//没有安装,自己写下载的代码}
}else {//debug模式
Intent intent1 = new Intent();
intent1.setComponent(new ComponentName("插件包名", "插件包名.MainActivity"));
intent1.putExtras(bundle);
startActivity(intent1);
}

5.关于FileProvider

5.1这个首先公用library的那个moudle要有一个公用的Utils,并且每个moudle(插件或宿主)都初始化此工具类

public final class Utils { 
 private static Context context;
 /**
  * 初始化工具类
  *
  * @param context 上下文
  */
 public static void init(@NonNull final Context context) {
  Utils.context = context.getApplicationContext();
 }

 /**
  * 获取ApplicationContext
  *
  * @return ApplicationContext
  */
 public static Context getContext() {
  if (context != null) return context;
  throw new NullPointerException("请保证你已经初始化过!");
 }
}

5.2每个moudle(插件或宿主)的manifest文件中都添加

<provider
 android:name="android.support.v4.content.FileProvider"
 android:authorities="对应moudle的包名.fileProvider"
 android:grantUriPermissions="true"
 android:exported="false">
 <meta-data
  android:name="android.support.FILE_PROVIDER_PATHS"
  android:resource="@xml/file_paths" />
</provider>

需使用FileProvider时:以获取文件URI为例

File mTmpFile = new File("你自己的文件路径");
Uri uri= FileProvider.getUriForFile(Utils.getContext(), Utils.getContext().getPackageName() + ".fileProvider", mTmpFile);

Utils.getContext().getPackageName(),重点是这个方法;

a.debug时,获取的对应插件的包名,通过每个moudle的manifest配置中对应的fileProvider找到文件;

b.release时,Utils.getContext().getPackageName()获取的只会是宿主的fileProvider,这时自然可以通过宿主manifest配置对应的fileProvider找到文件;

声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:notice#nhooo.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。