M8Test Help

组件插件

19

开发组件插件步骤如下:

  1. 在 build.gradle.kts 文件中添加 m8test sdk 依赖 , 为了减小插件apk大小, 如果是 M8Test Version Catalog 中存在的依赖库请使用 compileOnly 来依赖项目

import com.m8test.util.VersionUtils plugins { alias(m8test.plugins.android.application) alias(m8test.plugins.kotlin.android) } android { namespace = "com.m8test.component.c2" compileSdk = m8test.versions.compileSdk.get().toInt() defaultConfig { minSdk = m8test.versions.minSdk.get().toInt() targetSdk = m8test.versions.targetSdk.get().toInt() versionName = libs.versions.versionName.get() versionCode = VersionUtils.getCode(versionName!!) testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" } buildTypes { release { isMinifyEnabled = false proguardFiles( getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro" ) } } compileOptions { sourceCompatibility = JavaVersion.toVersion(m8test.versions.sourceCompatibility.get()) targetCompatibility = JavaVersion.toVersion(m8test.versions.targetCompatibility.get()) } kotlinOptions { jvmTarget = m8test.versions.jvmTarget.get() } } dependencies { compileOnly(m8test.utilcodex) compileOnly(m8test.m8test.sdk) compileOnly(project(":component:c1:api")) }
  1. 编写插件继承 AbstractComponent 并实现 getVariables 方法

package com.m8test.component.c2 import com.blankj.utilcode.util.AppUtils import com.m8test.plugin.api.ApkPluginProvider import com.m8test.script.core.api.component.Variable import com.m8test.script.core.api.engine.ScriptContext import com.m8test.script.core.impl.component.AbstractComponent /** * Description TODO * * @date 2025/02/02 21:10:37 * @author M8Test, [email protected], https://m8test.com */ class C2Component(apkPluginProvider: ApkPluginProvider) : AbstractComponent(apkPluginProvider) { override fun onInstall() { super.onInstall() addDependency { setName("C1") setVersion(AppUtils.getAppVersionName()) // setUrl("http://") } } override fun getVariables(scriptContext: ScriptContext): List<Variable> { return listOf<Variable>(C2Impl(scriptContext)) } }
  1. 定义一个接口实现 Variable 接口, 在接口中可以定义自己需要的方法

package com.m8test.component.c2 import com.m8test.script.core.api.component.Variable /** * Description TODO * * @date 2025/02/02 21:07:40 * @author M8Test, [email protected], https://m8test.com */ interface C2 : Variable { fun test() }
  1. 实现步骤3定义的接口

package com.m8test.component.c2 import com.m8test.component.c1.api.C1 import com.m8test.script.core.api.engine.ScriptContext import java.lang.reflect.Type /** * Description TODO * * @date 2025/02/02 21:08:08 * @author M8Test, [email protected], https://m8test.com */ class C2Impl(private val scriptContext: ScriptContext) : C2 { override fun test() { val c1 = scriptContext.getCurrentScript().getVariable(C1::class.java) // 调用其他组件的方法 c1.test() scriptContext.getBindings().getConsole() .log("c2 test ${scriptContext.getCurrentScript().getConfig().getName()}") } override fun getPublicType(): Type { return C2::class.java } override fun getGlobalName(): String { return "c2" } override fun isPrefixRequired(): Boolean { return true } override fun isSuffixRequired(): Boolean { return true } }
  1. 在 AndroidManifest.xml 中配置插件信息

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"> <application android:icon="@drawable/ic_launcher"> <meta-data android:name="com.m8test.plugin.description" android:value="本插件实现了简单的组件功能以及演示如何依赖其他组件." /> <meta-data android:name="com.m8test.plugin.url" android:value="https://github.com/m8test/Plugins" /> <meta-data android:name="com.m8test.plugin.type" android:value="component" /> <meta-data android:name="com.m8test.plugin.name" android:value="C2" /> <meta-data android:name="com.m8test.plugin.className" android:value="com.m8test.component.c2.C2Component" /> </application> </manifest>
  • com.m8test.plugin.type: 插件类型, 此处为 component

  • com.m8test.plugin.name: 插件名称, 可以为任意字符串

  • com.m8test.plugin.className: 实现了插件的全类名

  1. 通过 Component.addDependency 添加依赖的组件, 在此之前需要在 build.gradle.kts 文件中添加依赖到的组件提供的 sdk, 使用 compileOnly 即可. 如果没有使用到其他组件的api就不需要此步骤

Last modified: 29 April 2025