由于我不能使用私有(private) maven 来共享我的库,我正在考虑共享 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'))
最佳答案
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"
}
}
}
注意事项:
我们使用的是自定义 maven 发布,因此您必须使用
artifact
子句定义发布的内容我们必须自己生成 pom,在上面的代码中我使用了所有编译配置依赖项,您可能希望确保涵盖所有您关心的配置。
运行 gradle publish
将发布到 repo
文件夹的 maven repo 结构,然后您可以从不同的项目中使用它。
2。使用已发布的 .aar
在不同的android项目中,使用#1中发布的aar: 在顶级 build.gradle 中:
allprojects {
repositories {
jcenter()
maven {
url "D:/full/path/to/repo"
}
}
}
将早期存储库的路径添加为 Maven 存储库。请注意,您可能必须使用完整路径,因为 $buildDir
对该项目具有不同的值。在您的应用 build.gradle 中:
dependencies {
...
other dependencies
...
implementation ('com.example:example:0.0.1-SNAPSHOT@aar'){transitive=true}
}
transitive=true
是从 pom 文件中获取传递依赖项所必需的。
关于android - 使用依赖 pom/iml 文件手动添加 aar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34872382/