RePlugin插件化框架接入笔记

2020-11-13  本文已影响0人  CarlosLynn

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目录进行模拟

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架构模式.

lateinit property mViewModel has not been initialized

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

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

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

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的启动或者关闭动画.

val intent = Intent()
intent.component = ComponentName("personal","com.live.personal.ComplainActivity")
intent.putExtra("qq","980928062")
RePlugin.startActivity(context,intent)
val qq = intent.getStringExtra("qq")
Log.e("ComplainActivity", "onCreate: $qq")
E/ComplainActivity: onCreate: 980928062

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'
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,宿主程序下载,安装插件

@Override
public boolean onPluginNotExistsForActivity(Context context, String plugin, Intent intent, int process) {
    return super.onPluginNotExistsForActivity(context, plugin, intent, process);
}
上一篇下一篇

猜你喜欢

热点阅读