手动添加具有依赖性pom / iml文件的aar

时间:2016-01-19 08:56:50

标签: android android-studio gradle pom.xml aar

由于我不能使用私人专家来共享我的图书馆,我正在考虑共享aar并导入另一个项目。 当aar和jar文件不包含任何依赖项时,问题就出现了。所以一旦我在android studio中手动导入aar(使用Import .JAR / .AA Package)就没有依赖关系,我必须再次手动添加所有依赖项。 我已经通过gradle任务生成了一个pom文件,虽然我找不到任何方法可以在项目中手动导入它。

在"导入.JAR / .AA包"自动生成的build.gradle文件上;是:

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

有没有办法添加pom / iml文件?类似的东西:

artifacts.add("default", file('pomDependencies.xml'))

2 个答案:

答案 0 :(得分:32)

1。出版

在您的aar项目中,添加maven-publish插件并添加必要的插件配置。

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"
        }
    }
}

很少有事情需要注意:

  1. 我们正在使用自定义maven出版物,因此您必须使用artifact子句定义要发布的内容

  2. 我们必须自己生成pom,在上面的代码中我使用了所有编译配置依赖项,您可能希望确保涵盖所有您关注的配置。

  3. 运行gradle publish将发布到maven repo结构到repo文件夹,然后您可以从另一个项目中使用该文件夹。

    2。使用已发布的.aar

    在另一个Android项目中,使用#1中发布的aar: 在顶级build.gradle中:

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

    将早期repo的路径添加为maven存储库。请注意,您可能必须使用完整路径,因为$buildDir具有此项目的不同值。在您的app build.gradle中:

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

    transitive=true是从pom文件中获取传递依赖项所必需的。

答案 1 :(得分:2)

情况发生了一些变化,这是您使用最新版本的gradle

的方法

创建本地软件包(aar和pom)

修改您的库build.gradle文件以包含

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"
        }
    }
}

从终端运行

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

aarpom文件已在$HOME/.m2/repository/上创建

如何从其他项目加载库

通过以下方式修改项目的build.gradle

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

您可以使用$rootDir并设置相对路径。

在您的应用模块build.gradle

中将库添加为依赖项

implementation 'com.domain:name:1.0.0'

相关问题