Manually adding aar with dependency pom/iml file

后端 未结 2 1986
悲哀的现实
悲哀的现实 2020-12-02 09:48

Since I cannot use a private maven in order to share my library, I was thinking in sharing the aar and importing into another project. The problem comes when the aar and ja

2条回答
  •  庸人自扰
    2020-12-02 10:06

    Things have changed a little, here's how you do it with the latest versions of gradle

    Create the package localy (aar and pom)

    Modify your library build.gradle file to include

    apply plugin: 'maven-publish'
    
    android {
        ...
        ...
    }
    
    dependencies {
        ...
        ...
    }
    
    publishing {
        publications {
            maven(MavenPublication) {
                groupId 'com.domain' //You can either define these here or get them from project conf elsewhere
                artifactId 'name'
                version '1.0.0'
                artifact "$buildDir/outputs/aar/sdk-release.aar" //aar artifact you want to publish
    
                //generate pom nodes for dependencies
                pom.withXml {
                def dependenciesNode = asNode().appendNode('dependencies')
                configurations.implementation.allDependencies.each { dependency ->
                    if (dependency.name != 'unspecified') {
                        def dependencyNode = dependenciesNode.appendNode('dependency')
                        dependencyNode.appendNode('groupId', dependency.group)
                        dependencyNode.appendNode('artifactId', dependency.name)
                        dependencyNode.appendNode('version', dependency.version)
                    }
                }
            }
        }
    
        //publish to filesystem repo
        repositories{
            maven {
                url "$buildDir/repo"
            }
        }
    }
    

    Run from terminal

    ./gradlew clean
    ./gradlew build
    ./gradlew --console=verbose publishToMavenLocal
    

    The aar and pom files have been created at $HOME/.m2/repository/

    How to load the library from a different project

    Modify the projects's build.gradle in the following way:

    allprojects {
        repositories {
            maven {
                url "/Users/username/.m2/repository/"
            }
            google()
            jcenter()
        }
    

    You can use $rootDir and set a relative path.

    Add the library as a dependency in your app module build.gradle

    implementation 'com.domain:name:1.0.0'

提交回复
热议问题