diff --git a/app/build.gradle b/app/build.gradle index 5666d31d..e6ff7fc4 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -172,7 +172,7 @@ if (PLUGIN_ENABLE.toBoolean()) { //例 fileNameBuilder.append("android_junk_code_keep") - contentBuilder.append( "\n") } diff --git a/build.gradle b/build.gradle index 255481e8..617aba19 100644 --- a/build.gradle +++ b/build.gradle @@ -11,7 +11,7 @@ buildscript { classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" classpath "com.getkeepsafe.dexcount:dexcount-gradle-plugin:3.0.1" if (PLUGIN_ENABLE.toBoolean()) { - classpath "com.github.qq549631030:android-junk-code:1.3.0" + classpath "com.github.qq549631030:android-junk-code:1.3.1" } // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files diff --git a/gradle.properties b/gradle.properties index 50440ed9..fd349bf3 100644 --- a/gradle.properties +++ b/gradle.properties @@ -24,4 +24,5 @@ org.gradle.warning.mode=all RELEASE_SIGNING_ENABLED=true +#PLUGIN_ENABLE = false PLUGIN_ENABLE = true \ No newline at end of file diff --git a/library/build.gradle b/library/build.gradle index 99d48dc1..f73dc2b2 100644 --- a/library/build.gradle +++ b/library/build.gradle @@ -81,5 +81,5 @@ java { dependencies { implementation gradleApi() implementation 'com.squareup:javapoet:1.13.0' - compileOnly 'com.android.tools.build:gradle-api:7.1.0' + compileOnly 'com.android.tools.build:gradle-api:7.0.0' } \ No newline at end of file diff --git a/library/gradle.properties b/library/gradle.properties index 06af2411..d771665c 100644 --- a/library/gradle.properties +++ b/library/gradle.properties @@ -1,7 +1,7 @@ #project GROUP=com.github.qq549631030 POM_ARTIFACT_ID=android-junk-code -VERSION_NAME=1.3.0 +VERSION_NAME=1.3.1 POM_PACKAGING=jar POM_NAME=AndroidJunkCode 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 08c01171..f344eb28 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 @@ -1,6 +1,6 @@ package cn.hx.plugin.junkcode.plugin - +import cn.hx.plugin.junkcode.utils.JunkUtil import org.gradle.api.Plugin import org.gradle.api.Project @@ -12,10 +12,8 @@ class AndroidJunkCodePlugin implements Plugin { if (!android || !android.hasProperty("applicationVariants")) { throw IllegalArgumentException("must apply this plugin after 'com.android.application'") } - def androidComponents = project.extensions.findByName("androidComponents") - //AGP 7.4.0+ - if (androidComponents && androidComponents.hasProperty("pluginVersion") - && (androidComponents.pluginVersion.major > 7 || androidComponents.pluginVersion.minor >= 4)) { + //AGP 7.0.0+ + if (JunkUtil.isAGP7_0_0(project)) { new NewVariantApiPlugin().apply(project) } else { new OldVariantApiPlugin().apply(project) diff --git a/library/src/main/groovy/cn/hx/plugin/junkcode/plugin/NewVariantApiPlugin.groovy b/library/src/main/groovy/cn/hx/plugin/junkcode/plugin/NewVariantApiPlugin.groovy index 9d60ac75..897214e7 100644 --- a/library/src/main/groovy/cn/hx/plugin/junkcode/plugin/NewVariantApiPlugin.groovy +++ b/library/src/main/groovy/cn/hx/plugin/junkcode/plugin/NewVariantApiPlugin.groovy @@ -2,18 +2,21 @@ package cn.hx.plugin.junkcode.plugin import cn.hx.plugin.junkcode.ext.AndroidJunkCodeExt import cn.hx.plugin.junkcode.ext.JunkCodeConfig -import cn.hx.plugin.junkcode.task.ManifestMergeTask import cn.hx.plugin.junkcode.task.GenerateJunkCodeTask +import cn.hx.plugin.junkcode.task.ManifestMergeTask +import cn.hx.plugin.junkcode.utils.JunkUtil import com.android.build.api.artifact.SingleArtifact import org.gradle.api.Plugin import org.gradle.api.Project import org.gradle.api.tasks.TaskProvider +/** + * AGP 7.0.0+ + */ class NewVariantApiPlugin implements Plugin { @Override void apply(Project project) { - def android = project.extensions.findByName("android") def androidComponents = project.extensions.findByName("androidComponents") def generateJunkCodeExt = project.extensions.create("androidJunkCode", AndroidJunkCodeExt, project.container(JunkCodeConfig)) androidComponents.onVariants(androidComponents.selector().all(), { variant -> @@ -23,24 +26,26 @@ class NewVariantApiPlugin implements Plugin { println("AndroidJunkCode: generate code for variant $variantName? ${junkCodeConfig != null}") } if (junkCodeConfig) { - def junkCodeOutDir = new File(project.buildDir, "generated/source/junk/${variantName}") + def junkCodeOutDir = project.layout.buildDirectory.dir("generated/source/junk/${variantName}") def generateJunkCodeTaskProvider = project.tasks.register("generate${variantName.capitalize()}JunkCode", GenerateJunkCodeTask) { config = junkCodeConfig - namespace = android.namespace - javaOutputFolder.set(new File(junkCodeOutDir, "java")) - resOutputFolder.set(new File(junkCodeOutDir, "res")) - manifestOutputFile.set(new File(junkCodeOutDir, "AndroidManifest.xml")) - proguardOutputFile.set(new File(junkCodeOutDir, "proguard-rules.pro")) - } - if (variant.sources.java) { - variant.sources.java.addGeneratedSourceDirectory(generateJunkCodeTaskProvider, { - it.javaOutputFolder - }) + namespace = variant.namespace + javaOutputFolder.set(junkCodeOutDir.map { it.dir("java") }) + resOutputFolder.set(junkCodeOutDir.map { it.dir("res") }) + manifestOutputFile.set(junkCodeOutDir.map { it.file("AndroidManifest.xml") }) + proguardOutputFile.set(junkCodeOutDir.map { it.file("proguard-rules.pro") }) } - if (variant.sources.res) { - variant.sources.res.addGeneratedSourceDirectory(generateJunkCodeTaskProvider, { - it.resOutputFolder - }) + if (JunkUtil.isAGP7_4_0(project)) { + if (variant.sources.java) { + variant.sources.java.addGeneratedSourceDirectory(generateJunkCodeTaskProvider, { + it.javaOutputFolder + }) + } + if (variant.sources.res) { + variant.sources.res.addGeneratedSourceDirectory(generateJunkCodeTaskProvider, { + it.resOutputFolder + }) + } } TaskProvider manifestUpdater = project.tasks.register('merge' + variantName.capitalize() + 'JunkCodeManifest', ManifestMergeTask) { it.genManifestFile.set(generateJunkCodeTaskProvider.flatMap { @@ -56,5 +61,21 @@ class NewVariantApiPlugin implements Plugin { }) } }) + if (!JunkUtil.isAGP7_4_0(project)) { + def android = project.extensions.findByName("android") + android.applicationVariants.all { variant -> + def variantName = variant.name + def junkCodeConfig = generateJunkCodeExt.variantConfig.findByName(variantName) + if (junkCodeConfig) { + def generateJunkCodeTaskProvider = project.tasks.named("generate${variantName.capitalize()}JunkCode", GenerateJunkCodeTask) + variant.registerJavaGeneratingTask(generateJunkCodeTaskProvider, generateJunkCodeTaskProvider.get().javaOutputFolder.get().asFile) + + variant.registerGeneratedResFolders(project.files(generateJunkCodeTaskProvider.map { + it.resOutputFolder.asFile + }).builtBy(generateJunkCodeTaskProvider)) + variant.mergeResourcesProvider.configure { dependsOn(generateJunkCodeTaskProvider.get()) } + } + } + } } } \ No newline at end of file diff --git a/library/src/main/groovy/cn/hx/plugin/junkcode/plugin/OldVariantApiPlugin.groovy b/library/src/main/groovy/cn/hx/plugin/junkcode/plugin/OldVariantApiPlugin.groovy index 8250d9a7..3379bdb9 100644 --- a/library/src/main/groovy/cn/hx/plugin/junkcode/plugin/OldVariantApiPlugin.groovy +++ b/library/src/main/groovy/cn/hx/plugin/junkcode/plugin/OldVariantApiPlugin.groovy @@ -7,7 +7,7 @@ import org.gradle.api.Plugin import org.gradle.api.Project /** - * before AGP 7.4.0 + * before AGP 7.0.0 */ class OldVariantApiPlugin implements Plugin { diff --git a/library/src/main/groovy/cn/hx/plugin/junkcode/task/AndroidJunkCodeTask.groovy b/library/src/main/groovy/cn/hx/plugin/junkcode/task/AndroidJunkCodeTask.groovy index eb930c45..9055ace6 100644 --- a/library/src/main/groovy/cn/hx/plugin/junkcode/task/AndroidJunkCodeTask.groovy +++ b/library/src/main/groovy/cn/hx/plugin/junkcode/task/AndroidJunkCodeTask.groovy @@ -6,7 +6,7 @@ import org.gradle.api.DefaultTask import org.gradle.api.tasks.* /** - * before AGP 7.4.0 + * before AGP 7.0.0 */ abstract class AndroidJunkCodeTask extends DefaultTask { diff --git a/library/src/main/groovy/cn/hx/plugin/junkcode/task/GenerateJunkCodeTask.groovy b/library/src/main/groovy/cn/hx/plugin/junkcode/task/GenerateJunkCodeTask.groovy index 98d11517..7315d1f7 100644 --- a/library/src/main/groovy/cn/hx/plugin/junkcode/task/GenerateJunkCodeTask.groovy +++ b/library/src/main/groovy/cn/hx/plugin/junkcode/task/GenerateJunkCodeTask.groovy @@ -5,6 +5,7 @@ import cn.hx.plugin.junkcode.utils.JunkUtil import org.gradle.api.DefaultTask import org.gradle.api.file.DirectoryProperty import org.gradle.api.file.RegularFileProperty +import org.gradle.api.provider.Property import org.gradle.api.tasks.* abstract class GenerateJunkCodeTask extends DefaultTask { @@ -13,7 +14,7 @@ abstract class GenerateJunkCodeTask extends DefaultTask { abstract JunkCodeConfig config @Input - abstract String namespace + abstract Property getNamespace() @OutputDirectory abstract DirectoryProperty getJavaOutputFolder() @@ -53,7 +54,7 @@ abstract class GenerateJunkCodeTask extends DefaultTask { packageName = config.packageBase + "." + JunkUtil.generateName(i) } } - def list = JunkUtil.generateActivity(javaDir, resDir, namespace, packageName, config) + def list = JunkUtil.generateActivity(javaDir, resDir, getNamespace().get(), packageName, config) activityList.addAll(list) JunkUtil.generateJava(javaDir, packageName, config) packageList.add(packageName) diff --git a/library/src/main/groovy/cn/hx/plugin/junkcode/utils/JunkUtil.groovy b/library/src/main/groovy/cn/hx/plugin/junkcode/utils/JunkUtil.groovy index f6e428f2..d8298687 100644 --- a/library/src/main/groovy/cn/hx/plugin/junkcode/utils/JunkUtil.groovy +++ b/library/src/main/groovy/cn/hx/plugin/junkcode/utils/JunkUtil.groovy @@ -6,6 +6,7 @@ import com.squareup.javapoet.ClassName import com.squareup.javapoet.JavaFile import com.squareup.javapoet.MethodSpec import com.squareup.javapoet.TypeSpec +import org.gradle.api.Project import javax.lang.model.element.Modifier import java.nio.file.Files @@ -397,4 +398,29 @@ class JunkUtil { } } } + + /** + * 是否是AGP7.0.0及以上 + * @param project + * @return + */ + static boolean isAGP7_0_0(Project project) { + def androidComponents = project.extensions.findByName("androidComponents") + if (androidComponents && androidComponents.hasProperty("pluginVersion")) { + return true + } + return false + } + /** + * 是否是AGP7.4.0及以上 + * @param project + * @return + */ + static boolean isAGP7_4_0(Project project) { + def androidComponents = project.extensions.findByName("androidComponents") + if (androidComponents && androidComponents.hasProperty("pluginVersion") && (androidComponents.pluginVersion.major > 7 || androidComponents.pluginVersion.minor >= 4)) { + return true + } + return false + } } \ No newline at end of file