Skip to content

Commit

Permalink
Merge branch 'master' of github.com:kezong/fat-aar-android into incre…
Browse files Browse the repository at this point in the history
…mental-build

* 'master' of github.com:kezong/fat-aar-android:
  support merge data-binding files kezong#25 kezong#67
  Refactor duplicated code
  Avoid creation of preBuild tasks
  Avoid generateResources tasks creation
  Migrate sync task
  Migrate more tasks
  Migrate more tasks
  migrate more R related tasks
  Migrate R file task
  migrate mergeManifest tasks
  Migrate mergeJar tasks
  Migrate mergeClasses tasks to config avoidance API
  Fix some warnings

# Conflicts:
#	source/src/main/groovy/com/kezong/fataar/RProcessor.groovy
#	source/src/main/groovy/com/kezong/fataar/VariantProcessor.groovy
  • Loading branch information
puboe committed Sep 10, 2020
2 parents a438b2b + 964e47e commit 08239c4
Show file tree
Hide file tree
Showing 7 changed files with 239 additions and 185 deletions.
2 changes: 1 addition & 1 deletion source/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ dependencies {
implementation gradleApi()
implementation localGroovy()

implementation 'com.android.tools.build:gradle:4.0.0'
implementation 'com.android.tools.build:gradle:3.6.1'
}

//afterEvaluate {
Expand Down
2 changes: 1 addition & 1 deletion source/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-5.1.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-6.0.1-all.zip
8 changes: 8 additions & 0 deletions source/src/main/groovy/com/kezong/fataar/Constants.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.kezong.fataar

class Constants {

public static final String RE_BUNDLE_FOLDER = "aar_rebundle"

public static final String INTERMEDIATES_TEMP_FOLDER = "fat-r"
}
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
147 changes: 76 additions & 71 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 All @@ -35,11 +34,11 @@ class RProcessor {
mGradlePluginVersion = version
mVersionAdapter = new VersionAdapter(project, variant, version)
// R.java dir
mJavaDir = mProject.file("${mProject.getBuildDir()}/intermediates/fat-R/r/${mVariant.dirName}")
mJavaDir = mProject.file("${mProject.getBuildDir()}/intermediates/${Constants.INTERMEDIATES_TEMP_FOLDER}/r/${mVariant.dirName}")
// R.class compile dir
mClassDir = mProject.file("${mProject.getBuildDir()}/intermediates/fat-R/r-class/${mVariant.dirName}")
mClassDir = mProject.file("${mProject.getBuildDir()}/intermediates/${Constants.INTERMEDIATES_TEMP_FOLDER}/r-class/${mVariant.dirName}")
// R.jar dir
mJarDir = mProject.file("${mProject.getBuildDir()}/outputs/aar-R/${mVariant.dirName}/libs")
mJarDir = mProject.file("${mProject.getBuildDir()}/outputs/${Constants.RE_BUNDLE_FOLDER}/${mVariant.dirName}/libs")
// aar zip file
mAarUnZipDir = mJarDir.getParentFile()
// aar output dir
Expand All @@ -48,48 +47,49 @@ 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.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 @@ -167,56 +167,61 @@ class RProcessor {
return map
}

private Task createRFileTask(final File destFolder) {
def task = mProject.tasks.create(name: 'createRsFile' + mVariant.name) {
private TaskProvider createRFileTask(final File destFolder, final TaskProvider RClassTask) {
def task = mProject.tasks.register('createRsFile' + mVariant.name) {
finalizedBy(RClassTask)
outputs.dir(destFolder)
}
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)

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 08239c4

Please sign in to comment.