Java 使用 gradle 无法为 aar 库解析传递依赖项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22795455/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Transitive dependencies not resolved for aar library using gradle
提问by mkorszun
I have investigated a while and probably saw most popular answers here related to aarand transitive dependenciesbut somehow it is still not clear for me how to make this working.
我已经调查了一段时间,可能在这里看到了与aar和传递依赖相关的最流行的答案,但不知何故,我仍然不清楚如何使其工作。
So:
所以:
I have android library with given gradle config:
我有给定 gradle 配置的 android 库:
apply plugin: 'android-library'
apply plugin: 'android-maven'
version = "1.0.0"
group = "com.somepackage"
buildscript {
repositories {
mavenCentral()
mavenLocal()
}
dependencies {
classpath 'com.github.dcendents:android-maven-plugin:1.0'
}
}
android {
compileSdkVersion 19
buildToolsVersion '19.0.3'
defaultConfig {
minSdkVersion 10
}
}
repositories {
maven { url 'http://www.bugsense.com/gradle/' }
}
dependencies {
provided 'com.google.android.gms:play-services:+'
provided 'com.android.support:appcompat-v7:+'
compile 'com.google.code.gson:gson:2.2.4'
compile 'com.bugsense.trace:bugsense:3.6'
compile 'commons-net:commons-net:3.3'
}
Then I am deploying it to local maven repo with gradle install
. POM file of the deployed library looks like this:
然后我将它部署到本地 maven 仓库gradle install
。已部署库的 POM 文件如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>com.sprezzat</groupId>
<artifactId>app</artifactId>
<version>1.0.0</version>
<packaging>aar</packaging>
<dependencies>
<dependency>
<groupId>com.bugsense.trace</groupId>
<artifactId>bugsense</artifactId>
<version>3.6</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>3.3</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.2.4</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
And finally gradle config of my android application using above library as a dependency:
最后我的android应用程序的gradle配置使用上面的库作为依赖:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.9.+'
}
}
apply plugin: 'android'
repositories {
mavenCentral()
mavenLocal()
}
android {
compileSdkVersion 15
buildToolsVersion "19.0.2"
defaultConfig {
minSdkVersion 10
targetSdkVersion 18
}
}
dependencies {
compile 'com.google.android.gms:play-services:+'
compile 'com.android.support:appcompat-v7:+'
compile 'com.somepackage:LIBRARY_NAME:1.0.0@aar'
}
And after deploying application on phone I am getting NoClassDefFoundError
for classes belonging to compile dependencies of my android library.
在手机上部署应用程序后,我得到NoClassDefFoundError
了属于我的 android 库的编译依赖项的类。
Inspecting my android application dependencies using gradle dependencies
:
使用gradle dependencies
以下命令检查我的 android 应用程序依赖项:
apk - Classpath packaged with the compiled main classes.
+--- com.google.android.gms:play-services:+ -> 4.3.23
| \--- com.android.support:support-v4:19.0.1 -> 19.1.0
+--- com.android.support:appcompat-v7:+ -> 19.1.0
| \--- com.android.support:support-v4:19.1.0
\--- com.somepackage:LIBRARY_NAME:1.0.0
According to above tree, all transitive dependencies are not detected. Where is the problem and how should it be done correctly?
根据上面的树,没有检测到所有的传递依赖。问题出在哪里,应该如何正确完成?
采纳答案by mkorszun
I have solved my problem by setting transitive
attribute for my aar dependency:
我通过transitive
为我的 aar 依赖项设置属性解决了我的问题:
compile ('com.somepackage:LIBRARY_NAME:1.0.0@aar'){
transitive=true
}
回答by user3070402
you should not use "@aar", if use "@" is become "Artifact only notation", if you want to use "@" and want have dependence transitive, you should add "transitive=true"
你不应该使用“@aar”,如果使用“@”变成“ Artifact only notation”,如果你想使用“@”并且想要依赖传递,你应该添加“transitive=true”
回答by suhas_sm
Try this if you are using aar locally:
如果您在本地使用 aar,请尝试此操作:
compile(project(:your-library-name)) {
transitive=true
}
回答by Merc
Simply adding @aar at the end of the dependency is what worked for me.
只需在依赖项的末尾添加 @aar 对我有用。
dependencies {
implementation 'org.videolan.vlc:libvlc:3.0.13@aar'
}
回答by Sergey Dryganets
For me complete publishing solution looks like this:
对我来说,完整的发布解决方案如下所示:
apply plugin: 'com.github.dcendents.android-maven'
group = GROUP
version = VERSION
// you could move it to env variable or property
def publishFlavorless = true
def firstTask = null
android.libraryVariants.all { variant ->
if (variant.name.toLowerCase().contains("debug")) {
// Workaround for https://github.com/gradle/gradle/issues/1487
if (publishFlavorless && firstTask == null) {
def bundleTask = tasks["bundle${variant.name.capitalize()}Aar"]
firstTask = bundleTask
artifacts {
archives(firstTask.archivePath) {
builtBy firstTask
name = project.name
}
}
}
return
}
def bundleTask = tasks["bundle${variant.name.capitalize()}Aar"]
artifacts {
archives(bundleTask.archivePath) {
classifier variant.flavorName
builtBy bundleTask
name = project.name
}
}
}
install {
repositories.mavenInstaller {
// This generates POM.xml with proper parameters
pom.project {
name POM_NAME
artifactId POM_ARTIFACT_ID
// For aar it is equal to 'aar' with jar transitive dependencies won't work
packaging POM_PACKAGING
description POM_DESCRIPTION
}
}
}
The transitive = true
block is required as well ...
该transitive = true
块是必需的,以及...
回答by Reaz Murshed
I was having a similar problem and felt I could share the steps of solving the problem.
我遇到了类似的问题,觉得我可以分享解决问题的步骤。
The basic idea of not being able to use the transitive dependencies while you are publishing your own aar
is actually not having the .pom
file generated with the expected transitive dependencies.
在发布自己的依赖项时无法使用传递依赖项的基本思想aar
实际上是没有.pom
使用预期的传递依赖项生成文件。
I was using 'maven-publish'
plugin for my android aar
dependency to publish it in my own private maven repository. The transitive dependencies were not resolved when my other projects were adding my aar
dependency in their build.gradle
. Hence here what I did to modify the .pom
file while publishing my aar
.
我正在'maven-publish'
为我的 androidaar
依赖项使用插件将它发布到我自己的私有 Maven 存储库中。当我的其他项目aar
在他们的build.gradle
. 因此,在这里我.pom
在发布我的aar
.
An important thing to note here that, the dependencies which you want to have the transitive behavior should be imported using the api
in your library project's build.gradle
file like the following.
这里需要注意的重要一点是,您希望具有传递行为的依赖项应使用api
库项目build.gradle
文件中的导入,如下所示。
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
api 'com.android.volley:volley:1.0.0'
api "com.google.code.gson:gson:$globalGsonVersion"
}
Now as I said earlier, I was using maven-publish
plugin to publish the aar
dependency and hence my publishing task in the gradle looks like the following.
现在正如我之前所说,我使用maven-publish
插件来发布aar
依赖项,因此我在 gradle 中的发布任务如下所示。
publishing {
publications {
mavenAar(MavenPublication) {
from components.android
}
mavenJava(MavenPublication) {
pom.withXml {
def dependenciesNode = asNode().appendNode('dependencies')
// Iterate over the api dependencies (we don't want the test ones), adding a <dependency> node for each
configurations.api.allDependencies.each {
def dependencyNode = dependenciesNode.appendNode('dependency')
dependencyNode.appendNode('groupId', it.group)
dependencyNode.appendNode('artifactId', it.name)
dependencyNode.appendNode('version', it.version)
}
}
}
}
repositories {
maven {
// Your repository information goes here
}
}
}
Hence, I used another mavenJava
task to publish the .pom
file in my private maven repo so that when the aar
file is added as a dependency to some other module, it gets the .pom
information and download the transitive dependency.
因此,我使用另一个mavenJava
任务.pom
在我的私有 Maven 存储库中发布文件,以便当aar
文件作为依赖项添加到其他模块时,它获取.pom
信息并下载传递依赖项。
To complete the answer, this is how you should add the dependency in the build.gradle
file for your own published aar
to me imported.
要完成答案,您应该如何在build.gradle
文件中添加依赖项,以便您自己发布aar
给我导入。
api('com.example.masudias:my_lib:1.0.0@aar') {
transitive = true
}
回答by yoAlex5
transitive
means that the consumer includes a producer and all producer's dependencies. It increase build time and can create some issues with dependency versions
transitive
意味着消费者包括一个生产者和所有生产者的依赖。它会增加构建时间,并且可能会导致依赖版本出现一些问题
By default, Gradle dependency has transitive = true
默认情况下,Gradle 依赖项有 transitive = true
api ('com.package:library:0.0.1')
//the same
api ('com.package:library:0.0.1') {
transitive = true
}
When you use @artifact notation
it has transitive = false
当你使用@artifact notation
它时有transitive = false
api ('com.package:library:0.0.1@aar')
//the same
api ('com.package:library:0.0.1@aar') {
transitive = false
}