Android gradle dependency not downloading internal dependencies when build with flavours - android

I am having one android project which includes one own dependency (Module1), which internally includes another dependency(Module2). In my project app not able to consume classes from Module2.
App gradle looks like:
flavourApi("com.own.dependency:module1:0.0.1"){
isTransitive = true
}
Module1 gradle looks like:
flavourApi("com.own.dependency:module2:0.0.1")
App is able to download module1 but not downloading module2.
Publishing Module1 looks like as follow
pom {
name.set(POM_NAME)
description.set(POM_DESCRIPTION)
url.set(POM_URL)
scm {
connection.set(POM_SCM_CONNECTION)
url.set(POM_SCM_URL)
developerConnection.set(POM_SCM_DEV_CONNECTION)
}
developers {
developer {
id.set(POM_DEVELOPER_ID)
name.set(POM_DEVELOPER_NAME)
email.set(POM_DEVELOPER_EMAIL)
}
}
withXml {
val dependenciesNode = asNode().appendNode("dependencies")
configurations.implementation.get().allDependencies.forEach {
if (it.group != null && it.version != null) {
val dependencyNode = dependenciesNode.appendNode("dependency")
dependencyNode.appendNode("groupId", it.group)
dependencyNode.appendNode("artifactId", it.name)
dependencyNode.appendNode("version", it.version)
}
}
configurations.api.get().allDependencies.forEach {
if (it.group != null && it.version != null) {
val dependencyNode = dependenciesNode.appendNode("dependency")
dependencyNode.appendNode("groupId", it.group)
dependencyNode.appendNode("artifactId", it.name)
dependencyNode.appendNode("version", it.version)
}
}
configurations.releaseApi.get().allDependencies.forEach {
if (it.group != null && it.version != null) {
val dependencyNode = dependenciesNode.appendNode("dependency")
dependencyNode.appendNode("groupId", it.group)
dependencyNode.appendNode("artifactId", it.name)
dependencyNode.appendNode("version", it.version)
}
}
}
}
App downloads Module1 but not downloading Module2. POM file is generated for Module1 which includes dependency node for Module2. I don't want to include Module2 as dependency for app.
Got the Answer :
Need to include flavour dependencies :
configurations.getByName("flavourApi").allDependencies.forEach{
if (it.group != null && it.version != null) {
val dependencyNode = dependenciesNode.appendNode("dependency")
dependencyNode.appendNode("groupId", it.group)
dependencyNode.appendNode("artifactId", it.name)
dependencyNode.appendNode("version", it.version)
}
}

Related

Android publishing flavour based artifact on Artifactory

I am trying publish flavour based artifact on my Artifactory server. Gradle code looks like this
def GroupId = 'com.example.directory'
def ArtifactId = 'demo
def Version = '0.0.1-IB1''
productFlavors {
extended {
ArtifactId = "extended"
}
mini {
ArtifactId = "mini"
}
}
publishing {
publications {
aar(MavenPublication) {
android.libraryVariants.all { variant ->
groupId GroupId
version Version
artifactId ArtifactId
def filename = "${archivesBaseName}-${variant.baseName}.aar"
artifact("$buildDir/outputs/aar/${filename}")
}
pom.withXml {
def dependenciesNode = asNode().appendNode('dependencies')
def deps = configurations.implementation.allDependencies + configurations.api.allDependencies
deps.each {
if (it.group != null && (it.name != null || "unspecified" == it.name) && it.version != null && "unspecified" != it.version) {
def dependencyNode = dependenciesNode.appendNode('dependency')
dependencyNode.appendNode('groupId', it.group)
dependencyNode.appendNode('artifactId', it.name)
dependencyNode.appendNode('version', it.version)
}
}
}
}
}
}
artifactory {
contextUrl = 'http://artifactoryUrl/artifactory'
publish {
repository {
repoKey = GroupId
username = "${userName}"
password = "${pass}"
}
defaults {
publications('aar')
publishArtifacts = true
properties = ['qa.level': 'basic', 'dev.team': 'core']
publishPom = true
}
}
}
But when I run gradle command it generates the specific flavour + build type artifact. But while publishing it tries to publish all varient of the library and throws an error for other aar file names.
./gradlew clean && ./gradlew :demo:assembleextendedrelease && ./gradlew :demo:artifactoryPublish

Maven api pulish with Gradle 7.2

