M8Test Help

通用插件

17

开发通用插件步骤如下:

  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.plugins.common.fileicons" 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.m8test.sdk) }
  1. 编写插件继承 AbstractPluggableApkPlugin 并重写所需要的方法

package com.m8test.plugins.common.fileicons import com.m8test.plugin.api.ApkPluginProvider import com.m8test.plugin.impl.AbstractPluggableApkPlugin import com.m8test.util.FileIconUtils /** * Description TODO * * @date 2024/12/23 22:24:15 * @author M8Test, [email protected], https://m8test.com */ class FileIconsPlugin(apkPluginProvider: ApkPluginProvider) : AbstractPluggableApkPlugin(apkPluginProvider) { override fun onInstall() { super.onInstall() FileIconUtils.register(this) { file -> val id: Int? = when (file.extension) { "7z" -> R.drawable.ic_7z "ai" -> R.drawable.ic_ai "avi" -> R.drawable.ic_avi "bat" -> R.drawable.ic_bat "bin" -> R.drawable.ic_bin "bmp" -> R.drawable.ic_bmp "conf" -> R.drawable.ic_conf "css" -> R.drawable.ic_css "doc" -> R.drawable.ic_doc "docx" -> R.drawable.ic_docx "eot" -> R.drawable.ic_eot "htm" -> R.drawable.ic_htm "html" -> R.drawable.ic_html "ico" -> R.drawable.ic_ico "ini" -> R.drawable.ic_ini "jar" -> R.drawable.ic_jar "java" -> R.drawable.ic_java "jpeg" -> R.drawable.ic_jpeg "jpg" -> R.drawable.ic_jpg "js" -> R.drawable.ic_js "md" -> R.drawable.ic_md "mp3" -> R.drawable.ic_mp3 "mp4" -> R.drawable.ic_mp4 "mp5" -> R.drawable.ic_mp5 "mpge" -> R.drawable.ic_mpge "pdf" -> R.drawable.ic_pdf "pl" -> R.drawable.ic_pl "png" -> R.drawable.ic_png "ppt" -> R.drawable.ic_ppt "psd" -> R.drawable.ic_psd "py" -> R.drawable.ic_py "rar" -> R.drawable.ic_rar "rm" -> R.drawable.ic_rm "sh" -> R.drawable.ic_sh "svg" -> R.drawable.ic_svg "tar" -> R.drawable.ic_tar "text" -> R.drawable.ic_text "ttf" -> R.drawable.ic_ttf "woff" -> R.drawable.ic_woff "xlsx" -> R.drawable.ic_xlsx "xml" -> R.drawable.ic_xml "yaml" -> R.drawable.ic_yaml "yml" -> R.drawable.ic_yml "zip" -> R.drawable.ic_zip else -> null } id?.let { getResources().getDrawable(it, null) } } } }
  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="common" /> <meta-data android:name="com.m8test.plugin.name" android:value="file-icons" /> <meta-data android:name="com.m8test.plugin.className" android:value="com.m8test.plugins.common.fileicons.FileIconsPlugin" /> </application> </manifest>
  • com.m8test.plugin.type: 插件类型, 此处为 common

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

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

Last modified: 29 April 2025