Skip to content

Commit

Permalink
最低支持到AGP 1.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
qq549631030 committed Sep 24, 2022
1 parent 9b346f5 commit 41f188e
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 78 deletions.
3 changes: 0 additions & 3 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ class AndroidJunkCodeExt {

NamedDomainObjectContainer<JunkCodeConfig> variantConfig

AndroidJunkCodeExt(NamedDomainObjectContainer<JunkCodeConfig> variantConfig) {
this.variantConfig = variantConfig
}

void variantConfig(Action<? super NamedDomainObjectContainer<JunkCodeConfig>> action) {
action.execute(variantConfig)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,82 +3,71 @@ 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

class AndroidJunkCodePlugin implements Plugin<Project> {

@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)
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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++) {
Expand Down

0 comments on commit 41f188e

Please sign in to comment.