Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.9k views
in Technique[技术] by (71.8m points)

android - Manually adding aar with dependency pom/iml file

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 jar files does not contain any dependency. So once I manually import the aar in android studio (using Import .JAR/.AA Package) there is no dependency, and I have to manually add all dependencies again. I already generated a pom file through a gradle task, although I cannot find any way to manually import it on the project.

On the build.gradle file automatically generated by the "Import .JAR/.AA Package" is:

configurations.maybeCreate("default")
artifacts.add("default", file('TestSample_1.0.0.aar'))

Is there a way to add the pom/iml file too? something like:

artifacts.add("default", file('pomDependencies.xml'))
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

1. Publishing

In your aar project, add maven-publish plugin and add necessary plugin configuration.

apply plugin: 'com.android.library'
apply plugin: 'maven-publish'

...

dependencies {
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.1.1'
    compile 'com.novoda:bintray-release:0.2.7'
}
    
...

publishing {
    publications {
        maven(MavenPublication) {
            groupId 'com.example' //You can either define these here or get them from project conf elsewhere
            artifactId 'example'
            version '0.0.1-SNAPSHOT'
            artifact "$buildDir/outputs/aar/app-release.aar" //aar artifact you want to publish

            //generate pom nodes for dependencies
            pom.withXml {
                def dependenciesNode = asNode().appendNode('dependencies')
                configurations.compile.allDependencies.each { dependency ->
                    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"
        }
    }
}

Few things to note:

  1. We're using a custom maven publication, so you have to define what is being published with the artifact clause

  2. We have to generate the pom ourselves, in the code above I'm using all compile config dependencies, you may want to make sure all the configs you care about are covered.

Running gradle publish will publish to a maven repo structure to the repo folder, which you can then consume from a different project.

2. Using published .aar

In a different android project, to use the aar published in #1: In top level build.gradle:

allprojects {
    repositories {
        jcenter()
        maven {
            url "D:/full/path/to/repo"
        }
    }
}

add the path to earlier repo as a maven repository. Note that you may have to use the full path, because $buildDir has a different value for this project. In your app build.gradle:

dependencies {
    ...
    other dependencies
    ...
    implementation ('com.example:example:0.0.1-SNAPSHOT@aar'){transitive=true}
}

transitive=true is required for to fetch the transitive dependencies from the pom file.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...