简体   繁体   English

如何使用 Android Gradle 插件 3.0.0 将所有风味变体发布到 maven?

[英]How to publish all flavor variants to maven with Android Gradle plugin 3.0.0?

Earlier I have used android gradle plugin v2.2.2, now I am migrating to 3.0.之前我用过android gradle plugin v2.2.2,现在我正在迁移到3.0。 The below code publishes( uploadArchives task) all my variants to maven without any issues with 2.14.1.下面的代码将我的所有变体发布( uploadArchives任务)到 Maven,而 2.14.1 没有任何问题。 But as soon I upgrade to 3.0 nothing is been published.但是一旦我升级到 3.0,就没有发布任何内容。 If I remove the prodcutFlavors , it publishes the release version of the library to maven properly.如果我删除prodcutFlavors ,它会将库的发布版本正确发布到 maven。 Am I missing something?我错过了什么吗? Is something changed in 3.0? 3.0 有什么变化吗?

I have read that by default now all variants will be published and publishNonDefault is no more needed.我已经读过,默认情况下,现在将发布所有变体,并且不再需要 publishNonDefault。 After removing publishNonDefault also it does not work.删除publishNonDefault后它也不起作用。

I am using Gradle 4.1 with plugin 3.0 & Gradle 2.14.1 with plugin 2.2.2我使用 Gradle 4.1 和插件 3.0 & Gradle 2.14.1 和插件 2.2.2

apply plugin: 'com.android.library'
apply plugin: 'maven'
android {
    compileSdkVersion 26

    defaultConfig {
        minSdkVersion 17
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        vectorDrawables.useSupportLibrary = true
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

    }


    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    publishNonDefault true

    flavorDimensions "app"
    productFlavors{
        apple{

        }
        orange{

        }
    }
}


dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])

    implementation 'com.android.support:appcompat-v7:26.0.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}
uploadArchives {
    repositories {
        mavenDeployer {
            repository(url: mavenLocal().url)
            pom.version = "1.3.7"
            pom.artifactId = "android"
            pom.groupId = "com.example"
        }
    }
}

I am facing a similar problem.我面临着类似的问题。 Although I don't use productFlavors , I am expecting mavenDeployer to publish three configurations (corresponding to my three build types "debug", "release", and "staging").虽然我不使用productFlavors ,但我希望mavenDeployer发布三种配置(对应于我的三种构建类型“调试”、“发布”和“暂存”)。 But it no longerd does, it only publishes an AAR to Maven for the "release" configuration.但它不再这样做,它只向 Maven 发布一个 AAR 以用于“发布”配置。

Prior to Android Plugin for Gradle 3.0.1, the following mavenDeployer code did publish all three AAR files:在 Android Plugin for Gradle 3.0.1 之前,以下mavenDeployer代码确实发布了所有三个 AAR 文件:

uploadArchives {
    repositories.mavenDeployer {
        repository(url:"file://" + "${project.buildDir}/maven/releases")

        pom.groupId = "com.example"
        pom.artifactId = "sdk"
        pom.version = sdkName
    }
}

The Gradle code above used to produce the following 3 files published to Maven:上面的 Gradle 代码用于生成以下 3 个发布到 Maven 的文件:

sdk/build/maven/releases/com/locuslabs/sdk/develop/sdk-develop-debug.aar 
sdk/build/maven/releases/com/locuslabs/sdk/develop/sdk-develop-staging.aar 
sdk/build/maven/releases/com/locuslabs/sdk/develop/sdk-develop-release.aar

However, after upgrading to Android Plugin for Gradle 3.0.1, it only produces:但是,升级到 Android Plugin for Gradle 3.0.1 后,它只会产生:

sdk/build/maven/releases/com/locuslabs/sdk/develop/sdk-develop.aar

Notice that there isn't even a suffix of -release on the file name even though I'm pretty sure it's the release one.请注意,文件名中甚至没有-release后缀,即使我很确定它是发布版。

So far the closest I've come to a solution is documentation for a newer way to publish AAR files to Maven: android-maven-publish .到目前为止,我最接近解决方案的是将 AAR 文件发布到 Maven 的更新方法的文档: android-maven-publish

Background research:背景研究:

From Android Libraries: maven vs maven-publish gradle project I learned that there are two Gradle mechanisms for publishing to Gradle.Android 库:maven vs maven-publish gradle 项目我了解到有两种 Gradle 机制可以发布到 Gradle。 Plus there are unofficial Android implementations of those.此外,还有这些的非官方 Android 实现。

