From d82b74cc5bb6e92a80a1385c793c01611ce5a54a Mon Sep 17 00:00:00 2001 From: huangx Date: Fri, 19 Jun 2020 16:55:12 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9task=E5=90=8D=E4=B8=BAgenerat?= =?UTF-8?q?eXXXJunkCode,=E5=8A=A0=E5=85=A5dexCount=E6=8F=92=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 37 +++++++++++++++---- app/build.gradle | 8 ++-- build.gradle | 1 + library/build.gradle | 2 +- .../plugin/AndroidJunkCodePlugin.groovy | 2 +- 5 files changed, 37 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 970a7474..cf443183 100644 --- a/README.md +++ b/README.md @@ -31,16 +31,39 @@ android.applicationVariants.all { variant -> packageBase = "cn.hx.plugin.ui" //生成java类根包名 packageCount = 30 //生成包数量 activityCountPerPackage = 3 //每个包下生成Activity类数量 - otherCountPerPackage = 10 //每个包下生成其它类的数量 - methodCountPerClass = 10 //每个类下生成方法数量 - resPrefix = "lite_junk_" //生成的layout、drawable、string等资源名前缀 - drawableCount = 30 //生成drawable资源数量 - stringCount = 30 //生成string数量 + otherCountPerPackage = 50 //每个包下生成其它类的数量 + methodCountPerClass = 20 //每个类下生成方法数量 + resPrefix = "junk_" //生成的layout、drawable、string等资源名前缀 + drawableCount = 300 //生成drawable资源数量 + stringCount = 300 //生成string数量 }) break } } ``` -### 生在文件所有目录 -build/generated/source/junk \ No newline at end of file +### 生成文件所有目录 +build/generated/source/junk + +### 使用插件[methodCount](https://github.com/KeepSafe/dexcount-gradle-plugin)对比 + +#### 未加垃圾代码 +``` +Total methods in app-debug.apk: 26162 (39.92% used) +Total fields in app-debug.apk: 12771 (19.49% used) +Total classes in app-debug.apk: 2897 (4.42% used) +Methods remaining in app-debug.apk: 39373 +Fields remaining in app-debug.apk: 52764 +Classes remaining in app-debug.apk: 62638 +``` + +#### 加了垃圾代码 +``` +Total methods in app-release-unsigned.apk: 59733 (91.15% used) +Total fields in app-release-unsigned.apk: 13462 (20.54% used) +Total classes in app-release-unsigned.apk: 4488 (6.85% used) +Methods remaining in app-release-unsigned.apk: 5802 +Fields remaining in app-release-unsigned.apk: 52073 +Classes remaining in app-release-unsigned.apk: 61047 +``` +增加了1591个类33571个方法 \ No newline at end of file diff --git a/app/build.gradle b/app/build.gradle index 32d836a6..462eeb5e 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -2,6 +2,7 @@ apply plugin: 'com.android.application' apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions' apply plugin: 'android-junk-code' +apply plugin: 'com.getkeepsafe.dexcount' android { compileSdkVersion 29 @@ -26,17 +27,16 @@ android { android.applicationVariants.all { variant -> switch (variant.name) { - case "debug": case "release": androidJunkCode.configMap.put(variant.name, { packageBase = "cn.hx.plugin.ui" packageCount = 30 activityCountPerPackage = 3 - otherCountPerPackage = 10 + otherCountPerPackage = 50 methodCountPerClass = 20 resPrefix = "junk_" - drawableCount = 30 - stringCount = 30 + drawableCount = 300 + stringCount = 300 }) break } diff --git a/build.gradle b/build.gradle index 5af35660..88ab9148 100644 --- a/build.gradle +++ b/build.gradle @@ -13,6 +13,7 @@ buildscript { classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" classpath 'com.novoda:bintray-release:0.9.2' classpath "cn.hx.plugin:android-junk-code:1.0.1" + classpath "com.getkeepsafe.dexcount:dexcount-gradle-plugin:1.0.2" // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files diff --git a/library/build.gradle b/library/build.gradle index e82a6cd9..0c1e02da 100644 --- a/library/build.gradle +++ b/library/build.gradle @@ -47,7 +47,7 @@ publish { userOrg = bintray_Org bintrayUser = bintray_User bintrayKey = bintrayApikey - dryRun = false + dryRun = true } uploadArchives { diff --git a/library/src/main/groovy/cn/hx/plugin/junkcode/plugin/AndroidJunkCodePlugin.groovy b/library/src/main/groovy/cn/hx/plugin/junkcode/plugin/AndroidJunkCodePlugin.groovy index d53a65bd..19025293 100644 --- a/library/src/main/groovy/cn/hx/plugin/junkcode/plugin/AndroidJunkCodePlugin.groovy +++ b/library/src/main/groovy/cn/hx/plugin/junkcode/plugin/AndroidJunkCodePlugin.groovy @@ -24,7 +24,7 @@ class AndroidJunkCodePlugin implements Plugin { if (junkCodeConfig) { def dir = new File(project.buildDir, "generated/source/junk/$variantName") String packageName = findPackageName(variant) - def generateJunkCodeTask = project.task("generateJunkCode${variantName.capitalize()}", type: AndroidJunkCodeTask) { + def generateJunkCodeTask = project.task("generate${variantName.capitalize()}JunkCode", type: AndroidJunkCodeTask) { junkCodeConfig.delegate = config junkCodeConfig.resolveStrategy = DELEGATE_FIRST junkCodeConfig.call()