Home » Android » Gradle: How to publish a Android library to local repository

Gradle: How to publish a Android library to local repository

Posted by: admin April 23, 2020 Leave a comment

Questions:

I have a library and a Android app using Gradle and Android Studio. I can include the library directly in the project as following

compile project(':library')

Because I don’t want to mesh up with library source code, I want to publish the library into local repository so that I can use as

compile 'com.mygroup:library:1.0'

Any advise?

How to&Answers:

I just found a solution. In the build.gradle of the library project, add this

apply plugin: 'maven'

group = 'com.mygroup'
version = '1.0'

uploadArchives {
    repositories {
        mavenDeployer {
            repository(url: "file://[your local maven path here]")
            // or repository(url: mavenLocal().getUrl()) 
        }
    }
}

In the project folder, type following command

gradle uploadArchives

Read Publishing artifacts for more information

Answer:

For an Android Library you should use the Android Gradle-Maven plugin https://github.com/dcendents/android-maven-gradle-plugin:

buildscript {
    repositories {
        mavenCentral()
    }

    dependencies {
        classpath 'com.github.dcendents:android-maven-gradle-plugin:1.3'
    }
}

apply plugin: 'com.android.library'
apply plugin: 'com.github.dcendents.android-maven'

Then to publish to your local repository run:

./gradlew install

which will install it in $HOME/.m2/repository. In your app or other project you can then include it by adding mavenLocal() to your repositories.

Alternatively, if your library is on GitHub then you can simply include it in other projects using JitPack. Then you don’t have to run the above command and can just use what’s been pushed.

Answer:

Publish de library on your local maven repository and then on your gradle use

repositories {
  mavenLocal()
}

If you have other repositories listed, make sure your mavenLocal appears first.

Docs: section 51.6.4 on https://gradle.org/docs/current/userguide/dependency_management.html

Answer:

I prefer adding the java sources and the javadoc to the maven repository. The following script publishes your android library to a maven repository using the android maven plugin. It creates the .aar, javadoc.jar, sources.jar and .pom and updates the maven-metadata.xml after uploading the files to the maven repository. I also put the script on GitHub.

apply plugin: 'com.android.library'
apply plugin: 'maven'

//Your android configuration
android {
    //...
}

//maven repository info
group = 'com.example'
version = '1.0.0'

ext {
    //Specify your maven repository url here
    repositoryUrl = 'ftp://your.maven.repository.com/maven2'
    //Or you can use 'file:\\C:\Temp' or 'maven-temp' for a local maven repository
}

//Upload android library to maven with javadoc and android sources
configurations {
    deployerJars
}

//If you want to deploy to an ftp server
dependencies {
    deployerJars "org.apache.maven.wagon:wagon-ftp:2.2"
}

// custom tasks for creating source/javadoc jars
task javadoc(type: Javadoc) {
    source = android.sourceSets.main.java.srcDirs
    classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
    destinationDir = file("../javadoc/")
    failOnError false
}

task javadocJar(type: Jar, dependsOn: javadoc) {
    classifier = 'javadoc'
    from javadoc.destinationDir
}

//Creating sources with comments
task androidSourcesJar(type: Jar) {
    classifier = 'sources'
    from android.sourceSets.main.java.srcDirs
}

//Put the androidSources and javadoc to the artifacts
artifacts {
    archives androidSourcesJar
    archives javadocJar
}

uploadArchives {
    repositories {
        mavenDeployer {
            configuration = configurations.deployerJars
            repository(url: repositoryUrl) {
                //if your repository needs authentication
                authentication(userName: "username", password: "password")
            }
        }
    }
}

Call it with

./gradlew uploadArchives

Answer:

In settings.gradle add

include 'riz.com.mylibrary'
project(':riz.com.mylibrary').projectDir = new File('C:\Users\Rizwan Asif\AndroidStudioProjects\mylibrary')

Then in build.gradle in the dependencies add

compile project(':riz.com.mylibrary')