diff --git a/plugin/ResourcePlugin/.gitignore b/plugin/ResourcePlugin/.gitignore deleted file mode 100644 index 42afabf..0000000 --- a/plugin/ResourcePlugin/.gitignore +++ /dev/null @@ -1 +0,0 @@ -/build \ No newline at end of file diff --git a/plugin/ResourcePlugin/README.md b/plugin/ResourcePlugin/README.md deleted file mode 100644 index 1d64ca6..0000000 --- a/plugin/ResourcePlugin/README.md +++ /dev/null @@ -1,49 +0,0 @@ -资源重复检查,目前仅支持: -- Drawable 目录重复资源检查 -- Assets 目录重复资源检查 -- layout 目录重复资源检查 - -配置 maven 镜像源和依赖 -```java -buildscript { - repositories { - ... - // maven 源 - maven{ - url "https://raw.githubusercontent.com/MRwangqi/Maven/main" - } - } - dependencies { - classpath "com.android.tools.build:gradle:7.0.4" - // 依赖 resourcePlugin 插件 - classpath "com.github.MRwangqi:resourcePlugin:1.0.0" - } -} -``` - -在 app 工程的 build.gradle 中依赖插件(app 为主壳模块) - -```java -plugins { - id 'com.android.application' - // apply resourcePlugin 插件 - id 'resourcePlugin' -} -``` - - - -也有一些小伙伴的项目没有适配 AGP 7.X,所以,这里用 gradle file 写了一遍,但只实现了 layout 的重复检查,其他两个就交由大家自己实现了,照葫芦画瓢,我觉得应该不难。 - -在 app 工程的 build.gradle 中引入 [resourcePlugin.gradle](../../app/resourcePlugin.gradle) 文件 -```aidl -plugins { - id 'com.android.application' -} -apply from :"resourcePlugin.gradle" -``` - - -示例 demo 运行效果: -![result](result.png) - diff --git a/plugin/ResourcePlugin/build.gradle b/plugin/ResourcePlugin/build.gradle deleted file mode 100644 index c697f7f..0000000 --- a/plugin/ResourcePlugin/build.gradle +++ /dev/null @@ -1,44 +0,0 @@ -apply plugin: 'groovy' -apply plugin: 'kotlin' -apply plugin: 'java-gradle-plugin' -apply plugin: 'maven-publish' - -dependencies { - implementation gradleApi() - implementation localGroovy() - api "com.android.tools.build:gradle-api:$apgVersion" - api "com.android.tools.build:gradle:$apgVersion" - implementation "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion" -} - -version = "1.0.0" -group = "com.github.MRwangqi" - -gradlePlugin { - plugins { - version { - id = 'resourcePlugin' - implementationClass = 'com.codelang.plugin.ResourcePlugin' - } - } -} - - -afterEvaluate { - publishing { - publications { - maven(MavenPublication) { - from components.java - groupId = "com.github.MRwangqi" - artifactId = "resourcePlugin" - version = "1.0.0" - } - } - - repositories { - maven { - url = "../../build/Maven" - } - } - } -} \ No newline at end of file diff --git a/plugin/ResourcePlugin/result.png b/plugin/ResourcePlugin/result.png deleted file mode 100644 index 108e439..0000000 Binary files a/plugin/ResourcePlugin/result.png and /dev/null differ diff --git a/plugin/ResourcePlugin/src/main/java/com/codelang/plugin/ResourcePlugin.kt b/plugin/ResourcePlugin/src/main/java/com/codelang/plugin/ResourcePlugin.kt deleted file mode 100644 index bf57e65..0000000 --- a/plugin/ResourcePlugin/src/main/java/com/codelang/plugin/ResourcePlugin.kt +++ /dev/null @@ -1,172 +0,0 @@ -package com.codelang.plugin - -import com.android.build.gradle.AppExtension -import org.gradle.api.Plugin -import org.gradle.api.Project -import java.io.File -import org.gradle.api.logging.Logger - - -class ResourcePlugin : Plugin { - - var logger: Logger? = null - override fun apply(project: Project) { - println("ResourcePlugin---------->") - val appExtension = project.extensions.getByType(AppExtension::class.java) - - logger = project.logger - - appExtension.applicationVariants.all { variant -> - val mergeResourcesTask = variant.mergeResourcesProvider.get() - val mergeAssetsTask = variant.mergeAssetsProvider.get() - - val resourceTask = project.task("DuplicateResource${variant.name.capitalize()}Check") - resourceTask.doLast { - // 获取所有参与合并的 res 资源 - val files = variant.allRawAndroidResources.files - checkResLayout(files) - checkResDrawable(files) - checkAssets(mergeAssetsTask.getLibraries()?.files, mergeAssetsTask.sourceFolderInputs.files) - } - mergeResourcesTask.dependsOn(resourceTask, mergeAssetsTask) - } - } - - /** - * 检查 Layout 资源是否冲突 - */ - private fun checkResLayout(files: Set) { - val hashMap = HashMap() - val duplicateLayout = HashMap>() - files.forEach { file -> - val layoutDir = file.listFiles()?.firstOrNull { it.isDirectory && it.name == "layout" } - if (layoutDir != null) { - layoutDir.listFiles()?.forEach { layout -> - if (hashMap.containsKey(layout.name)) { - var list = duplicateLayout[layout.name] - if (list == null) { - list = arrayListOf() - list.add(hashMap[layout.name] ?: "") - } - list.add(layout.absolutePath) - // 资源重复 - duplicateLayout[layout.name] = list - } else { - hashMap[layout.name] = layout.absolutePath - } - } - } - } - - if (duplicateLayout.isNotEmpty()) { - println() - logger?.error("-------- layout 资源重复----------") - duplicateLayout.forEach { - println(it.key) - it.value.forEach { - logger?.error("----> $it") - } - } - } - } - - /** - * 检查 Drawable 资源是否冲突 - */ - private fun checkResDrawable(files: Set) { - val hashMap = HashMap>() - val duplicateDrawable = HashMap>() - files.forEach { file -> - val drawableDir = file.listFiles()?.filter { it.isDirectory && it.name.contains("drawable") } - - val moduleMap = HashMap>() - drawableDir?.map { drawable -> - drawable.listFiles()?.forEach { file -> - // Drawable 是否重复只判断文件名,不判断扩展名,因为 XX.png,xx.jpeg,xx.webp 都是指一个图片 - if (hashMap.containsKey(file.nameWithoutExtension)) { - // 资源重复 - var list = duplicateDrawable[file.nameWithoutExtension] - if (list == null) { - list = arrayListOf() - list.addAll(hashMap[file.nameWithoutExtension] ?: arrayListOf()) - } - list.add(file.absolutePath) - duplicateDrawable[file.nameWithoutExtension] = list - } else { - val list = moduleMap[file.nameWithoutExtension] ?: arrayListOf() - list.add(file.absolutePath) - moduleMap[file.nameWithoutExtension] = list - } - } - } - - hashMap.putAll(moduleMap) - } - - if (duplicateDrawable.isNotEmpty()) { - println() - logger?.error("-------- Drawable 资源重复----------") - duplicateDrawable.forEach { - println(it.key) - it.value.forEach { - logger?.error("----> $it") - } - } - } - } - - private fun checkAssets(libraryAssets: Set?, sourceFolderAssets: Set) { - - val files = arrayListOf() - files.addAll(libraryAssets?.toList() ?: arrayListOf()) - files.addAll(sourceFolderAssets.toList()) - - val duplicateAssets = HashMap>() - val hashMap = HashMap() - - // 递归遍历 assets 下的文件,以相对路径为 key,然后遍历重复 file - files.forEach { assets -> - val list = arrayListOf().apply { - getFiles(assets, this) - } - - list.forEach { - val relativePath = it.absolutePath.replace(assets.absolutePath, "") - if (hashMap.containsKey(relativePath) && relativePath.isNotEmpty()) { - // assets 资源重复 - var l = duplicateAssets[relativePath] - if (l == null) { - l = arrayListOf() - l.add(hashMap[relativePath] ?: "") - } - l.add(it.absolutePath) - duplicateAssets[relativePath] = l - } else { - hashMap[relativePath] = it.absolutePath - } - } - } - - if (duplicateAssets.isNotEmpty()) { - println() - logger?.error("-------- assets 资源重复----------") - duplicateAssets.forEach { - println(it.key) - it.value.forEach { - logger?.error("----> $it") - } - } - } - } - - private fun getFiles(file: File, list: ArrayList) { - if (file.isDirectory) { - file.listFiles()?.forEach { - getFiles(it, list) - } - } else { - list.add(file) - } - } - -} \ No newline at end of file

AltStyle によって変換されたページ (->オリジナル) /