Skip to content

Commit

Permalink
Support android gradle build tools 2.3.3 and lower with gradle 4.x
Browse files Browse the repository at this point in the history
  • Loading branch information
warnyul committed Sep 26, 2017
1 parent 00c2e2a commit 08997c3
Show file tree
Hide file tree
Showing 9 changed files with 289 additions and 283 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,16 @@ class AndroidMavenPublishPlugin implements Plugin<Project> {
if (isAndroidLibraryPluginApplied(project)) {
def android = project.extensions.getByType(LibraryExtension)

def configurations = project.configurations

android.libraryVariants.all { v ->
project.components.add(new AndroidVariantLibrary(project, v))
def publishConfig = new VariantPublishConfiguration(v)
project.components.add(new AndroidVariantLibrary(configurations, publishConfig))
}

// For default publish config
project.components.add(new AndroidLibrary(project))
def defaultPublishConfig = new DefaultPublishConfiguration(project)
project.components.add(new AndroidVariantLibrary(configurations, defaultPublishConfig))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,48 +16,49 @@

package digital.wup.android_maven_publish

import com.android.build.gradle.api.BaseVariant
import org.gradle.api.Project
import org.gradle.api.artifacts.ConfigurationContainer
import org.gradle.api.artifacts.DependencySet
import org.gradle.api.artifacts.ModuleDependency
import org.gradle.api.artifacts.PublishArtifact
import org.gradle.api.attributes.Usage
import org.gradle.api.internal.artifacts.publish.ArchivePublishArtifact
import org.gradle.api.internal.component.SoftwareComponentInternal
import org.gradle.api.internal.component.UsageContext
import org.gradle.api.plugins.JavaPlugin
import org.gradle.api.tasks.bundling.AbstractArchiveTask

final class AndroidVariantLibrary implements SoftwareComponentInternal {

private final String variantName
private final UsageContext compileUsage
private final UsageContext runtimeUsage
private final Set<UsageContext> _usages
private final PublishConfiguration publishConfiguration

AndroidVariantLibrary(Project project, BaseVariant variant) {
variantName = variant.name
compileUsage = new CompileUsage(project, variant)
runtimeUsage = new RuntimeUsage(project, variant)
AndroidVariantLibrary(ConfigurationContainer configurations, PublishConfiguration publishConfiguration) {
this.publishConfiguration = publishConfiguration

final UsageContext compileUsage = new CompileUsage(configurations, publishConfiguration)
final UsageContext runtimeUsage = new RuntimeUsage(configurations, publishConfiguration)

def usages = [compileUsage]
if (configurations.findByName(JavaPlugin.IMPLEMENTATION_CONFIGURATION_NAME)) {
usages += runtimeUsage
}
_usages = Collections.unmodifiableSet(usages.toSet())
}

@Override
Set<UsageContext> getUsages() {
return Collections.unmodifiableSet([compileUsage, runtimeUsage].toSet())
return _usages
}

@Override
String getName() {
return "android${variantName.capitalize()}"
return publishConfiguration.name
}


private static class CompileUsage extends BaseUsage {

private DependencySet dependencies

CompileUsage(Project project, BaseVariant variant) {
super(project, variant)
CompileUsage(ConfigurationContainer configurations, PublishConfiguration publishConfiguration) {
super(configurations, publishConfiguration)
}

@Override
Expand All @@ -68,7 +69,12 @@ final class AndroidVariantLibrary implements SoftwareComponentInternal {
@Override
Set<ModuleDependency> getDependencies() {
if (dependencies == null) {
dependencies = configurations.findByName(variant.name + JavaPlugin.API_ELEMENTS_CONFIGURATION_NAME.capitalize()).allDependencies
def apiElements = publishConfiguration.publishConfig + JavaPlugin.API_ELEMENTS_CONFIGURATION_NAME.capitalize()
if (configurations.findByName(apiElements)) {
dependencies = configurations.findByName(apiElements).allDependencies
} else {
dependencies = configurations.findByName('default').allDependencies
}
}
return dependencies.withType(ModuleDependency)
}
Expand All @@ -78,8 +84,8 @@ final class AndroidVariantLibrary implements SoftwareComponentInternal {

private DependencySet dependencies

RuntimeUsage(Project project, BaseVariant variant) {
super(project, variant)
RuntimeUsage(ConfigurationContainer configurations, PublishConfiguration publishConfiguration) {
super(configurations, publishConfiguration)
}

@Override
Expand All @@ -90,33 +96,25 @@ final class AndroidVariantLibrary implements SoftwareComponentInternal {
@Override
Set<ModuleDependency> getDependencies() {
if (dependencies == null) {
dependencies = configurations.findByName(variant.name + JavaPlugin.RUNTIME_ELEMENTS_CONFIGURATION_NAME.capitalize()).allDependencies
def runtimeElements = publishConfiguration.publishConfig + JavaPlugin.RUNTIME_ELEMENTS_CONFIGURATION_NAME.capitalize()
dependencies = configurations.findByName(runtimeElements).allDependencies
}
return dependencies.withType(ModuleDependency)
}
}

private static abstract class BaseUsage implements UsageContext {
protected final Project project
protected final BaseVariant variant
protected final ConfigurationContainer configurations
protected final PublishConfiguration publishConfiguration;

BaseUsage(Project project, BaseVariant variant) {
this.project = project
this.configurations = project.configurations
this.variant = variant
BaseUsage(ConfigurationContainer configurations, PublishConfiguration publishConfiguration) {
this.configurations = configurations
this.publishConfiguration = publishConfiguration
}

@Override
Set<PublishArtifact> getArtifacts() {
def artifacts = variant.outputs.collect { o ->
def archiveTask = project.tasks.findByName("bundle${variant.name.capitalize()}")

return new ArchivePublishArtifact(archiveTask as AbstractArchiveTask)
.builtBy(o.assemble)
}.toSet()

return Collections.unmodifiableSet(artifacts)
return publishConfiguration.artifacts
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright 2017 W.UP Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package digital.wup.android_maven_publish

import com.android.build.gradle.LibraryExtension
import org.gradle.api.Project
import org.gradle.api.artifacts.Dependency
import org.gradle.api.artifacts.PublishArtifact

class DefaultPublishConfiguration implements PublishConfiguration {

private Project project

DefaultPublishConfiguration(Project project) {
this.project = project
}

@Override
String getName() {
return 'android'
}

@Override
String getPublishConfig() {
def android = project.extensions.getByType(LibraryExtension)
return android.defaultPublishConfig
}

@Override
Set<PublishArtifact> getArtifacts() {
def configurations = project.configurations
def artifacts = configurations.getByName(Dependency.ARCHIVES_CONFIGURATION).allArtifacts.toSet()

// Fix duplicated artifact when use android gradle build tools 2.3.3 or lower
return artifacts.unique(false, new Comparator<PublishArtifact>() {
@Override
int compare(PublishArtifact a1, PublishArtifact a2) {
"${a1.file.path}${a1.type}${a1.classifier}" <=> "${a2.file.path}${a2.type}${a2.classifier}"
}
})
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright 2017 W.UP Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package digital.wup.android_maven_publish

import org.gradle.api.artifacts.PublishArtifact

interface PublishConfiguration {

/**
* Component name for publish
*
* @return Name of component
*/
String getName()

/**
* Publish configuration name or variant name
* @return
*/
String getPublishConfig()

/**
* Publish artifacts
*
* @return Artifacts
*/
Set<PublishArtifact> getArtifacts()
}
Loading

0 comments on commit 08997c3

Please sign in to comment.