Home > OS >  How to make aar the default artifact in your library
How to make aar the default artifact in your library

Time:09-22

I need to publish a library on Sonatype, on our own server at the company. I've used maven-publish plugin with the following implementation.

apply plugin: 'maven-publish'

task androidSourcesJar(type: Jar) {
    archiveClassifier.set('sources')
    if (project.plugins.findPlugin("com.android.library")) {
        from android.sourceSets.main.java.srcDirs
        from android.sourceSets.main.kotlin.srcDirs
    }
}

afterEvaluate {
    publishing {
        publications {
            release(MavenPublication) {
                groupId GROUP_ID
                artifactId ARTIFACT_ID
                version VERSION_NAME

                from components.release
                artifact androidSourcesJar

                pom {
                    name = ARTIFACT_ID
                    description = DESCRIPTION
                    url = 'library url'
                    licenses {
                        license {
                            name = 'License'
                            url = 'license url'
                        }
                    }
                    developers {
                        developer {
                            id = 'dev id'
                            name = 'dev name'
                            email = 'dev mail'
                        }
                    }
                    scm {
                        connection = 'url.git'
                        developerConnection = 'url.git'
                        url = 'url.git'
                    }
                }
            }
        }

        repositories {
            maven {
                url MAVEN_REPOSITORY_URL
                credentials {
                    username = USER_NAME
                    password = PASSWORD
                }
            }
        }
    }
}

The library got published successfully, but when I use it I have to specify that I need the aar as following.

implementation 'com.companyname.android:library-name:1.0.0@aar'

what should I do to use it without the aar specification? like the following.

implementation 'com.companyname.android:library-name:1.0.0'

So Gradle could find the aar file and add it to the external libraries.

CodePudding user response:

You have to define the aar as artifact in your publications:

artifact("$buildDir/outputs/aar/something-release.aar")

Have a look at this answer for a simple example.

CodePudding user response:

I've solved it. The problem wasn't in the pom or in the way I am packging the library. It was in the way I define it in Gradle. I used to define the custom maven repository in the root Gradle this way.

allprojects {
    repositories {
        google()
        mavenCentral()
        maven {
            url "http://localhost:8081/repository/maven-releases/"
            credentials {
                username = "username"
                password = "password"
            }
            allowInsecureProtocol(true)
            metadataSources{
                mavenPom()
            }
        }
    }
}

However, the documentation stated that

"Since Gradle 5.3, when parsing a metadata file, be it, Ivy or Maven, Gradle will look for a marker indicating that a matching Gradle Module Metadata files exist. If it is found, it will be used instead of the Ivy or Maven file.

Starting with Gradle 5.6, you can disable this behavior by adding ignoreGradleMetadataRedirection() to the metadataSources declaration."

so I've edited it as following.

allprojects {
    repositories {
        google()
        mavenCentral()
        maven {
            url "http://localhost:8081/repository/maven-releases/"
            credentials {
                username = "username"
                password = "password"
            }
            allowInsecureProtocol(true)
            metadataSources{
                mavenPom()
                ignoreGradleMetadataRedirection()
            }
        }
    }
}
  • Related