I want to publish an Android in jitpack.io but api dependencies are missing, that's why I use with Gradle 7.2 this
project.afterEvaluate {
publishing {
publications {
release(MavenPublication) {
from components.release
pom {
configurations.api.getDependencies().each { dep -> addDependency(dep, "compile") }
configurations.implementation.getDependencies().each { dep -> addDependency(dep, "runtime") }
}
}
}
}
}
This works with Gradle 6.x but with Gradle 7.x it fails
configurations.api.getDependencies().each { dep -> addDependency(dep, "compile") }
with
Could not find method addDependency() for arguments [DefaultProjectDependency{dependencyProject='project ':libs:gvr'', configuration='default'}] on object of type org.gradle.api.publish.maven.internal.publication.DefaultMavenPom.
Does someone knows how to solve it ?
Use this snippet
release(MavenPublication) {
// Applies the component for the release build variant.
from components.release
artifact(sourcesJar)
// You can then customize attributes of the publication as shown below.
groupId = '[group]'
artifactId = '[artifact]'
version = '[version]'
pom.withXml {
def dependenciesNode = (asNode().get("dependencies") as groovy.util.NodeList).get(0) as groovy.util.Node
def configurationNames = ["implementation", "api"]
configurationNames.forEach { configurationName ->
configurations[configurationName].allDependencies.forEach {
if (it.group != null && it.version != "unspecified") {
def dependencyNode = dependenciesNode.appendNode("dependency")
dependencyNode.appendNode("groupId", it.group)
dependencyNode.appendNode("artifactId", it.name)
dependencyNode.appendNode("version", it.version)
}
}
}
}
}
}
}

How to generate maven pom with maven-publish gradle plugin after migrating to BOM

I was using the following for generating pom with maven-publish plugin in my build.gradle :
configurations.implementation.allDependencies.each {
if (it.group != null && (it.name != null || "unspecified" == it.name) && it.version != null) {
def dependencyNode = dependenciesNode.appendNode('dependency')
dependencyNode.appendNode('groupId', it.group)
dependencyNode.appendNode('artifactId', it.name)
dependencyNode.appendNode('version', it.version)
}
}
which was working great when using dependencies as follow :
implementation 'com.google.firebase:firebase-config:19.2.0'
implementation 'com.google.firebase:firebase-messaging:20.3.0'
implementation 'com.google.firebase:firebase-analytics:17.6.0'
implementation 'com.google.firebase:firebase-crashlytics:17.2.2'
but not anymore after migrating to BOM :
implementation platform('com.google.firebase:firebase-bom:26.0.0')
implementation 'com.google.firebase:firebase-config'
implementation 'com.google.firebase:firebase-messaging'
implementation 'com.google.firebase:firebase-analytics'
implementation 'com.google.firebase:firebase-crashlytics'
which do not get versions (and thus do not include dependencies) while publishing :
> Task :vendor:generatePomFileForPublication
DefaultExternalModuleDependency{group='com.google.firebase', name='firebase-bom', version='26.0.0', configuration='default'}
DefaultExternalModuleDependency{group='com.google.firebase', name='firebase-config', version='null', configuration='default'}
DefaultExternalModuleDependency{group='com.google.firebase', name='firebase-messaging', version='null', configuration='default'}
DefaultExternalModuleDependency{group='com.google.firebase', name='firebase-analytics', version='null', configuration='default'}
DefaultExternalModuleDependency{group='com.google.firebase', name='firebase-crashlytics', version='null', configuration='default'}
Does one knows how to read versions from the BOM using gradle DSL ?
Or how to generate the dependencyManagement part of the pom.xml to reflect the BOM ?
Decided to import BOM under dependencyManagement in generated pom.xml
google(MavenPublication) {
groupId 'group'
artifactId 'artifact'
version android.defaultConfig.versionName
artifact(sourceJar)
artifact("$buildDir/outputs/aar/my-release.aar") {
extension 'aar'
}
pom.withXml {
def dependenciesNode = asNode().appendNode('dependencies')
def dependenciesManagementNode = asNode().appendNode('dependencyManagement').appendNode('dependencies')
configurations.implementation.allDependencies.each {
if (it.group != null && (it.name != null || "unspecified" == it.name)) {
if (it.name.endsWith("-bom")) {
def dependencyNode = dependenciesManagementNode.appendNode('dependency')
dependencyNode.appendNode('groupId', it.group)
dependencyNode.appendNode('artifactId', it.name)
dependencyNode.appendNode('version', it.version)
dependencyNode.appendNode('scope', "import")
dependencyNode.appendNode('type', "pom")
} else {
def dependencyNode = dependenciesNode.appendNode('dependency')
dependencyNode.appendNode('groupId', it.group)
dependencyNode.appendNode('artifactId', it.name)
if (it.version != null) {
dependencyNode.appendNode('version', it.version)
}
}
}
}
}
}

Could not apply withXml() to generated POM > Could not get unknown property 'implementation'

