Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android library dependencies missing from POM with Gradle

I am using Gradle to build an Android library project and deploy it to maven repository as an aar.

The library has some dependencies, which should be included in the POM

With apply plugin: 'maven' no POM file is present, just the artifact

With apply plugin: 'maven-publish' a POM file is generated, but it does not include any dependencies

Any ideas? Is this just not supported?

Gradle 2.2 and Android Gradle Plugin 1.1.0

First approach:

configurations {
    archives {
        extendsFrom configurations.default
    }
}

afterEvaluate { project ->
    uploadArchives {

        configuration = configurations.archives

        repositories {
            mavenDeployer {
                repository(url: "http://nexus-url") {
                    authentication(userName: nexusUsername, password: nexusPassword)

                pom.groupId = 'com.example'
                pom.version = '123-SNAPSHOT'
                pom.artifactId = 'foo'
                pom.packaging = 'aar'

                pom.project {
                    artifactId = 'bar'
                    packaging 'aar'
                    description 'baz'
            }
        }
    }
}

Also tried it without wrapping it in afterEvaluate

Second approach:

publishing {
    publications {
        sdk(MavenPublication) {
            groupId 'com.example'
            artifactId 'foo'
            version = "0.123-SNAPSHOT"
            artifact("$buildDir/outputs/aar/app-sdk-debug.aar")
        }
    }
    repositories {
        maven {
            url "http://nexus-url"
            credentials {
                username 'foo'
                password 'bar'
            }
        }
    }
}

Update

The root cause of the problem is that this project uses flavors. Without flavors the pom is generated properly when using apply plugin: 'maven'

like image 717
Philipp E. Avatar asked Mar 03 '15 08:03

Philipp E.


People also ask

How do I add a dependency in Gradle?

To add a dependency to your project, specify a dependency configuration such as implementation in the dependencies block of your module's build.gradle file.

How do I add a dependency to Pom?

Add a Java Maven Dependency to the Utility Projectfield (e.g., commons-logging) to search for a dependency. Select the dependency, and click OK. Expand the utility project, right-click the pom. xml file, and select Run As>Maven Install to install the file into the local repository.

Where are the dependencies in Android Studio?

Go to File > Project Structure in Android Studio. Select the app module in the Modules list on the left. Select the Dependencies tab. Click the + button on the lower left to add the dependency.

What is dependencyResolutionManagement?

The dependencyResolutionManagement repositories block accepts the same notations as in a project, which includes Maven or Ivy repositories, with or without credentials, etc. By default, repositories declared by a project will override whatever is declared in settings.


1 Answers

This is the solution that worked for me in the end:

publishing {
    publications {
        sdk(MavenPublication) {
            artifactId libName
            artifact "${project.buildDir}/outputs/aar/${libName}-${project.version}.aar"

            //The publication doesn't know about our dependencies, so we have to manually add them to the pom
            pom.withXml {
                // for dependencies and exclusions
                def dependenciesNode = asNode().appendNode('dependencies')
                configurations.implementation.allDependencies.withType(ModuleDepend‌​ency) { ModuleDependency dp ->
                    def dependencyNode = dependenciesNode.appendNode('dependency')
                    dependencyNode.appendNode('groupId', dp.group)
                    dependencyNode.appendNode('artifactId', dp.name)
                    dependencyNode.appendNode('version', dp.version)

                    // for exclusions
                    if (dp.excludeRules.size() > 0) {
                        def exclusions = dependencyNode.appendNode('exclusions')
                        dp.excludeRules.each { ExcludeRule ex ->
                            def exclusion = exclusions.appendNode('exclusion')
                            exclusion.appendNode('groupId', ex.group)
                            exclusion.appendNode('artifactId', ex.module)
                        }
                    }
                }
            }
        }
    }

    repositories {
        maven {
            name 'myrepo'
            url 'https://maven.foo.com'

            credentials {
                username mavenUsername
                password mavenPassword
            }      
        }
    }
}
like image 128
Philipp E. Avatar answered Oct 05 '22 12:10

Philipp E.