Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle: How to publish a Android library to local repository

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?

like image 463
Bao Le Avatar asked Feb 06 '15 08:02

Bao Le


3 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

like image 86
Bao Le Avatar answered Oct 09 '22 18:10

Bao Le


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.

like image 32
Andrejs Avatar answered Oct 09 '22 17:10

Andrejs


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

like image 5
Logain Avatar answered Oct 09 '22 18:10

Logain