42

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?

Bao Le
  • 16,643
  • 9
  • 65
  • 68

5 Answers5

60

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

Allan Veloso
  • 5,823
  • 1
  • 38
  • 36
Bao Le
  • 16,643
  • 9
  • 65
  • 68
  • 2
    `[your local maven path here]` is like `~/.m2` folder? – zygimantus May 08 '17 at 20:53
  • 10
    repository(url: mavenLocal().getUrl()) – abg Aug 01 '17 at 13:48
  • In Gradle 7.x, The maven plugin has been removed. You should use the maven-publish plugin instead. https://docs.gradle.org/7.0/userguide/upgrading_version_6.html#removal_of_the_legacy_maven_plugin – repitch Apr 23 '23 at 05:34
30

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.

Andrejs
  • 26,885
  • 12
  • 107
  • 96
5

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

Logain
  • 4,259
  • 1
  • 23
  • 32
5

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
Philipp Hofmann
  • 3,388
  • 26
  • 32
2

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')
Rizwan Asif
  • 125
  • 1
  • 8
  • 2
    This the best way if you are developing a library at the same time, but I am getting an error `Configuration with name 'default' not found` when I try to grade sync. – Megakoresh Oct 05 '16 at 08:16