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: 12 June 2025