Gradle: How to publish a Android library to local repository

后端 未结 5 1711
逝去的感伤
逝去的感伤 2020-12-04 10:47

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         


        
相关标签:
5条回答
  • 2020-12-04 11:10

    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
    
    0 讨论(0)
  • 2020-12-04 11:18

    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

    0 讨论(0)
  • 2020-12-04 11:28

    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.

    0 讨论(0)
  • 2020-12-04 11:32

    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')
    
    0 讨论(0)
  • 2020-12-04 11:34

    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

    0 讨论(0)
提交回复
热议问题