Skip to content

Commit

Permalink
Merge pull request #195 from puboe/configuration-avoidance
Browse files Browse the repository at this point in the history
Use Gradle's configuration avoidance APIs
  • Loading branch information
kezong authored Sep 10, 2020
2 parents a95c54f + 7907426 commit e584c4f
Show file tree
Hide file tree
Showing 3 changed files with 190 additions and 190 deletions.
26 changes: 16 additions & 10 deletions source/src/main/groovy/com/kezong/fataar/FlavorArtifact.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.kezong.fataar
import com.android.build.gradle.api.LibraryVariant
import org.gradle.api.Project
import org.gradle.api.Task
import org.gradle.api.UnknownTaskException
import org.gradle.api.artifacts.ModuleVersionIdentifier
import org.gradle.api.artifacts.ResolvedDependency
import org.gradle.api.artifacts.component.ComponentArtifactIdentifier
Expand All @@ -12,6 +13,7 @@ import org.gradle.api.internal.artifacts.DefaultResolvedArtifact
import org.gradle.api.internal.tasks.TaskDependencyContainer
import org.gradle.api.internal.tasks.TaskDependencyResolveContext
import org.gradle.api.tasks.TaskDependency
import org.gradle.api.tasks.TaskProvider
import org.gradle.internal.Factory
import org.gradle.internal.component.model.DefaultIvyArtifactName

Expand Down Expand Up @@ -95,22 +97,26 @@ class FlavorArtifact {
return new File(outputName)
}