I am trying to publish an Android library to bintray. I have set it up in both bintray and my Android project.
publish.gradle:
apply plugin: 'maven-publish'
apply plugin: 'com.jfrog.bintray'
version '1.0'
group 'np.com.ngimasherpa'
publishing {
publications {
Production(MavenPublication) {
artifact("$buildDir/outputs/aar/emptyviewsupportrecyclerview-release.aar")
groupId this.group
artifactId 'emptyviewsupportrecyclerview'
version this.version
pom.withXml {
def dependenciesNode = asNode().appendNode('dependencies')
configurations.implementation.allDependencies.each {
if (it.name != 'unspecified') {
def dependencyNode = dependenciesNode.appendNode('dependency')
dependencyNode.appendNode('groupId', it.group)
dependencyNode.appendNode('artifactId', it.name)
dependencyNode.appendNode('version', it.version)
}
}
}
}
}
}
bintray {
user = project.hasProperty('user') ?: System.getenv('BINTRAY_USER')
key = project.hasProperty('apiKey') ?: System.getenv('BINTRAY_API_KEY')
publications = ['Production']
configurations = ['archives']
dryRun = false
override = false
publish = true
pkg {
repo = 'EmptySupportRecyclerView'
name = 'np.com.ngimasherpa.emptyviewsupportrecyclerview'
version {
name = this.version
released = new Date()
vcsTag = this.version
}
}
}
When I'm trying to upload the library using ./gradlew bintrayUpload, I get:
Execution failed for task ':generatePomFileForProductionPublication'.
- Could not apply withXml() to generated POM
- Could not get unknown property 'implementation' for configuration container of type
org.gradle.api.internal.artifacts.configurations.DefaultConfigurationContainer.

Kotlin Android Library Module exported with gradle maven-publish plugin not adding dependencies

I'm trying to export an Android Library Module developed in Kotlin using gradle's maven-publish plugin. aar file is successfully generated and exported however no dependency is added to it - at all, not even Kotlin ones. Below is the plugin configuration based on this answer:
apply plugin: 'maven-publish'
Properties properties = new Properties()
properties.load(project.rootProject.file('local.properties').newDataInputStream())
ext {
releaseRepository = "s3://****/maven/releases"
snapshotsRepository = "s3://****/maven/snapshots"
artifactRepository = "$buildDir/outputs/aar/render-engine-release.aar"
_version = properties.getProperty('lib.version')
_artifact = properties.getProperty('lib.name')
_group = properties.getProperty('lib.group')
}
task sourceJar(type: Jar) {
from android.sourceSets.main.java.srcDirs
classifier "source"
}
publishing {
publications {
maven(MavenPublication) {
groupId _group
artifactId _artifact
version _version
artifact (sourceJar)
artifact artifactRepository
pom.withXml {
def dependenciesNode = asNode().appendNode('dependencies')
configurations.compile.allDependencies.each {
if(_group != null && (_artifact != null || "unspecified" == _artifact) && _version != null) {
def dependencyNode = dependenciesNode.appendNode('dependency')
dependencyNode.appendNode('groupId', _group)
dependencyNode.appendNode('artifactId', _artifact)
dependencyNode.appendNode('version', _version)
}
}
}
}
}
repositories {
maven {
if(_version.endsWith('-SNAPSHOT')) {
url snapshotsRepository
} else {
url releaseRepository
}
credentials(AwsCredentials) {
accessKey AWS_ACCESS_KEY
secretKey AWS_SECRET_KEY
}
}
}
}
Ok, so keeping reading the answers on the linked question I've implemented this that solved my problem. My final publish.gradle is like:
apply plugin: 'maven-publish'
Properties properties = new Properties()
properties.load(project.rootProject.file('local.properties').newDataInputStream())
ext {
releaseRepository = "s3://****/maven/releases"
snapshotsRepository = "s3://****/maven/snapshots"
artifactRepository = "$buildDir/outputs/aar/render-engine-release.aar"
_version = properties.getProperty('lib.version')
_artifact = properties.getProperty('lib.name')
_group = properties.getProperty('lib.group')
}
task sourceJar(type: Jar) {
from android.sourceSets.main.java.srcDirs
classifier "source"
}
publishing {
publications {
maven(MavenPublication) {
groupId _group
artifactId _artifact
version _version
artifact(sourceJar)
artifact artifactRepository
pom.withXml {
def dependenciesNode = asNode().appendNode('dependencies')
configurations.compile.getAllDependencies().each { Dependency dep ->
if (dep.group == null || dep.version == null || dep.name == null || dep.name == "unspecified")
return
def dependencyNode = dependenciesNode.appendNode('dependency')
dependencyNode.appendNode('groupId', dep.group)
dependencyNode.appendNode('artifactId', dep.name)
dependencyNode.appendNode('version', dep.version)
if (!dep.transitive) {
def exclusionNode = dependencyNode.appendNode('exclusions').appendNode('exclusion')
exclusionNode.appendNode('groupId', '*')
exclusionNode.appendNode('artifactId', '*')
} else if (!dep.properties.excludeRules.empty) {
def exclusionsNode = dependencyNode.appendNode('exclusions')
dep.properties.excludeRules.each { ExcludeRule rule ->
def exclusionNode = exclusionsNode.appendNode('exclusion')
exclusionNode.appendNode('groupId', rule.group ?: '*')
exclusionNode.appendNode('artifactId', rule.module ?: '*')
}
}
}
}
}
}
repositories {
maven {
if (_version.endsWith('-SNAPSHOT')) {
url snapshotsRepository
} else {
url releaseRepository
}
credentials(AwsCredentials) {
accessKey AWS_ACCESS_KEY
secretKey AWS_SECRET_KEY
}
}
}
}

Categories

Resources