1

我使用以下 build.gradle 将自定义 android 库发布到我的本地 maven 存储库:

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.0.0'
    }
}

task wrapper(type: Wrapper) {
    gradleVersion = '2.2.1'
}

apply plugin: "com.android.library"
apply plugin: "maven-publish"

group = "com.example"
//name = "mylibrary"
version = "0.0.1"

repositories {
    mavenCentral()
    mavenLocal()
}

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.2"

    defaultConfig {
        versionCode 1
        versionName project.version

        minSdkVersion 14
        targetSdkVersion 21
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile "org.slf4j:slf4j-api:1.7.5"
}

publishing {
    publications {
        android(MavenPublication) {
            artifact bundleRelease

            //FIXME manually add the dependencies as gradle doesn't find them in the default configuration
            pom.withXml {
                def dependenciesNode = asNode().appendNode('dependencies')

                configurations.default.allDependencies.each {
                    def dependencyNode = dependenciesNode.appendNode('dependency')
                    dependencyNode.appendNode('groupId', it.group)
                    dependencyNode.appendNode('artifactId', it.name)
                    dependencyNode.appendNode('version', it.version)
                }
            }
        }
    }
}

运行时使用正确的内容(包括 classes.jar)和正确的 mylibrary-0.0.1.pom 创建./gradlew publishToMavenLocal文件:~/.m2/repository/com/example/mylibrary/0.0.1/mylibrary-0.0.1.aar

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example</groupId>
    <artifactId>mylibrary</artifactId>
    <version>0.0.1</version>
    <packaging>aar</packaging>
    <dependencies>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.5</version>
        </dependency>
    </dependencies>
</project>

但是,当我像在另一个项目中一样声明对该库的依赖时,该compile "com.example:mylibrary:0.0.1@aar"库及其依赖项均不包括在内,即使它似乎已找到。./gradlew dependencies产量

compile - Classpath for compiling the main sources.
\--- com.example:mylibrary:0.0.1

运行构建时,编译失败,因为我的库中的类及其依赖项中的类都找不到。

我将 Gradle 2.2.1 与 android 构建工具 1.0.0 和新的 maven-publish 机制一起使用。

注意:由于缺少库中的类,它肯定不仅仅是传递依赖的问题,所以这个解决方案不起作用。我希望从 maven 中获取该库,因此不能像此处一样在本地提供它(并且它对传递依赖项没有帮助)。

4

1 回答 1

0

没有足够的声誉发表评论...

我没有使用新的 maven-plugin,但是如果你使用android-maven-plugin部署到 bintray ,你只需要将 repo 添加到你的 build.gradle (或者你可以要求他们将它托管在 jcenter 上,并且你不必做任何事情)。然后你就可以使用transitive = true.

于 2015-02-13T23:33:08.870 回答