mko*_*zun 84 java android gradle android-library aar
我已经调查了一段时间,可能看到这里最流行的答案与aar和transitive依赖关系有关,但不知何故,我仍然不清楚如何使这个工作.
所以:
我有给定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'
}
Run Code Online (Sandbox Code Playgroud)
然后我将它部署到本地maven repo 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>
Run Code Online (Sandbox Code Playgroud)
最后使用上面的库作为依赖项来我的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'
}
Run Code Online (Sandbox Code Playgroud)
在手机上部署应用程序后,我正在获取NoClassDefFoundError属于我的android库的编译依赖项的类.
使用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
Run Code Online (Sandbox Code Playgroud)
根据上面的树,没有检测到所有传递依赖性.问题在哪里以及如何正确完成?
mko*_*zun 85
我通过transitive为我的aar依赖设置属性来解决我的问题:
compile ('com.somepackage:LIBRARY_NAME:1.0.0@aar'){
transitive=true
}
Run Code Online (Sandbox Code Playgroud)
小智 18
你不应该使用"@aar",如果使用"@"成为" Artifact only notation ",如果你想使用"@"并希望有依赖传递,你应该添加"transitive = true"
suh*_*_sm 14
如果您在本地使用aar,请尝试此操作:
compile(project(:your-library-name)) {
transitive=true
}
Run Code Online (Sandbox Code Playgroud)
我遇到了类似的问题,觉得我可以分享解决问题的步骤。
在发布自己的依赖项时无法使用传递依赖项的基本思想aar实际上是没有.pom使用预期的传递依赖项生成文件。
我正在'maven-publish'为我的 androidaar依赖项使用插件将它发布到我自己的私有 Maven 存储库中。当我的其他项目aar在他们的build.gradle. 因此,在这里我.pom在发布我的aar.
这里需要注意的重要一点是,您希望具有传递行为的依赖项应使用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"
}
Run Code Online (Sandbox Code Playgroud)
现在正如我之前所说,我使用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
}
}
}
Run Code Online (Sandbox Code Playgroud)
因此,我使用另一个mavenJava任务.pom在我的私有 Maven 存储库中发布文件,以便当aar文件作为依赖项添加到其他模块时,它获取.pom信息并下载传递依赖项。
要完成答案,您应该如何在build.gradle文件中添加依赖项,以便您自己发布aar给我导入。
api('com.example.masudias:my_lib:1.0.0@aar') {
transitive = true
}
Run Code Online (Sandbox Code Playgroud)
传递依赖
transitive意味着消费者(例如应用程序)包括一个生产者和所有生产者的依赖项(例如库)。它会增加构建时间,并且可能会导致依赖版本出现一些问题
默认情况下,Gradle 依赖项有 transitive = true
api ('com.package:library:0.0.1')
//the same
api ('com.package:library:0.0.1') {
transitive = true
}
Run Code Online (Sandbox Code Playgroud)
当你使用@artifact notation它时有transitive = false
api ('com.package:library:0.0.1@aar')
//the same
api ('com.package:library:0.0.1@aar') {
transitive = false
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
38602 次 |
| 最近记录: |