Skip to content

Commit

Permalink
Merge pull request #199 from puboe/incremental-build
Browse files Browse the repository at this point in the history
Incremental build
  • Loading branch information
kezong authored Sep 25, 2020
2 parents 964e47e + 33419d8 commit 7f6cc03
Show file tree
Hide file tree
Showing 7 changed files with 164 additions and 143 deletions.
2 changes: 1 addition & 1 deletion source/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ publish {
userOrg = 'kezong'
groupId = 'com.kezong'
artifactId = 'fat-aar'
publishVersion = '1.2.16'
publishVersion = '1.2.17'
desc = 'Gradle plugin for merging dependencies applies to gradle plugin version 3.+'
website = 'https://github.com/kezong/fat-aar-android'
}
Expand Down
2 changes: 1 addition & 1 deletion source/src/main/groovy/com/kezong/fataar/Constants.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ class Constants {

public static final String RE_BUNDLE_FOLDER = "aar_rebundle"

public static final String INTERMEDIATES_TEMP_FOLDER = "fat-r"
public static final String INTERMEDIATES_TEMP_FOLDER = "fat-aar"
}
42 changes: 20 additions & 22 deletions source/src/main/groovy/com/kezong/fataar/ExplodedHelper.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,33 @@ import org.gradle.api.Project
class ExplodedHelper {

static void processLibsIntoLibs(Project project,
Collection<AndroidArchiveLibrary> androidLibraries, Collection<File> jarFiles,
Collection<AndroidArchiveLibrary> androidLibraries,
Collection<File> jarFiles,
File folderOut) {
for (androidLibrary in androidLibraries) {
if (!androidLibrary.rootFolder.exists()) {
Utils.logInfo('[warning]' + androidLibrary.rootFolder + ' not found!')
continue
}
if (androidLibrary.localJars.isEmpty()) {
Utils.logInfo("Not found jar file, Library:${androidLibrary.name}")
Utils.logInfo("Not found jar file, Library: ${androidLibrary.name}")
} else {
Utils.logInfo("Merge ${androidLibrary.name} jar file, Library:${androidLibrary.name}")
}
androidLibrary.localJars.each {
Utils.logInfo(it.path)
}
project.copy {
from(androidLibrary.localJars)
into folderOut
Utils.logInfo("Merge ${androidLibrary.name} local jar files")
project.copy {
from(androidLibrary.localJars)
into(folderOut)
}
}
}
for (jarFile in jarFiles) {
if (!jarFile.exists()) {
if (jarFile.exists()) {
Utils.logInfo("Copy jar from: $jarFile to $folderOut.absolutePath")
project.copy {
from(jarFile)
into(folderOut)
}
} else {
Utils.logInfo('[warning]' + jarFile + ' not found!')
continue
}
Utils.logInfo('copy jar from: ' + jarFile + " to " + folderOut.absolutePath)
project.copy {
from(jarFile)
into folderOut
}
}
}
Expand All @@ -65,8 +63,9 @@ class ExplodedHelper {
}

static void processLibsIntoClasses(Project project,
Collection<AndroidArchiveLibrary> androidLibraries, Collection<File> jarFiles,
File folderOut) {
Collection<AndroidArchiveLibrary> androidLibraries,
Collection<File> jarFiles,
File folderOut) {
Utils.logInfo('Merge Libs')
Collection<File> allJarFiles = new ArrayList<>()
for (androidLibrary in androidLibraries) {
Expand All @@ -78,10 +77,9 @@ class ExplodedHelper {
allJarFiles.addAll(androidLibrary.localJars)
}
for (jarFile in jarFiles) {
if (!jarFile.exists()) {
continue
if (jarFile.exists()) {
allJarFiles.add(jarFile)
}
allJarFiles.add(jarFile)
}
for (jarFile in allJarFiles) {
project.copy {
Expand Down
77 changes: 40 additions & 37 deletions source/src/main/groovy/com/kezong/fataar/RProcessor.groovy
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.kezong.fataar

import com.android.build.gradle.api.LibraryVariant
import com.android.build.gradle.tasks.BundleAar
import org.gradle.api.Project
import org.gradle.api.Task
import org.gradle.api.tasks.PathSensitivity
import org.gradle.api.tasks.TaskProvider
import org.gradle.api.tasks.bundling.Zip
import org.gradle.api.tasks.compile.JavaCompile
Expand All @@ -21,9 +23,9 @@ class RProcessor {
private final File mClassDir
private final File mJarDir
private final File mAarUnZipDir
private final File mAarOutputDir
private final File mIntermediateAarFile
private final File mAarOutputFile
private final String mGradlePluginVersion
private String mAarOutputPath
private VersionAdapter mVersionAdapter
private final Collection<AndroidArchiveLibrary> mLibraries

Expand All @@ -33,61 +35,58 @@ class RProcessor {
mLibraries = libraries
mGradlePluginVersion = version
mVersionAdapter = new VersionAdapter(project, variant, version)
// Aar output file
mAarOutputFile = mVersionAdapter.getOutputFile()
// R.java dir
mJavaDir = mProject.file("${mProject.getBuildDir()}/intermediates/${Constants.INTERMEDIATES_TEMP_FOLDER}/r/${mVariant.dirName}")
mJavaDir = mProject.file("${mProject.getBuildDir()}/intermediates/${Constants.INTERMEDIATES_TEMP_FOLDER}/r/${mVariant.name}")
// R.class compile dir
mClassDir = mProject.file("${mProject.getBuildDir()}/intermediates/${Constants.INTERMEDIATES_TEMP_FOLDER}/r-class/${mVariant.dirName}")
mClassDir = mProject.file("${mProject.getBuildDir()}/intermediates/${Constants.INTERMEDIATES_TEMP_FOLDER}/r-class/${mVariant.name}")
// Intermediate aar dir
mIntermediateAarFile = mProject.file("${mProject.getBuildDir()}/intermediates/${Constants.INTERMEDIATES_TEMP_FOLDER}/aar/${mVariant.name}/${mAarOutputFile.name}")
// R.jar dir
mJarDir = mProject.file("${mProject.getBuildDir()}/outputs/${Constants.RE_BUNDLE_FOLDER}/${mVariant.dirName}/libs")
// aar zip file
mAarUnZipDir = mJarDir.getParentFile()
// aar output dir
mAarOutputDir = mProject.file("${mProject.getBuildDir()}/outputs/aar/")

mAarOutputPath = mVersionAdapter.getOutputPath()
mJarDir = mProject.file("${mProject.getBuildDir()}/outputs/${Constants.RE_BUNDLE_FOLDER}/${mVariant.name}/libs")
// Aar unzip dir
mAarUnZipDir = mJarDir.parentFile
}

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

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

bundleTask.configure {
bundleTask.configure { BundleAar it ->
finalizedBy(RFileTask)

// Redirect bundle output to intermediate directory to avoid breaking incremental builds
// because of bundle and reBundle tasks having the same output
if (Utils.compareVersion(mProject.gradle.gradleVersion, "6.0.1") >= 0) {
it.getDestinationDirectory().set(mIntermediateAarFile.parentFile)
} else {
it.destinationDir = mIntermediateAarFile.parentFile
}

doFirst {
File f = new File(mAarOutputPath)
if (f.exists()) {
f.delete()
if (mIntermediateAarFile.exists()) {
mIntermediateAarFile.delete()
}
// Delete previously unzipped data.
mAarUnZipDir.deleteDir()
mJarDir.mkdirs()
}

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
}
mProject.copy {
from mProject.zipTree(mIntermediateAarFile)
into mAarUnZipDir
}
deleteEmptyDir(mAarUnZipDir)
}
}
}
Expand Down Expand Up @@ -168,9 +167,13 @@ class RProcessor {
}

private TaskProvider createRFileTask(final File destFolder, final TaskProvider RClassTask) {
def task = mProject.tasks.register('createRsFile' + mVariant.name) {
def task = mProject.tasks.register("createRsFile${mVariant.name}") {
finalizedBy(RClassTask)

inputs.files(mLibraries.stream().map { it.symbolFile }.collect())
.withPathSensitivity(PathSensitivity.RELATIVE)
outputs.dir(destFolder)

doLast {
if (destFolder.exists()) {
destFolder.deleteDir()
Expand Down Expand Up @@ -238,16 +241,16 @@ class RProcessor {
return task
}

private TaskProvider createBundleAarTask(final File from, final File destDir, final String filePath) {
private TaskProvider createBundleAarTask(final File from, final File destDir, final String aarFileName) {
String taskName = "reBundleAar${mVariant.name.capitalize()}"
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.getArchiveFileName().set(aarFileName)
it.getDestinationDirectory().set(destDir)
} else {
it.archiveName = new File(filePath).name
it.archiveName = aarFileName
it.destinationDir = destDir
}
}
Expand Down
Loading

0 comments on commit 7f6cc03

Please sign in to comment.