美文网首页
RePlugin插件化框架接入笔记

RePlugin插件化框架接入笔记

作者: CarlosLynn | 来源:发表于2020-11-13 18:03 被阅读0次

1,创建宿主工程按照官方文档对接

https://github.com/Qihoo360/RePlugin

2,各配置版本兼容:

配置1:无法兼容插件放置到最后的顺序问题

1,distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.4-all.zip
2,classpath 'com.android.tools.build:gradle:3.6.4'
3,ext.kotlin_version = '1.4.10'

配置2(推荐):

1,distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-all.zip
2,classpath 'com.android.tools.build:gradle:3.5.3'
3,ext.kotlin_version = '1.4.10'

3,按业务需求情况整理宿主工程

4,创建插件工程按照文档对接

https://github.com/Qihoo360/RePlugin/wiki/%E6%8F%92%E4%BB%B6%E6%8E%A5%E5%85%A5%E6%8C%87%E5%8D%97

5,业务需求情况开发插件工程

6,宿主加载外置插件

宿主工程加载一个外置插件apk,这里为了简化实验流程,我们暂时将插件apk放置到assets目录进行模拟

  • 6.1插件工程打包apk
    注意事项
    1,kotlin中的代码不支持导入布局id使用,需要findViewById
    2,Activiy中所使用的到布局要避免id重复,否则会引起报错
    3,清单文件中配置插件名称
    4,清单文件中配置插件版本
    5,插件中申请权限无效需要在宿主中申请权限
  • 6.2创建assets目录,并放置插件apk
  • 6.2将apk通过IO流复制到工程进行动态加载
    加载错误
ava.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.

解决1:我们在代码super.onCreate(savedInstanceState)之前添加主题.

    override fun onCreate(savedInstanceState: Bundle?) {
        setTheme(R.style.AppTheme)
        super.onCreate(savedInstanceState)
    }

仍然报错.
解决2:经过对官方文档的推敲,该库已经半年没有维护,不支持AndroidX,因此我们更换该库的其他仓库分支:
https://github.com/froyohuang/RePlugin-AndroidX
参考该库文档我们进行重新配置依赖,然后进行打包插件apk.
我们将重新打包的apk,进行重新加载后可以打开.
报错2:

java.lang.IllegalArgumentException: Unable to find GlideModule implementation

解决方案1(无效):

  implementation 'com.github.bumptech.glide:glide:4.8.0'
  kapt 'com.github.bumptech.glide:compiler:4.8.0'

解决方案2:
暂时不用glide.

7,将原项目公共部分抽取到一个模块中,本项目是mvvm架构模式.

  • 7.1将View层基类转移至公共模块
  • 7.2将model层基类转移至公共模块
  • 7.3将ViewModel层基类转移至公共模块
  • 7.4将网络层转移至公共模块
  • 7.5将通用工具类转移至公共模块
    异常1:
lateinit property mViewModel has not been initialized

解决办法:增加判断是否初始化

fun isPersonInitialized():Boolean = :: person.isInitialized 

8,宿主程序和插件程序之间的通信

  • 8.1宿主页面简单跳转至插件页面
    方式1:
if (RePlugin.isPluginInstalled("personal")) {
    RePlugin.startActivity(context, RePlugin.createIntent("personal", "com.live.personal.ComplainActivity"))
}

方式2:

val intent = Intent()
intent.component = ComponentName("personal", "com.live.personal.ComplainActivity")
RePlugin.startActivity(context, intent)

注意:无需再添加Activity的启动或者关闭动画.

  • 8.2宿主页面跳转至插件页面,并传递参数
val intent = Intent()
intent.component = ComponentName("personal","com.live.personal.ComplainActivity")
intent.putExtra("qq","980928062")
RePlugin.startActivity(context,intent)
  • 8.3插件页面接受宿主传递参数
val qq = intent.getStringExtra("qq")
Log.e("ComplainActivity", "onCreate: $qq")
E/ComplainActivity: onCreate: 980928062
  • 8.4插件页面跳转到宿主页面传递参数并通信同上.

9,插件调试

buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        applicationVariants.all { variant ->
            variant.outputs.all {
                variant.getPackageApplication().outputDirectory = new File(project.rootDir.absolutePath + "/main/src/main/assets/external")
                outputFileName = "personal.apk"
            }
        }
    }
}

异常分析:

11/19 14:42:42: Launching 'plugin-personal' on HUAWEI OXF-AN00.
Installation did not succeed.
The application could not be installed.

List of apks:
[0] 'D:\ZxnWork\YinYuan\plugin-personal\build\outputs\apk\debug\personal.apk'
Installation failed due to: 'Invalid File: D:\ZxnWork\YinYuan\plugin-personal\build\outputs\apk\debug\personal.apk'
  • 9.3 插件build的速度特别慢,优化办法
    gradle.proerties
org.gradle.jvmargs=-Xmx4608M 
android.useAndroidX=true
android.enableJetifier=true
kotlin.code.style=official
android.injected.testOnly=false
org.gradle.daemon=true
org.gradle.parallel=true
org.gradle.configureondemand=true

10,宿主程序下载,安装插件

  • 10.1
  • 若插件需要下载,则请覆写RePluginCallbacks.onPluginNotExistsForActivity方法,并在此打开您的下载页面并控制其逻辑
  • 除非是基础和核心功能插件,否则请尽量减少“静默安装”(指的是用户无感知的情况下,偷偷在后台安装)插件的情况,以减少内部存储空间的消耗,降低对用户的影响。
  • 插件与宿主共享类方案,插件可“无缝”使用宿主类,调用RePluginConfig.setUseHostClassIfNotFound(true)即可。如不调用,则默认不支持此功能,这样更稳定
  • 在插件不存在时,提示下载
@Override
public boolean onPluginNotExistsForActivity(Context context, String plugin, Intent intent, int process) {
    return super.onPluginNotExistsForActivity(context, plugin, intent, process);
}

相关文章

网友评论

      本文标题:RePlugin插件化框架接入笔记

      本文链接:https://www.haomeiwen.com/subject/uzxbbktx.html