Android库 – 使用Gradle将多个变体发布到本地Maven仓库

我正在使用Android Gradle插件0.13.2,Android Studio 0.8.11,Gradle 2.1和maven插件。

我想用一个命令(任务)将我的Android库的多个变体(flavor + build type)安装到本地Maven仓库。

目前,Android Gradle插件0.13.2允许我将publishNonDefault标志设置为发布所有变体,但是正如文档所述,它将使用与Maven Repository不兼容的分类器发布变体。

我现在的解决方法是使用defaultPublishConfig“myVariant”并更改我拥有的每个变体。

apply plugin: 'com.android.library' apply plugin: 'maven' android { defaultPublishConfig "myVariant" . . . } task installArchives(type: Upload) { repositories.mavenInstaller { configuration = configurations.getByName(Dependency.DEFAULT_CONFIGURATION) pom.groupId = "com.company" pom.artifactId = "mylibrary" pom.version = "1.0.0-myVariant" } } 

我希望有一个任务能够将所有变体正确地发布到本地Maven仓库。

为了解决这个问题,我不得不为每个变体创build一个上传任务,并使它们彼此依赖,并在启动过程的主任务上相互依赖。

 apply plugin: 'com.android.library' apply plugin: 'maven' android { . . . } // Master task that will publish all variants def DefaultTask masterTask = project.tasks.create("installArchives", DefaultTask) android.libraryVariants.all { variant -> variant.outputs.each { output -> // Configuration defines which artifacts will be published, create one configuration for each variant output (artifact) def Configuration variantConfiguration = project.configurations.create("${variant.name}Archives") project.artifacts.add(variantConfiguration.name, output.packageLibrary) // Create one Upload type task for each configuration def Upload variantTask = project.tasks.create("${variant.name}Install", Upload) variantTask.configuration = variantConfiguration variantTask.repositories.mavenInstaller { pom.groupId = "com.yourcompany" pom.artifactId = "yourLibrary" pom.version = "1.0.0-${variant.name}" //Give a different version for each variant pom.packaging = "aar" } // Make all tasks depend on each other and on master task masterTask.dependsOn variantTask masterTask = variantTask } } 

任务installArchives将把所有的变体发布到本地的Maven仓库。

 ./gradlew installArchives