The Maven Plugin: Multiple Artifacts per Project Maven 插件:每个项目有多个工件

An old one which I think you're using because you've specified apply plugin: 'maven' which is documented here The Maven Plugin .我认为您正在使用的旧版本是因为您已指定apply plugin: 'maven'此处记录在The Maven Plugin 中 To get multiple AAR files output, presumably you would "need need to declare a filter for each artifact you want to publish."要获得多个 AAR 文件输出,大概您将“需要为要发布的每个工件声明一个过滤器”。 but the drawback is "you need to generate multiple POMs" ( Multiple Artifacts per Project ).但缺点是“您需要生成多个 POM”( 每个项目的多个工件)。

Maven Publishing (new): Publishing multiple modules Maven 发布(新):发布多个模块

The apparent replacement for The Maven Plugin is a new one,Maven Publishing (new) , which seems to have the ability to publish multiple artifacts as described in Publishing multiple modules . Maven Plugin的明显替代品是一个新的Maven Publishing (new) ,它似乎能够发布多个工件,如发布多个模块中所述 Basically it seems like you just need to have multiple method calls in the publications block (sorry if I'm using the wrong Gradle/Groovy syntax--it's a confusing language that I wish was never created).基本上,您似乎只需要在publications块中进行多个方法调用(抱歉,如果我使用了错误的 Gradle/Groovy 语法——我希望从未创建过这种令人困惑的语言)。

publishing {
    publications {
        impl(MavenPublication) {               // for first variant?
            groupId 'org.gradle.sample.impl'
            artifactId 'project2-impl'
            version '2.3'

            from components.java
        }
        api(MavenPublication) {                // for second variant?
            groupId 'org.gradle.sample'
            artifactId 'project2-api'
            version '2'

            artifact apiJar
        }
    }
}

task apiJar(type: Jar) {
    baseName "publishing-api"
    from sourceSets.main.output
    exclude '**/impl/**'
}

If that weren't complicated enough, according to Android Libraries: maven vs maven-publish gradle project , "The above two official plugins don't fully support Android, so there are two community ports."如果这还不够复杂,根据Android Libraries: maven vs maven-publish gradle project ,“上述两个官方插件并不完全支持 Android,因此有两个社区端口。”

Gradle Android Maven plugin (unofficial) Gradle Android Maven 插件(非官方)

The documentation at Gradle Android Maven plugin doesn't explain how we would output multiple AARs. Gradle Android Maven 插件的文档没有解释我们将如何输出多个 AAR。

android-maven-publish (unofficial) android-maven-publish(非官方)

As mentioned above, the documentation at android-maven-publish looks the most promising because it provides an example to "publish custom variants" which implies that it would solve your problem of publishing all flavor variants.如上所述, android-maven-publish 上的文档看起来最有前途,因为它提供了一个“发布自定义变体”的示例,这意味着它将解决您发布所有风味变体的问题。 Here's the example from android-maven-publish :这是来自android-maven-publish的示例:

publishing {
    publications {
        android.libraryVariants.all { variant ->

            "maven${variant.name.capitalize()}Aar"(MavenPublication) {
                from components.findByName("android${variant.name.capitalize()}")
                groupId 'digital.wup.test-publish'
                artifactId 'test-publish'
                version "1.0.0-${variant.name}"
            }
        }
    }
}

TheMaven Publish plugin for Android is indeed the way to do this, as mentioned by htofoya .正如htofoya所提到的,适用于 AndroidMaven Publish 插件确实可以做到这一点

You can either enter all the variants one by one, as shown in the docs, or you can do something like this:您可以一一输入所有变体,如文档中所示,或者您可以执行以下操作:

afterEvaluate {
    android.libraryVariants.each { variant ->
        // Only consider release
        if (variant.buildType.name != "release") { return }
        publishing.publications.create(variant.name, MavenPublication) {
            from components.findByName(variant.name)
            groupId libraryGroupId
            artifactId "${libraryArtifactId}-${variant.flavorName}"
            version libraryVersion
        }
    }
}

The example from android-maven-publish , shown in Michael Osofsky answer, is flawed and doesn't produce correct POM files Michael Osofsky回答中显示的android-maven-publish示例存在缺陷,无法生成正确的 POM 文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM