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

前端之家收集整理的这篇文章主要介绍了Android库 – 使用Gradle将多个变体发布到本地Maven存储库前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用 Android Gradle插件0.13.2,Android Studio 0.8.11,Gradle 2.1和maven插件.

我想用一个命令(任务)将我的Android库的多个变种(风味构建类型)安装到本地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存储库.

解决方法

为了解决这个问题,我必须为每个变体创建一个上载任务,并使它们相互依赖,并依赖于启动该过程的主任务.
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

猜你在找的Android相关文章