手动添加 aar 依赖 pom/iml 文件 [英] Manually adding aar with dependency pom/iml file

查看:102
本文介绍了手动添加 aar 依赖 pom/iml 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

关于导入.JAR/.AA包"自动生成的build.gradle文件是:

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'))

有没有办法也添加 pom/iml 文件?类似:

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

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

推荐答案

1.发布

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

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

注意事项:

  1. 我们使用的是自定义 Maven 发布,因此您必须使用 artifact 子句定义发布的内容

我们必须自己生成 pom,在上面的代码中,我使用了所有编译配置依赖项,您可能需要确保覆盖了您关心的所有配置.

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.

运行 gradle publish 会将 maven repo 结构发布到 repo 文件夹,然后您可以从不同的项目中使用该文件夹.

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

在不同的 android 项目中,要使用 #1 中发布的 aar:在顶级 build.gradle 中:

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

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

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 是从 pom 文件中获取传递依赖项所必需的.

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

这篇关于手动添加 aar 依赖 pom/iml 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