diff --git a/library/build.gradle b/library/build.gradle index a645b638..c14ef475 100644 --- a/library/build.gradle +++ b/library/build.gradle @@ -17,9 +17,6 @@ java { } dependencies { - implementation fileTree(dir: 'libs', include: ['*.jar']) implementation gradleApi() - implementation localGroovy() implementation 'com.squareup:javapoet:1.13.0' - implementation 'com.android.tools.build:gradle:3.0.0' } \ No newline at end of file diff --git a/library/src/main/groovy/cn/hx/plugin/junkcode/ext/AndroidJunkCodeExt.groovy b/library/src/main/groovy/cn/hx/plugin/junkcode/ext/AndroidJunkCodeExt.groovy index f9871c7f..db163729 100644 --- a/library/src/main/groovy/cn/hx/plugin/junkcode/ext/AndroidJunkCodeExt.groovy +++ b/library/src/main/groovy/cn/hx/plugin/junkcode/ext/AndroidJunkCodeExt.groovy @@ -7,6 +7,10 @@ class AndroidJunkCodeExt { NamedDomainObjectContainer variantConfig + AndroidJunkCodeExt(NamedDomainObjectContainer variantConfig) { + this.variantConfig = variantConfig + } + void variantConfig(Action> action) { action.execute(variantConfig) } 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 e05b420e..2938b96b 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 @@ -3,7 +3,6 @@ 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.AndroidJunkCodeTask -import com.android.build.gradle.AppExtension import org.gradle.api.Plugin import org.gradle.api.Project @@ -11,74 +10,64 @@ class AndroidJunkCodePlugin implements Plugin { @Override void apply(Project project) { - def android = project.extensions.findByType(AppExtension) - if (!android) { + def android = project.extensions.findByName("android") + if (!android || !android.hasProperty("applicationVariants")) { throw IllegalArgumentException("must apply this plugin after 'com.android.application'") } - def generateJunkCodeExt = project.extensions.create("androidJunkCode", AndroidJunkCodeExt) - generateJunkCodeExt.variantConfig = project.container(JunkCodeConfig.class, new JunkCodeConfigFactory()) - + def generateJunkCodeExt = project.extensions.create("androidJunkCode", AndroidJunkCodeExt, project.container(JunkCodeConfig)) android.applicationVariants.all { variant -> def variantName = variant.name def junkCodeConfig = generateJunkCodeExt.variantConfig.findByName(variantName) if (junkCodeConfig) { - createGenerateJunkCodeTask(project, android, variant, junkCodeConfig) - } - } - } - - private def createGenerateJunkCodeTask = { project, android, variant, junkCodeConfig -> - def variantName = variant.name - def generateJunkCodeTaskName = "generate${variantName.capitalize()}JunkCode" - def dir = new File(project.buildDir, "generated/source/junk/$variantName") - def resDir = new File(dir, "res") - def javaDir = new File(dir, "java") - def manifestFile = new File(dir, "AndroidManifest.xml") - def packageName = android.namespace//AGP 7.3+ - if (!packageName) {//AGP under 7.3 - //从AndroidManifest.xml找到package name - for (int i = 0; i < variant.sourceSets.size(); i++) { - def sourceSet = variant.sourceSets[i] - if (sourceSet.manifestFile.exists()) { + def junkCodeNamespace = "" + if (android.hasProperty("namespace")) {//AGP 7.3+ + junkCodeNamespace = android.namespace + } else { + //从AndroidManifest.xml找到package name def parser = new XmlParser() - def node = parser.parse(sourceSet.manifestFile) - packageName = node.attribute("package") - if (packageName) { + for (int i = 0; i < sourceSets.size(); i++) { + def sourceSet = sourceSets[i] + if (sourceSet.manifestFile.exists()) { + def node = parser.parse(sourceSet.manifestFile) + if (node.attribute("package")) { + junkCodeNamespace = node.attribute("package") + break + } + } + } + } + def junkCodeOutDir = new File(project.buildDir, "generated/source/junk/$variantName") + def generateJunkCodeTaskName = "generate${variantName.capitalize()}JunkCode" + def generateJunkCodeTask = project.tasks.create(generateJunkCodeTaskName, AndroidJunkCodeTask){ + config = junkCodeConfig + namespace = junkCodeNamespace + outDir = junkCodeOutDir + } + def manifestFile = new File(junkCodeOutDir, "AndroidManifest.xml") + //将自动生成的AndroidManifest.xml加入到一个未被占用的manifest位置(如果都占用了就不合并了,通常较少出现全被占用情况) + for (int i = variant.sourceSets.size() - 1; i >= 0; i--) { + def sourceSet = variant.sourceSets[i] + if (!sourceSet.manifestFile.exists()) { + sourceSet.manifest.srcFile(manifestFile) break } } + def javaDir = new File(junkCodeOutDir, "java") + variant.registerJavaGeneratingTask(generateJunkCodeTask, javaDir) + def resDir = new File(junkCodeOutDir, "res") + if (variant.respondsTo("registerGeneratedResFolders")) {//AGP 3.0.0+ + variant.registerGeneratedResFolders(project + .files(resDir) + .builtBy(generateJunkCodeTask)) + if (variant.hasProperty("mergeResourcesProvider")) {//AGP 3.3.0+ + variant.mergeResourcesProvider.configure { dependsOn(generateJunkCodeTask) } + } else { + variant.mergeResources.dependsOn(generateJunkCodeTask) + } + } else { + variant.registerResGeneratingTask(generateJunkCodeTask, resDir)//AGP 1.1.0+ + } } } - if (!packageName) { - packageName = "" - } - def generateJunkCodeTask = project.task(generateJunkCodeTaskName, type: AndroidJunkCodeTask) { - config = junkCodeConfig - manifestPackageName = packageName - outDir = dir - } - //将自动生成的AndroidManifest.xml加入到一个未被占用的manifest位置(如果都占用了就不合并了,通常较少出现全被占用情况) - for (int i = variant.sourceSets.size() - 1; i >= 0; i--) { - def sourceSet = variant.sourceSets[i] - if (!sourceSet.manifestFile.exists()) { - sourceSet.manifest.srcFile(manifestFile) - break - } - } - if (variant.respondsTo("registerGeneratedResFolders")) { - variant.registerGeneratedResFolders(project - .files(resDir) - .builtBy(generateJunkCodeTask)) - if (variant.hasProperty("mergeResourcesProvider")) { - variant.mergeResourcesProvider.configure { dependsOn(generateJunkCodeTask) } - } else { - //noinspection GrDeprecatedAPIUsage - variant.mergeResources.dependsOn(generateJunkCodeTask) - } - } else { - //noinspection GrDeprecatedAPIUsage - variant.registerResGeneratingTask(generateJunkCodeTask, resDir) - } - variant.registerJavaGeneratingTask(generateJunkCodeTask, javaDir) } } \ No newline at end of file diff --git a/library/src/main/groovy/cn/hx/plugin/junkcode/plugin/JunkCodeConfigFactory.groovy b/library/src/main/groovy/cn/hx/plugin/junkcode/plugin/JunkCodeConfigFactory.groovy deleted file mode 100644 index 8e9aa76b..00000000 --- a/library/src/main/groovy/cn/hx/plugin/junkcode/plugin/JunkCodeConfigFactory.groovy +++ /dev/null @@ -1,12 +0,0 @@ -package cn.hx.plugin.junkcode.plugin - -import cn.hx.plugin.junkcode.ext.JunkCodeConfig -import org.gradle.api.NamedDomainObjectFactory - -class JunkCodeConfigFactory implements NamedDomainObjectFactory { - - @Override - JunkCodeConfig create(String name) { - return new JunkCodeConfig(name) - } -} \ No newline at end of file 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 3b9b18b7..23da3bea 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 @@ -11,7 +11,7 @@ import groovy.text.GStringTemplateEngine import org.gradle.api.DefaultTask import org.gradle.api.tasks.Input import org.gradle.api.tasks.Nested -import org.gradle.api.tasks.OutputDirectories +import org.gradle.api.tasks.OutputDirectory import org.gradle.api.tasks.TaskAction import javax.lang.model.element.Modifier @@ -23,12 +23,12 @@ class AndroidJunkCodeTask extends DefaultTask { static abc = "abcdefghijklmnopqrstuvwxyz".toCharArray() @Nested - JunkCodeConfig config = new JunkCodeConfig() + JunkCodeConfig config @Input - String manifestPackageName = "" + String namespace - @OutputDirectories + @OutputDirectory File outDir @TaskAction @@ -145,7 +145,7 @@ class AndroidJunkCodeTask extends DefaultTask { .addModifiers(Modifier.PROTECTED) .addParameter(bundleClassName, "savedInstanceState") .addStatement("super.onCreate(savedInstanceState)") - .addStatement("setContentView(\$T.layout.${layoutName})", ClassName.get(manifestPackageName, "R")) + .addStatement("setContentView(\$T.layout.${layoutName})", ClassName.get(namespace, "R")) .build()) //其它方法 for (int j = 0; j < config.methodCountPerClass; j++) {