private static TaskDependency createTaskDependency(Project project, LibraryVariant variant) {
def taskPath = 'bundle' + variant.name.capitalize()
Task bundleTask = project.tasks.findByPath(taskPath)
if (bundleTask == null) {
taskPath = 'bundle' + variant.name.capitalize() + "Aar"
bundleTask = project.tasks.findByPath(taskPath)
}
if (bundleTask == null) {
throw new RuntimeException("Can not find task ${taskPath}!")
static TaskProvider<Task> getBundleTaskProvider(final Project project, final LibraryVariant variant) throws UnknownTaskException {
def taskPath = "bundle" + variant.name.capitalize()
TaskProvider bundleTask
try {
bundleTask = project.tasks.named(taskPath)
} catch (UnknownTaskException ignored) {
taskPath += "Aar"
bundleTask = project.tasks.named(taskPath)
}
return bundleTask
}

private static TaskDependency createTaskDependency(Project project, LibraryVariant variant) {
def bundleTaskProvider = getBundleTaskProvider(project, variant)

return new TaskDependency() {
@Override
Set<? extends Task> getDependencies(@Nullable Task task) {
def set = new HashSet()
set.add(bundleTask)
set.add(bundleTaskProvider.get())
return set
}
}
Expand Down
143 changes: 74 additions & 69 deletions source/src/main/groovy/com/kezong/fataar/RProcessor.groovy
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package com.kezong.fataar

import com.android.build.gradle.api.LibraryVariant

import org.gradle.api.Project
import org.gradle.api.Task
import org.gradle.api.tasks.TaskProvider
import org.gradle.api.tasks.bundling.Zip
import org.gradle.api.tasks.compile.JavaCompile
import org.gradle.jvm.tasks.Jar


/**
* R file processor
* @author kezong on 2019/7/16.
Expand Down Expand Up @@ -48,50 +47,50 @@ class RProcessor {
mAarOutputPath = mVersionAdapter.getOutputPath()
}

void inject(Task bundleTask) {
def RFileTask = createRFileTask(mJavaDir)
def RClassTask = createRClassTask(mJavaDir, mClassDir)
def RJarTask = createRJarTask(mClassDir, mJarDir)
void inject(TaskProvider<Task> bundleTask) {
def reBundleAar = createBundleAarTask(mAarUnZipDir, mAarOutputDir, mAarOutputPath)
def RJarTask = createRJarTask(mClassDir, mJarDir, reBundleAar)
def RClassTask = createRClassTask(mJavaDir, mClassDir, RJarTask)
def RFileTask = createRFileTask(mJavaDir, RClassTask)

reBundleAar.doFirst {
mProject.copy {
from mProject.zipTree(mAarOutputPath)
into mAarUnZipDir
reBundleAar.configure {
doFirst {
mProject.copy {
from mProject.zipTree(mAarOutputPath)
into mAarUnZipDir
}
deleteEmptyDir(mAarUnZipDir)
}
doLast {
Utils.logAnytime("target: $mAarOutputPath")
}
deleteEmptyDir(mAarUnZipDir)
}

reBundleAar.doLast {
Utils.logAnytime("target: $mAarOutputPath")
}
bundleTask.configure {
finalizedBy(RFileTask)

bundleTask.doFirst {
File f = new File(mAarOutputPath)
if (f.exists()) {
f.delete()
doFirst {
File f = new File(mAarOutputPath)
if (f.exists()) {
f.delete()
}
mJarDir.getParentFile().deleteDir()
mJarDir.mkdirs()
}
mJarDir.getParentFile().deleteDir()
mJarDir.mkdirs()
}

bundleTask.doLast {
// support gradle 5.1 && gradle plugin 3.4 before, the outputName is changed
File file = new File(mAarOutputPath)
if (!file.exists()) {
mAarOutputPath = mAarOutputDir.absolutePath + "/" + mProject.name + ".aar"
if (Utils.compareVersion(mProject.gradle.gradleVersion, "6.0.1") >= 0) {
reBundleAar.getArchiveFileName().set(new File(mAarOutputPath).name)
} else {
reBundleAar.archiveName = new File(mAarOutputPath).name
doLast {
// support gradle 5.1 && gradle plugin 3.4 before, the outputName is changed
File file = new File(mAarOutputPath)
if (!file.exists()) {
mAarOutputPath = mAarOutputDir.absolutePath + "/" + mProject.name + ".aar"
if (Utils.compareVersion(mProject.gradle.gradleVersion, "6.0.1") >= 0) {
reBundleAar.getArchiveFileName().set(new File(mAarOutputPath).name)
} else {
reBundleAar.archiveName = new File(mAarOutputPath).name
}
}
}
}

bundleTask.finalizedBy(RFileTask)
RFileTask.finalizedBy(RClassTask)
RClassTask.finalizedBy(RJarTask)
RJarTask.finalizedBy(reBundleAar)
}

private def createRFile(AndroidArchiveLibrary library, def rFolder, ConfigObject symbolsMap) {
Expand Down Expand Up @@ -169,54 +168,60 @@ class RProcessor {
return map
}

private Task createRFileTask(final File destFolder) {
def task = mProject.tasks.create(name: 'createRsFile' + mVariant.name)
task.doLast {
if (destFolder.exists()) {
destFolder.deleteDir()
}
if (mLibraries != null && mLibraries.size() > 0) {
def symbolsMap = getSymbolsMap()
mLibraries.each {
Utils.logInfo("Generate R File, Library:${it.name}")
createRFile(it, destFolder, symbolsMap)
private TaskProvider createRFileTask(final File destFolder, final TaskProvider RClassTask) {
def task = mProject.tasks.register('createRsFile' + mVariant.name) {
finalizedBy(RClassTask)

doLast {
if (destFolder.exists()) {
destFolder.deleteDir()
}
if (mLibraries != null && mLibraries.size() > 0) {
def symbolsMap = getSymbolsMap()
mLibraries.each {
Utils.logInfo("Generate R File, Library:${it.name}")
createRFile(it, destFolder, symbolsMap)
}
}
}
}

return task
}

private Task createRClassTask(final def sourceDir, final def destinationDir) {
private TaskProvider createRClassTask(final File sourceDir, final File destinationDir, final TaskProvider RJarTask) {
mProject.mkdir(destinationDir)

def classpath = mVersionAdapter.getRClassPath()
String taskName = "compileRs${mVariant.name.capitalize()}"
Task task = mProject.getTasks().create(taskName, JavaCompile.class, {
TaskProvider task = mProject.getTasks().register(taskName, JavaCompile.class) {
finalizedBy(RJarTask)

it.source = sourceDir.path
it.sourceCompatibility = mProject.android.compileOptions.sourceCompatibility
it.targetCompatibility = mProject.android.compileOptions.targetCompatibility
it.classpath = classpath
it.destinationDir destinationDir
})
it.destinationDir = destinationDir

task.doFirst {
Utils.logInfo("Compile R.class, Dir:${sourceDir.path}")
Utils.logInfo("Compile R.class, classpath:${classpath.first().absolutePath}")
doFirst {
Utils.logInfo("Compile R.class, Dir:${sourceDir.path}")
Utils.logInfo("Compile R.class, classpath:${classpath.first().absolutePath}")

if (mGradlePluginVersion != null && Utils.compareVersion(mGradlePluginVersion, "3.3.0") >= 0) {
mProject.copy {
from mProject.zipTree(mVersionAdapter.getRClassPath().first().absolutePath + "/R.jar")
into mVersionAdapter.getRClassPath().first().absolutePath
if (mGradlePluginVersion != null && Utils.compareVersion(mGradlePluginVersion, "3.3.0") >= 0) {
mProject.copy {
from mProject.zipTree(mVersionAdapter.getRClassPath().first().absolutePath + "/R.jar")
into mVersionAdapter.getRClassPath().first().absolutePath
}
}
}
}
return task
}

private Task createRJarTask(final def fromDir, final def desFile) {
private TaskProvider createRJarTask(final File fromDir, final File desFile, final TaskProvider reBundleAarTask) {
String taskName = "createRsJar${mVariant.name.capitalize()}"
Task task = mProject.getTasks().create(taskName, Jar.class, {
TaskProvider task = mProject.getTasks().register(taskName, Jar) {
finalizedBy(reBundleAarTask)

it.from fromDir.path
// The destinationDir property has been deprecated.
// This is scheduled to be removed in Gradle 7.0. Please use the destinationDirectory property instead.
Expand All @@ -225,33 +230,33 @@ class RProcessor {
it.getDestinationDirectory().set(desFile)
} else {
it.archiveName = "r-classes.jar"
it.destinationDir desFile
it.destinationDir = desFile
}
doFirst {
Utils.logInfo("Generate R.jar, Dir:$fromDir")
}
})
task.doFirst {
Utils.logInfo("Generate R.jar, Dir:$fromDir")
}
return task
}

private Task createBundleAarTask(final File from, final File destDir, final String filePath) {
private TaskProvider createBundleAarTask(final File from, final File destDir, final String filePath) {
String taskName = "reBundleAar${mVariant.name.capitalize()}"
Task task = mProject.getTasks().create(taskName, Zip.class, {
TaskProvider task = mProject.getTasks().register(taskName, Zip.class) {
it.from from
it.include "**"
if (Utils.compareVersion(mProject.gradle.gradleVersion, "6.0.1") >= 0) {
it.getArchiveFileName().set(new File(filePath).name)
it.getDestinationDirectory().set(destDir)
} else {
it.archiveName = new File(filePath).name
it.destinationDir(destDir)
it.destinationDir = destDir
}
})
}

return task
}

def deleteEmptyDir = { file ->
private void deleteEmptyDir(final File file) {
file.listFiles().each { x ->
if (x.isDirectory()) {
if (x.listFiles().size() == 0) {
Expand Down
Loading

0 comments on commit e584c4f

Please sign in to comment.