10

我有 2 个 lib 模块 : library, library2, 并使用android-maven-publish发布具有 multi-productFlavors 的多模块 android 项目

build.gradle在图书馆:

apply plugin: 'com.android.library'
apply plugin: 'digital.wup.android-maven-publish'

android {
    compileSdkVersion 26
    defaultConfig {
        minSdkVersion 14
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }

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

    flavorDimensions "runner"
    productFlavors {
        sit {
            dimension "runner"
        }
        dev {
            dimension "runner"
        }
        uat {
            dimension "runner"
        }
        prd {
            dimension "runner"
        }
    }

}

dependencies {
   ...
  implementation project(':android-library2')
}

apply from: '../publish.gradle'

build.gradle在图书馆2中:

apply plugin: 'com.android.library'
apply plugin: 'digital.wup.android-maven-publish'

android {
    compileSdkVersion 26
    defaultConfig {
        minSdkVersion 14
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }

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

    flavorDimensions "runner"
    productFlavors {
        sit {
            dimension "runner"
        }
        dev {
            dimension "runner"
        }
        uat {
            dimension "runner"
        }
        prd {
            dimension "runner"
        }
    }

}

dependencies {
   ...
}

apply from: '../publish.gradle'

发布.gradle:

publishing.publications() {
    android.libraryVariants.all { variant ->
        "maven$project.archivesBaseName${variant.name.capitalize()}Aar"(MavenPublication) {
            from components.findByName("android${variant.name.capitalize()}")
            groupId 'com.android.saleshelp'
            artifactId project.archivesBaseName + "-${variant.name.capitalize()}"
            version android.defaultConfig.versionName
        }
    }
}

publishing.repositories {
    mavenLocal()
}

当我在库中运行任务publish时,出现以下错误:

* What went wrong:
Could not determine the dependencies of task ':android-library2:publishMavenandroid-library2SitReleaseAarPublicationToMavenLocalRepository'.
> Publishing is not able to resolve a dependency on a project with multiple publications that have different coordinates.
  Found the following publications in project ':android-library':
    - Maven publication 'mavenandroid-libraryPrdDebugAar' with coordinates com.cestbon.android.saleshelp:android-library-PrdDebug:1.0
    - Maven publication 'mavenandroid-libraryPrdReleaseAar' with coordinates com.cestbon.android.saleshelp:android-library-PrdRelease:1.0
    - Maven publication 'mavenandroid-libraryDevDebugAar' with coordinates com.cestbon.android.saleshelp:android-library-DevDebug:1.0
    - Maven publication 'mavenandroid-libraryDevReleaseAar' with coordinates com.cestbon.android.saleshelp:android-library-DevRelease:1.0
    - Maven publication 'mavenandroid-libraryUatDebugAar' with coordinates com.cestbon.android.saleshelp:android-library-UatDebug:1.0
    - Maven publication 'mavenandroid-libraryUatReleaseAar' with coordinates com.cestbon.android.saleshelp:android-library-UatRelease:1.0
    - Maven publication 'mavenandroid-librarySitDebugAar' with coordinates com.cestbon.android.saleshelp:android-library-SitDebug:1.0
    - Maven publication 'mavenandroid-librarySitReleaseAar' with coordinates com.cestbon.android.saleshelp:android-library-SitRelease:1.0

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

如果我apply from: '../publish.gradle'build.gradlelibrary2 中删除,任务:library:publish可以成功运行。但因为我要library2独立发表,所以library2必须apply from: '../publish.gradle'

我该怎么办这个错误?谢谢。

环境:gradle 4.4 ~ 4.9-rc1

4

3 回答 3

7

这是maven-publish插件的一个已知限制。

当一个项目依赖有多个发布时,maven-publish插件无法正确选择它们。在您的情况下,当maven-publish插件尝试解析implementation project(':android-library2')时,它无法决定将什么写入 pom 文件,因为该android-library2项目有多个具有不同坐标的出版物。

在 Gradle 4.9-rc-1 中引入了别名属性。如果您将所有出版物标记为别名并且您有一个主要出版物,那么这可以解决您的问题。但在我看来,这并不能完全解决您的问题,因为 maven 发布插件会将主要出版物的坐标写入 pom 文件,而与产品风格无关。

如果这没有帮助,那么您可以使用原始错误报告中的解决方法。

于 2018-07-16T20:23:49.793 回答
4

我遇到了同样的问题,这似乎仍然是maven-publish. 我找到了一个对我来说非常有效的解决方法:

publishing {
  repositories {
    def repositoryName = 'X'
    maven { name = repositoryName /* ... */ }
    
    afterEvaluate {
      def targetComponent = System.getProperty("targetComponent")
      components.each { component ->
        if (component.name != targetComponent) return

        def appendage = component.name.replaceAll("[Rr]elease", "")
        if (!appendage.isEmpty()) appendage = "-$appendage"

        publications {
          "${component.name}"(MavenPublication) {
            from component
            artifactId = project.name + appendage
          }
        }

        publish.doFirst {
          if (targetComponent == null)
            throw new GradleException("Deployment requires specifying -DtargetComponent option")
        }
      }
    }
  }
}

gradle project:publish -DtargetComponent=release使用或您要发布的任何其他组件调用。

说明: Gradle 在该级别看到工作空间内的依赖关系project,但它在该级别输出到 Maven 存储库component。Maven 没有相同的项目与组件概念。这也意味着您无法在 Gradledependencies块中指定您正在寻找的 Maven 样式依赖项的哪个组件(因为 Maven 并没有真正理解这个概念)。结果:Gradle 无法弄清楚如何为具有多个publications 的工作空间内依赖项编写 POM 文件条目。

最小问题示例:

// a/build.gradle:
publications {
  release(MavenPublication) { from components.release }
  debug(MavenPublication) { from components.debug; artifactId = 'a-debug' }
}

// b/build.gradle:
dependencies {
  project(':a') // ERROR: should this be 'group:b:1.0.0' or 'group:b-debug:1.0.0'?
}

解决方法是让您在配置时间之前指出要在所有项目中部署的组件。在配置期间,Gradle 仅根据您标记的组件为每个项目生成一个发布,添加 Gradle 将项目内的部门映射到 Maven 样式的部门所需的额外信息。部署时只需要指定命令行参数即可;条件绑定到publish任务,因此只有在实际调用该任务时才会关闭。

限制:

  • 假设组件信息应该保存在 Maven 中artifactId;如果你愿意,你可以改变它
  • 如果您不指定属性,gradle publish --dry-run则不会显示有意义的结果
  • Gradle 每次调用只能发布一个组件
  • 您工作区中的每个项目都必须公开名称相似的组件(不过,您可以定制此代码来解决该问题),并且它们都绑定在该选择中(即a:release绑定到b:releasea:debug绑定到b:debug

编辑:我已经调用了我的任务deploy,但publish在编写此答案时将其重命名为。结果会自动maven-publish创建一个任务publish,它完全符合我的要求。我已经更新了代码片段以反映这一点。

EDIT2:GradleException改为抛出;它更习惯用语。

于 2020-10-02T18:46:11.080 回答
0

从 Android Gradle Plugin (AGP) v7.1.0 开始,可以将库变体作为一个 Maven 出版物发布。请参阅发行说明

发行说明当前链接到错误的页面(它不包含示例)。它可能应该链接到的页面,并且有各种示例,就是这个。寻找其中一个multipleVariants例子。

于 2022-01-26T12:27:28.757 回答