From 32414eb2e82c4ac42dccc12f1fae5a315839c3b9 Mon Sep 17 00:00:00 2001
From: kezong <379977@qq.com>
Date: Thu, 30 May 2019 10:56:53 +0800
Subject: [PATCH] support gradle plugin 3.3.0+ , gradle 5.0+
---
example/build.gradle | 4 +-
.../gradle/wrapper/gradle-wrapper.properties | 2 +-
.../src/main/res/layout/test_layout.xml | 10 +
source/build.gradle | 12 +-
.../com/kezong/fataar/ExplodedHelper.groovy | 21 +-
.../com/kezong/fataar/FatLibraryPlugin.groovy | 25 +-
.../com/kezong/fataar/FlavorArtifact.groovy | 107 +++++++++
.../groovy/com/kezong/fataar/LogUtil.groovy | 16 --
.../groovy/com/kezong/fataar/Utils.groovy | 49 ++++
.../com/kezong/fataar/VariantProcessor.groovy | 213 ++++++++++++------
10 files changed, 362 insertions(+), 97 deletions(-)
create mode 100644 example/lib-aar/src/main/res/layout/test_layout.xml
create mode 100644 source/src/main/groovy/com/kezong/fataar/FlavorArtifact.groovy
delete mode 100644 source/src/main/groovy/com/kezong/fataar/LogUtil.groovy
create mode 100644 source/src/main/groovy/com/kezong/fataar/Utils.groovy
diff --git a/example/build.gradle b/example/build.gradle
index 03a26373..2e56244a 100644
--- a/example/build.gradle
+++ b/example/build.gradle
@@ -8,8 +8,8 @@ buildscript {
maven { url "https://plugins.gradle.org/m2/" }
}
dependencies {
- classpath 'com.android.tools.build:gradle:3.2.1'
- classpath 'com.kezong:fat-aar:1.0.3'
+ classpath 'com.android.tools.build:gradle:3.4.1'
+ classpath 'com.kezong:fat-aar:1.1.6'
}
}
diff --git a/example/gradle/wrapper/gradle-wrapper.properties b/example/gradle/wrapper/gradle-wrapper.properties
index 6f5b76e5..be297714 100644
--- a/example/gradle/wrapper/gradle-wrapper.properties
+++ b/example/gradle/wrapper/gradle-wrapper.properties
@@ -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-4.6-all.zip
+distributionUrl=https\://services.gradle.org/distributions/gradle-5.1.1-all.zip
diff --git a/example/lib-aar/src/main/res/layout/test_layout.xml b/example/lib-aar/src/main/res/layout/test_layout.xml
new file mode 100644
index 00000000..8c1f1488
--- /dev/null
+++ b/example/lib-aar/src/main/res/layout/test_layout.xml
@@ -0,0 +1,10 @@
+
+
+
+
+
\ No newline at end of file
diff --git a/source/build.gradle b/source/build.gradle
index 8503937e..747324a1 100644
--- a/source/build.gradle
+++ b/source/build.gradle
@@ -22,14 +22,22 @@ dependencies {
implementation localGroovy()
implementation 'com.android.tools.build:gradle:3.2.1'
- compile 'com.squareup:javapoet:1.8.0'
}
+//afterEvaluate {
+// tasks.all { task ->
+// if (task.name == "generatePomFileForMavenPublication") {
+// task.setEnabled(false)
+// } else {
+// }
+// }
+//}
+
publish {
userOrg = 'kezong'
groupId = 'com.kezong'
artifactId = 'fat-aar'
- publishVersion = '1.0.1'
+ publishVersion = '1.1.6'
desc = 'The fat-aar works with the gradle plugin\'s version of the development is 3.0.1 and later.'
website = 'https://github.com/kezong/fat-aar-android'
}
diff --git a/source/src/main/groovy/com/kezong/fataar/ExplodedHelper.groovy b/source/src/main/groovy/com/kezong/fataar/ExplodedHelper.groovy
index 9ae922fd..ec9ced36 100644
--- a/source/src/main/groovy/com/kezong/fataar/ExplodedHelper.groovy
+++ b/source/src/main/groovy/com/kezong/fataar/ExplodedHelper.groovy
@@ -13,19 +13,19 @@ class ExplodedHelper {
static void processIntoJars(Project project,
Collection androidLibraries, Collection jarFiles,
File folderOut) {
- LogUtil.logInfo('Merge jars')
+ Utils.logInfo('Merge jars')
for (androidLibrary in androidLibraries) {
if (!androidLibrary.rootFolder.exists()) {
- LogUtil.logError('[warning]' + androidLibrary.rootFolder + ' not found!')
+ Utils.logError('[warning]' + androidLibrary.rootFolder + ' not found!')
continue
}
if (androidLibrary.localJars.isEmpty()) {
- LogUtil.logInfo("Not found jar file, Library:${androidLibrary.name}")
+ Utils.logInfo("Not found jar file, Library:${androidLibrary.name}")
} else {
- LogUtil.logInfo("Merge ${androidLibrary.name} jar file, Library:${androidLibrary.name}")
+ Utils.logInfo("Merge ${androidLibrary.name} jar file, Library:${androidLibrary.name}")
}
androidLibrary.localJars.each {
- LogUtil.logInfo(it.path)
+ Utils.logInfo(it.path)
}
project.copy {
from(androidLibrary.localJars)
@@ -34,10 +34,10 @@ class ExplodedHelper {
}
for (jarFile in jarFiles) {
if (!jarFile.exists()) {
- LogUtil.logError('[warning]' + jarFile + ' not found!')
+ Utils.logError('[warning]' + jarFile + ' not found!')
continue
}
- LogUtil.logInfo('copy jar from: ' + jarFile)
+ Utils.logInfo('copy jar from: ' + jarFile + " to " + folderOut.absolutePath)
project.copy {
from(jarFile)
into folderOut
@@ -48,15 +48,15 @@ class ExplodedHelper {
static void processIntoClasses(Project project,
Collection androidLibraries, Collection jarFiles,
File folderOut) {
- LogUtil.logInfo('Merge classes')
+ Utils.logInfo('Merge classes')
Collection allJarFiles = new ArrayList<>()
List rPathList = new ArrayList<>()
for (androidLibrary in androidLibraries) {
if (!androidLibrary.rootFolder.exists()) {
- LogUtil.logError('[warning]' + androidLibrary.rootFolder + ' not found!')
+ Utils.logError('[warning]' + androidLibrary.rootFolder + ' not found!')
continue
}
- LogUtil.logInfo('[androidLibrary]' + androidLibrary.getName())
+ Utils.logInfo('[androidLibrary]' + androidLibrary.getName())
allJarFiles.add(androidLibrary.classesJarFile)
String packageName = androidLibrary.getPackageName()
if (!Strings.isNullOrEmpty(packageName)) {
@@ -64,7 +64,6 @@ class ExplodedHelper {
}
}
for (jarFile in allJarFiles) {
- LogUtil.logInfo('copy classes from: ' + jarFile)
project.copy {
from project.zipTree(jarFile)
into folderOut
diff --git a/source/src/main/groovy/com/kezong/fataar/FatLibraryPlugin.groovy b/source/src/main/groovy/com/kezong/fataar/FatLibraryPlugin.groovy
index 8c4780dc..88511704 100644
--- a/source/src/main/groovy/com/kezong/fataar/FatLibraryPlugin.groovy
+++ b/source/src/main/groovy/com/kezong/fataar/FatLibraryPlugin.groovy
@@ -6,6 +6,7 @@ import org.gradle.api.Project
import org.gradle.api.ProjectConfigurationException
import org.gradle.api.artifacts.Configuration
import org.gradle.api.artifacts.ResolvedArtifact
+import org.gradle.api.artifacts.ResolvedDependency
/**
* plugin entry
@@ -24,6 +25,8 @@ class FatLibraryPlugin implements Plugin {
private Set artifacts
+ private Set unResolveArtifact
+
@Override
void apply(Project project) {
this.project = project
@@ -31,6 +34,7 @@ class FatLibraryPlugin implements Plugin {
createConfiguration()
project.afterEvaluate {
resolveArtifacts()
+ dealUnResolveArtifacts()
project.android.libraryVariants.all { variant ->
processVariant(variant)
}
@@ -55,7 +59,7 @@ class FatLibraryPlugin implements Plugin {
embedConf.resolvedConfiguration.resolvedArtifacts.each { artifact ->
// jar file wouldn't be here
if (ARTIFACT_TYPE_AAR == artifact.type || ARTIFACT_TYPE_JAR == artifact.type) {
- LogUtil.logInfo('[embed detected][' + artifact.type + ']' + artifact.moduleVersion.id)
+ Utils.logInfo('[embed detected][' + artifact.type + ']' + artifact.moduleVersion.id)
} else {
throw new ProjectConfigurationException('Only support embed aar and jar dependencies!', null)
}
@@ -67,6 +71,25 @@ class FatLibraryPlugin implements Plugin {
private void processVariant(LibraryVariant variant) {
def processor = new VariantProcessor(project, variant)
processor.addArtifacts(artifacts)
+ processor.addUnResolveArtifact(unResolveArtifact)
processor.processVariant()
}
+
+ private void dealUnResolveArtifacts() {
+ def dependencies = Collections.unmodifiableSet(embedConf.resolvedConfiguration.firstLevelModuleDependencies)
+ def dependencySet = new HashSet()
+ dependencies.each { dependency ->
+ boolean match = false
+ artifacts.each { artifact ->
+ if (dependency.moduleName == artifact.name) {
+ match = true
+ }
+ }
+ if (!match) {
+ Utils.logInfo('[unResolve dependency detected][' + dependency.name + ']')
+ dependencySet.add(dependency)
+ }
+ }
+ unResolveArtifact = Collections.unmodifiableSet(dependencySet)
+ }
}
diff --git a/source/src/main/groovy/com/kezong/fataar/FlavorArtifact.groovy b/source/src/main/groovy/com/kezong/fataar/FlavorArtifact.groovy
new file mode 100644
index 00000000..d8364d22
--- /dev/null
+++ b/source/src/main/groovy/com/kezong/fataar/FlavorArtifact.groovy
@@ -0,0 +1,107 @@
+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.artifacts.ModuleVersionIdentifier
+import org.gradle.api.artifacts.ResolvedDependency
+import org.gradle.api.artifacts.component.ComponentArtifactIdentifier
+import org.gradle.api.artifacts.component.ComponentIdentifier
+import org.gradle.api.internal.artifacts.DefaultModuleVersionIdentifier
+import org.gradle.api.internal.artifacts.DefaultResolvedArtifact
+import org.gradle.api.tasks.TaskDependency
+import org.gradle.internal.Factory
+import org.gradle.internal.component.model.DefaultIvyArtifactName
+
+import javax.annotation.Nullable
+
+/**
+ * FlavorArtifact
+ * @author yangchao on 2019/4/25.
+ */
+class FlavorArtifact {
+
+ static DefaultResolvedArtifact createFlavorArtifact(Project project, LibraryVariant variant, ResolvedDependency unResolvedArtifact, String version) {
+ ModuleVersionIdentifier identifier = createModuleVersionIdentifier(unResolvedArtifact)
+ DefaultIvyArtifactName artifactName = createArtifactName(unResolvedArtifact)
+ Project artifactProject = getArtifactProject(project, unResolvedArtifact)
+ File artifactFile = createArtifactFile(artifactProject, variant, unResolvedArtifact, version)
+ Factory fileFactory = new Factory() {
+ @Override
+ File create() {
+ return artifactFile
+ }
+ }
+ TaskDependency taskDependency = createTaskDependency(artifactProject, variant)
+ ComponentArtifactIdentifier artifactIdentifier = createComponentIdentifier(artifactFile)
+
+ return new DefaultResolvedArtifact(identifier, artifactName, artifactIdentifier, taskDependency, fileFactory)
+ }
+
+ private static ModuleVersionIdentifier createModuleVersionIdentifier(ResolvedDependency unResolvedArtifact) {
+ return new DefaultModuleVersionIdentifier(
+ unResolvedArtifact.getModuleGroup(),
+ unResolvedArtifact.getModuleName(),
+ unResolvedArtifact.getModuleVersion()
+ )
+ }
+
+ private static DefaultIvyArtifactName createArtifactName(ResolvedDependency unResolvedArtifact) {
+ return new DefaultIvyArtifactName(unResolvedArtifact.getModuleName(), "aar", "")
+ }
+
+ private static ComponentArtifactIdentifier createComponentIdentifier(final File artifactFile) {
+ return new ComponentArtifactIdentifier() {
+ @Override
+ ComponentIdentifier getComponentIdentifier() {
+ return null
+ }
+
+ @Override
+ String getDisplayName() {
+ return artifactFile.name
+ }
+ }
+ }
+
+ private static Project getArtifactProject(Project project, ResolvedDependency unResolvedArtifact) {
+ for (Project p : project.getRootProject().getAllprojects()) {
+ if (unResolvedArtifact.moduleName == p.name) {
+ return p
+ }
+ }
+ return null
+ }
+
+ private static File createArtifactFile(Project project, LibraryVariant variant, ResolvedDependency unResolvedArtifact, String version) {
+ def buildPath = project.buildDir.path
+ def outputName
+ if (Utils.compareVersion(project.gradle.gradleVersion, "5.1.0") >= 0 && Utils.compareVersion(version, "3.4") < 0) {
+ outputName = "$buildPath/outputs/aar/${unResolvedArtifact.moduleName}.aar"
+ } else {
+ outputName = "$buildPath/outputs/aar/$unResolvedArtifact.moduleName-$variant.flavorName-${variant.buildType.name}.aar"
+ }
+ 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}!")
+ }
+
+ return new TaskDependency() {
+ @Override
+ Set extends Task> getDependencies(@Nullable Task task) {
+ def set = new HashSet()
+ set.add(bundleTask)
+ return set
+ }
+ }
+ }
+}
diff --git a/source/src/main/groovy/com/kezong/fataar/LogUtil.groovy b/source/src/main/groovy/com/kezong/fataar/LogUtil.groovy
deleted file mode 100644
index 65a457ea..00000000
--- a/source/src/main/groovy/com/kezong/fataar/LogUtil.groovy
+++ /dev/null
@@ -1,16 +0,0 @@
-package com.kezong.fataar
-
-/**
- * LogUtil
- * @author kezong @since 2018-12-10 17:28
- */
-class LogUtil {
-
- def static logError(def msg) {
- println("【Fat-aar-ERROR】${msg}")
- }
-
- def static logInfo(def msg) {
- println("【Fat-aar-INFO】${msg}")
- }
-}
\ No newline at end of file
diff --git a/source/src/main/groovy/com/kezong/fataar/Utils.groovy b/source/src/main/groovy/com/kezong/fataar/Utils.groovy
new file mode 100644
index 00000000..36f6f7b5
--- /dev/null
+++ b/source/src/main/groovy/com/kezong/fataar/Utils.groovy
@@ -0,0 +1,49 @@
+package com.kezong.fataar
+
+/**
+ * Utils
+ * @author kezong @since 2018-12-10 17:28
+ */
+class Utils {
+
+ def static logError(def msg) {
+ println("【Fat-aar-ERROR】${msg}")
+ }
+
+ def static logInfo(def msg) {
+ println("【Fat-aar-INFO】${msg}")
+ }
+
+ static int compareVersion(String v1, String v2) {
+ if (v1.equals(v2)) {
+ return 0
+ }
+ String[] version1Array = v1.split("[._]")
+ String[] version2Array = v2.split("[._]")
+ int index = 0
+ int minLen = Math.min(version1Array.length, version2Array.length)
+ long diff = 0
+
+ while (index < minLen
+ && (diff = Long.parseLong(version1Array[index])
+ - Long.parseLong(version2Array[index])) == 0) {
+ index++
+ }
+ if (diff == 0) {
+ for (int i = index; i < version1Array.length; i++) {
+ if (Long.parseLong(version1Array[i]) > 0) {
+ return 1
+ }
+ }
+
+ for (int i = index; i < version2Array.length; i++) {
+ if (Long.parseLong(version2Array[i]) > 0) {
+ return -1
+ }
+ }
+ return 0
+ } else {
+ return diff > 0 ? 1 : -1
+ }
+ }
+}
\ No newline at end of file
diff --git a/source/src/main/groovy/com/kezong/fataar/VariantProcessor.groovy b/source/src/main/groovy/com/kezong/fataar/VariantProcessor.groovy
index 6805f3c6..058ab2a9 100644
--- a/source/src/main/groovy/com/kezong/fataar/VariantProcessor.groovy
+++ b/source/src/main/groovy/com/kezong/fataar/VariantProcessor.groovy
@@ -6,6 +6,7 @@ import org.gradle.api.Project
import org.gradle.api.Task
import org.gradle.api.artifacts.Dependency
import org.gradle.api.artifacts.ResolvedArtifact
+import org.gradle.api.artifacts.ResolvedDependency
import org.gradle.api.file.ConfigurableFileCollection
import org.gradle.api.internal.artifacts.DefaultResolvedArtifact
import org.gradle.api.tasks.Copy
@@ -16,7 +17,7 @@ import org.gradle.jvm.tasks.Jar
/**
* Processor for variant
* Created by Vigi on 2017/2/24.
- * Modified by kezong on 2018/12/18
+ * Modified by kezong on 2019/05/29
*/
class VariantProcessor {
@@ -32,9 +33,19 @@ class VariantProcessor {
private Collection mExplodeTasks = new ArrayList<>()
+ private String mGradlePluginVersion
+
+ private String aarOutputFilePath
+
VariantProcessor(Project project, LibraryVariant variant) {
mProject = project
mVariant = variant
+ // gradle version
+ mProject.parent.buildscript.getConfigurations().getByName("classpath").getDependencies().each { Dependency dep ->
+ if (dep.name == "gradle") {
+ mGradlePluginVersion = dep.version
+ }
+ }
}
void addArtifacts(Set resolvedArtifacts) {
@@ -45,6 +56,15 @@ class VariantProcessor {
mAndroidArchiveLibraries.add(library)
}
+ void addUnResolveArtifact(Set dependencies) {
+ if (dependencies != null) {
+ dependencies.each {
+ def artifact = FlavorArtifact.createFlavorArtifact(mProject, mVariant, it, mGradlePluginVersion)
+ mResolvedArtifacts.add(artifact)
+ }
+ }
+ }
+
void addJarFile(File jar) {
mJarFiles.add(jar)
}
@@ -65,7 +85,7 @@ class VariantProcessor {
throw new RuntimeException("Can not find task ${taskPath}!")
}
processArtifacts(bundleTask)
- processClassesAndJars()
+ processClassesAndJars(bundleTask)
if (mAndroidArchiveLibraries.isEmpty()) {
return
@@ -75,26 +95,25 @@ class VariantProcessor {
processAssets()
processJniLibs()
processProguardTxt(prepareTask)
- processRFile()
+ processRFile(bundleTask)
}
/**
* exploded artifact files
*/
private void processArtifacts(Task bundleTask) {
- for (DefaultResolvedArtifact artifact in mResolvedArtifacts) {
+ for (final DefaultResolvedArtifact artifact in mResolvedArtifacts) {
if (FatLibraryPlugin.ARTIFACT_TYPE_JAR == artifact.type) {
addJarFile(artifact.file)
} else if (FatLibraryPlugin.ARTIFACT_TYPE_AAR == artifact.type) {
AndroidArchiveLibrary archiveLibrary = new AndroidArchiveLibrary(mProject, artifact)
addAndroidArchiveLibrary(archiveLibrary)
- Set buildDependencies = artifact.getBuildDependencies().getDependencies()
+ Set buildDependencies = artifact.buildDependencies.getDependencies()
archiveLibrary.getExploadedRootDir().deleteDir()
- def zipFolder = archiveLibrary.getRootFolder()
+ final def zipFolder = archiveLibrary.getRootFolder()
zipFolder.mkdirs()
if (buildDependencies.size() == 0) {
mProject.copy {
- LogUtil.logInfo("explode $artifact.name")
from mProject.zipTree(artifact.file.absolutePath)
into zipFolder
}
@@ -104,10 +123,6 @@ class VariantProcessor {
from mProject.zipTree(artifact.file.absolutePath)
into zipFolder
}
- final String artifactName = artifact.name
- explodeTask.doFirst {
- LogUtil.logInfo("explode $artifactName")
- }
explodeTask.dependsOn(buildDependencies.first())
explodeTask.shouldRunAfter(buildDependencies.first())
Task javacTask = mVariant.getJavaCompiler()
@@ -138,7 +153,12 @@ class VariantProcessor {
throw new RuntimeException("Can not find class ${className}!")
}
Task processManifestTask = mVariant.getOutputs().first().getProcessManifest()
- File manifestOutputBackup = mProject.file(processManifestTask.getManifestOutputDirectory().absolutePath + '/AndroidManifest.xml')
+ File manifestOutputBackup
+ if (mGradlePluginVersion != null && Utils.compareVersion(mGradlePluginVersion, "3.3.0") >= 0) {
+ manifestOutputBackup = mProject.file("${mProject.buildDir.path}/intermediates/library_manifest/${mVariant.name}/AndroidManifest.xml")
+ } else {
+ manifestOutputBackup = mProject.file(processManifestTask.getManifestOutputDirectory().absolutePath + '/AndroidManifest.xml')
+ }
InvokeManifestMerger manifestsMergeTask = mProject.tasks.create('merge' + mVariant.name.capitalize() + 'Manifest', invokeManifestTaskClazz)
manifestsMergeTask.setVariantName(mVariant.name)
@@ -167,41 +187,64 @@ class VariantProcessor {
processManifestTask.finalizedBy manifestsMergeTask
}
+ private Task handleClassesMergeTask() {
+ final Task task = mProject.tasks.create(name: 'mergeClasses'
+ + mVariant.name.capitalize())
+ task.doFirst {
+ def dustDir = getClassPathDirFiles().first()
+ ExplodedHelper.processIntoClasses(mProject, mAndroidArchiveLibraries, mJarFiles, dustDir)
+ }
+ return task
+ }
+
+ private Task handleJarMergeTask() {
+ final Task task = mProject.tasks.create(name: 'mergeJars'
+ + mVariant.name.capitalize())
+ task.doFirst {
+ ExplodedHelper.processIntoJars(mProject, mAndroidArchiveLibraries, mJarFiles, getLibsDirFile())
+ }
+ return task
+ }
+
/**
* merge classes and jars
*/
- private void processClassesAndJars() {
+ private void processClassesAndJars(Task bundleTask) {
if (mVariant.getBuildType().isMinifyEnabled()) {
//merge proguard file
for (archiveLibrary in mAndroidArchiveLibraries) {
List thirdProguardFiles = archiveLibrary.proguardRules
for (File file : thirdProguardFiles) {
if (file.exists()) {
- LogUtil.logInfo('add proguard file: ' + file.absolutePath)
+ Utils.logInfo('add proguard file: ' + file.absolutePath)
mProject.android.getDefaultConfig().proguardFile(file)
}
}
}
}
- // compile to a unbroken classes.jar
- Task javacTask = mVariant.getJavaCompiler()
- if (javacTask == null) {
- throw new RuntimeException("Can not find java compiler task")
- }
- javacTask.doLast {
- ExplodedHelper.processIntoClasses(mProject, mAndroidArchiveLibraries, mJarFiles, getClassPathDirFiles().first())
- }
-
- // assemble task
String taskPath = 'transformClassesAndResourcesWithSyncLibJarsFor' + mVariant.name.capitalize()
Task syncLibTask = mProject.tasks.findByPath(taskPath)
if (syncLibTask == null) {
throw new RuntimeException("Can not find task ${taskPath}!")
}
- syncLibTask.doLast {
- ExplodedHelper.processIntoJars(mProject, mAndroidArchiveLibraries, mJarFiles, getLibsDirFile())
+
+ Task mergeClasses = handleClassesMergeTask()
+ syncLibTask.dependsOn(mergeClasses)
+ mExplodeTasks.each { it ->
+ mergeClasses.dependsOn it
}
+
+ Task mergeJars = handleJarMergeTask()
+ mergeJars.shouldRunAfter(syncLibTask)
+ bundleTask.dependsOn(mergeJars)
+ mExplodeTasks.each { it ->
+ mergeJars.dependsOn it
+ }
+
+ Task javacTask = mVariant.getJavaCompiler()
+ mergeClasses.dependsOn(javacTask)
+ mergeJars.dependsOn(javacTask)
}
/**
@@ -221,7 +264,7 @@ class VariantProcessor {
}
resourceGenTask.doFirst {
for (archiveLibrary in mAndroidArchiveLibraries) {
- LogUtil.logInfo("Merge resource,Library res:${archiveLibrary.resFolder}")
+ Utils.logInfo("Merge resource,Library res:${archiveLibrary.resFolder}")
mProject.android.sourceSets."main".res.srcDir(archiveLibrary.resFolder)
}
}
@@ -241,13 +284,13 @@ class VariantProcessor {
if (assetsTask == null) {
throw new RuntimeException("Can not find task in variant.getMergeAssets()!")
}
- for (archiveLibrary in mAndroidArchiveLibraries) {
- assetsTask.getInputs().dir(archiveLibrary.assetsFolder)
- }
+
assetsTask.doFirst {
for (archiveLibrary in mAndroidArchiveLibraries) {
- // the source set here should be main or variant?
- mProject.android.sourceSets."main".assets.srcDir(archiveLibrary.assetsFolder)
+ if (archiveLibrary.assetsFolder != null && archiveLibrary.assetsFolder.size() > 0) {
+ // the source set here should be main or variant?
+ mProject.android.sourceSets."main".assets.srcDir(archiveLibrary.assetsFolder)
+ }
}
}
@@ -265,13 +308,13 @@ class VariantProcessor {
if (mergeJniLibsTask == null) {
throw new RuntimeException("Can not find task ${taskPath}!")
}
- for (archiveLibrary in mAndroidArchiveLibraries) {
- mergeJniLibsTask.getInputs().dir(archiveLibrary.jniFolder)
- }
+
mergeJniLibsTask.doFirst {
for (archiveLibrary in mAndroidArchiveLibraries) {
- // the source set here should be main or variant?
- mProject.android.sourceSets."main".jniLibs.srcDir(archiveLibrary.jniFolder)
+ if (archiveLibrary.jniFolder != null && archiveLibrary.jniFolder.size() > 0) {
+ // the source set here should be main or variant?
+ mProject.android.sourceSets."main".jniLibs.srcDir(archiveLibrary.jniFolder)
+ }
}
}
@@ -293,7 +336,7 @@ class VariantProcessor {
List thirdProguardFiles = archiveLibrary.proguardRules
for (File file : thirdProguardFiles) {
if (file.exists()) {
- LogUtil.logInfo('add proguard file: ' + file.absolutePath)
+ Utils.logInfo('add proguard file: ' + file.absolutePath)
mergeFileTask.getInputs().file(file)
}
}
@@ -304,7 +347,7 @@ class VariantProcessor {
List thirdProguardFiles = archiveLibrary.proguardRules
for (File file : thirdProguardFiles) {
if (file.exists()) {
- LogUtil.logInfo('add proguard file: ' + file.absolutePath)
+ Utils.logInfo('add proguard file: ' + file.absolutePath)
proguardFiles.add(file)
}
}
@@ -313,41 +356,81 @@ class VariantProcessor {
mergeFileTask.dependsOn prepareTask
}
- private def processRFile() {
+ def deleteEmptyDir = { file ->
+ file.listFiles().each { x ->
+ if (x.isDirectory()) {
+ if (x.listFiles().size() == 0) {
+ x.delete()
+ } else {
+ deleteEmptyDir(x)
+ if (x.listFiles().size() == 0) {
+ x.delete()
+ }
+ }
+ }
+ }
+ }
+
+ private def processRFile(Task bundleTask) {
// R.java dir
File rFolder = mProject.file("${mProject.getBuildDir()}/intermediates/exploded-aar/r")
// R.class compile dir
File rClassFolder = mProject.file("${mProject.getBuildDir()}/intermediates/exploded-aar/r-class")
// R.jar dir
- File libFolder = mProject.file("${mProject.getBuildDir()}/outputs/aar-R/${mVariant.dirName}/libs")
- libFolder.getParentFile().delete()
- libFolder.mkdirs()
- // aar output file
- File outputFile
+ final File libFolder = mProject.file("${mProject.getBuildDir()}/outputs/aar-R/${mVariant.dirName}/libs")
// aar zip file
File outputDir = libFolder.getParentFile()
// aar output dir
File aarDir = mProject.file("${mProject.getBuildDir()}/outputs/aar/")
mVariant.outputs.all { output ->
- outputFile = output.outputFile
+ aarOutputFilePath = output.outputFile.absolutePath
+ }
+
+ if (bundleTask != null) {
+ bundleTask.doFirst {
+ File f = new File(aarOutputFilePath)
+ if (f.exists()) {
+ f.delete()
+ }
+ libFolder.getParentFile().deleteDir()
+ libFolder.mkdirs()
+ }
}
def RFileTask = createRFileTask(rFolder)
def RClassTask = createRClassTask(rFolder, rClassFolder)
def RJarTask = createRJarTask(rClassFolder, libFolder)
- def bundleAar = createBundleAarTask(outputDir, aarDir, outputFile)
+ def bundleAar = createBundleAarTask(outputDir, aarDir, aarOutputFilePath)
+ if (mGradlePluginVersion != null && Utils.compareVersion(mGradlePluginVersion, "3.3.0") >= 0) {
+ RClassTask.doFirst {
+ mProject.copy {
+ from mProject.zipTree(getRClassPath().first().absolutePath + "/R.jar")
+ into getRClassPath().first().absolutePath
+ }
+ }
+ }
+
bundleAar.doFirst {
- LogUtil.logInfo("Assemble final aar, from:$outputDir")
+ Utils.logInfo("Assemble final aar, from:$outputDir.absolutePath")
mProject.copy {
- from mProject.zipTree(outputFile.absolutePath)
+ from mProject.zipTree(aarOutputFilePath)
into outputDir
}
+ deleteEmptyDir(outputDir)
}
bundleAar.doLast {
- LogUtil.logInfo("Assemble final aar, target:$outputFile")
+ Utils.logInfo("Assemble final aar, target:$aarOutputFilePath")
}
Task assembleTask = mProject.tasks.findByPath("assemble${mVariant.name.capitalize()}")
+ assembleTask.doLast {
+ // support gradle 5.1 && gradle plugin 3.4 before, the outputName is changed
+ File file = new File(aarOutputFilePath)
+ if (!file.exists()) {
+ aarOutputFilePath = aarDir.absolutePath + "/" + mProject.name + ".aar"
+ bundleAar.archiveName = new File(aarOutputFilePath).name
+ }
+ }
assembleTask.finalizedBy(RFileTask)
RFileTask.finalizedBy(RClassTask)
RClassTask.finalizedBy(RJarTask)
@@ -392,7 +475,7 @@ class VariantProcessor {
def task = mProject.tasks.create(name: 'createRsFile' + mVariant.name)
task.doLast {
mAndroidArchiveLibraries.each {
- LogUtil.logInfo("Generate R File, Library:${it.name}")
+ Utils.logInfo("Generate R File, Library:${it.name}")
createRFile(it, destFolder)
}
}
@@ -403,7 +486,7 @@ class VariantProcessor {
private Task createRClassTask(def sourceDir, def destinationDir) {
mProject.mkdir(destinationDir)
- def classpath = getClassPathDirFiles()
+ def classpath = getRClassPath()
String taskName = "compileRs${mVariant.name.capitalize()}"
Task task = mProject.getTasks().create(taskName, JavaCompile.class, {
it.source = sourceDir.path
@@ -413,7 +496,7 @@ class VariantProcessor {
it.destinationDir destinationDir
})
task.doFirst {
- LogUtil.logInfo("Compile R.class, Dir:${sourceDir.path}")
+ Utils.logInfo("Compile R.class, Dir:${sourceDir.path}")
}
return task
}
@@ -426,32 +509,25 @@ class VariantProcessor {
it.destinationDir desFile
})
task.doFirst {
- LogUtil.logInfo("Generate R.jar, Dir:$fromDir")
+ Utils.logInfo("Generate R.jar, Dir:$fromDir")
}
return task
}
- private Task createBundleAarTask(File from, File destDir, File outputFile) {
+ private Task createBundleAarTask(File from, File destDir, String filePath) {
String taskName = "assembleFinalAar${mVariant.name.capitalize()}"
Task task = mProject.getTasks().create(taskName, Zip.class, {
it.from from
it.include "**"
- it.archiveName = outputFile.name
+ it.archiveName = new File(filePath).name
it.destinationDir(destDir)
})
return task
}
private ConfigurableFileCollection getClassPathDirFiles() {
- def gradleVersion
- mProject.parent.buildscript.getConfigurations().getByName("classpath").getDependencies().each { Dependency dep ->
- if (dep.name == "gradle") {
- gradleVersion = dep.version
- }
- }
-
ConfigurableFileCollection classpath
- if (gradleVersion != null && gradleVersion.contains("3.2")) { // Versions 3.2.x
+ if (mGradlePluginVersion != null && Utils.compareVersion(mGradlePluginVersion, "3.2.0") >= 0) { // >= Versions 3.2.X
classpath = mProject.files("${mProject.buildDir.path}/intermediates/" +
"javac/${mVariant.name}/compile${mVariant.name.capitalize()}JavaWithJavac/classes")
} else { // Versions 3.0.x and 3.1.x
@@ -460,6 +536,15 @@ class VariantProcessor {
return classpath
}
+ private ConfigurableFileCollection getRClassPath() {
+ if (mGradlePluginVersion != null && Utils.compareVersion(mGradlePluginVersion, "3.3.0") >= 0) {
+ return mProject.files("${mProject.buildDir.path}/intermediates/" + "compile_only_not_namespaced_r_class_jar/"
+ + "${mVariant.name}/generate${mVariant.name.capitalize()}RFile")
+ } else {
+ return getClassPathDirFiles()
+ }
+ }
+
private File getLibsDirFile() {
return mProject.file(mProject.buildDir.path + '/intermediates/packaged-classes/' + mVariant.dirName + "/libs")
}