Skip to content

Commit

Permalink
support gradle plugin 3.3.0+ , gradle 5.0+
Browse files Browse the repository at this point in the history
  • Loading branch information
kezong committed May 30, 2019
1 parent 55618e5 commit 32414eb
Show file tree
Hide file tree
Showing 10 changed files with 362 additions and 97 deletions.
4 changes: 2 additions & 2 deletions example/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
}

Expand Down
2 changes: 1 addition & 1 deletion example/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-4.6-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-5.1.1-all.zip
10 changes: 10 additions & 0 deletions example/lib-aar/src/main/res/layout/test_layout.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright (C) 2015 Baidu, Inc. All Rights Reserved.
-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">

</LinearLayout>
12 changes: 10 additions & 2 deletions source/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
Expand Down
21 changes: 10 additions & 11 deletions source/src/main/groovy/com/kezong/fataar/ExplodedHelper.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,19 @@ class ExplodedHelper {
static void processIntoJars(Project project,
Collection<AndroidArchiveLibrary> androidLibraries, Collection<File> 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)
Expand All @@ -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
Expand All @@ -48,23 +48,22 @@ class ExplodedHelper {
static void processIntoClasses(Project project,
Collection<AndroidArchiveLibrary> androidLibraries, Collection<File> jarFiles,
File folderOut) {
LogUtil.logInfo('Merge classes')
Utils.logInfo('Merge classes')
Collection<File> allJarFiles = new ArrayList<>()
List<String> 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)) {
rPathList.add(androidLibrary.getPackageName())
}
}
for (jarFile in allJarFiles) {
LogUtil.logInfo('copy classes from: ' + jarFile)
project.copy {
from project.zipTree(jarFile)
into folderOut
Expand Down
25 changes: 24 additions & 1 deletion source/src/main/groovy/com/kezong/fataar/FatLibraryPlugin.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -24,13 +25,16 @@ class FatLibraryPlugin implements Plugin<Project> {

private Set<ResolvedArtifact> artifacts

private Set<ResolvedDependency> unResolveArtifact

@Override
void apply(Project project) {
this.project = project
checkAndroidPlugin()
createConfiguration()
project.afterEvaluate {
resolveArtifacts()
dealUnResolveArtifacts()
project.android.libraryVariants.all { variant ->
processVariant(variant)
}
Expand All @@ -55,7 +59,7 @@ class FatLibraryPlugin implements Plugin<Project> {
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)
}
Expand All @@ -67,6 +71,25 @@ class FatLibraryPlugin implements Plugin<Project> {
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)
}
}
107 changes: 107 additions & 0 deletions source/src/main/groovy/com/kezong/fataar/FlavorArtifact.groovy
Original file line number Diff line number Diff line change
@@ -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<File> fileFactory = new Factory<File>() {
@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
}
}
}
}
16 changes: 0 additions & 16 deletions source/src/main/groovy/com/kezong/fataar/LogUtil.groovy

This file was deleted.

49 changes: 49 additions & 0 deletions source/src/main/groovy/com/kezong/fataar/Utils.groovy
Original file line number Diff line number Diff line change
@@ -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
}
}
}
Loading

0 comments on commit 32414eb

Please sign in to comment.