您的位置:首页 > 移动开发 > Android开发

Android 插件化框架 RePlugin 使用心得

2017-12-21 10:42 232 查看
0.先附上一篇原理讲解 http://www.jianshu.com/p/18530be5dcdd这应该算是给作者打的广告吧。如有涉及侵权,请通知我立刻删除。
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找到文件;

如有觉得讲解的不明白的刚好用到插件化框架的可以联系我
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  框架 插件 RePlugin