Gradle: How to publish a Android library to local repository

后端 未结 5 1731
逝去的感伤
逝去的感伤 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条回答
  •  猫巷女王i
    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
    

提交回复
热议问题