Android库 - 使用Gradle将多个变体发布到本地Maven存储库

时间:2014-09-30 17:16:32

标签: android maven gradle publish android-library

我使用的是Android Gradle插件0.13.2,Android Studio 0.8.11,Gradle 2.1和maven插件。

我想使用一个命令(任务)将我的Android库的多个变体(flavor + build类型)安装到本地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存储库的任务。

1 个答案:

答案 0 :(得分:11)

要解决此问题,我必须为每个变体创建一个上载任务,并使它们相互依赖,并依赖于启动该过程的主任务。

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