使用选定的风格将Android库(aar)发布到Bintray

使用选定的风格将Android库(aar)发布到Bintray,android,android-gradle-plugin,android-productflavors,bintray,Android,Android Gradle Plugin,Android Productflavors,Bintray,我刚刚在我的项目中添加了一些口味(或者产品口味,如果你愿意的话) 事实上,当我将库发布到bintray时,所有的风格都会被上传(这很好),但我无法使用它们。使用的插件是官方的 上传的aar: androidsdk-0.0.4-fullRelease.aar androidsdk-0.0.4-fullDebug.aar androidsdk-0.0.4-lightRelease.aar androidsdk-0.0.4-lightDebug.aar 如您所述,fullRelease被命名

我刚刚在我的项目中添加了一些
口味
(或者
产品口味
,如果你愿意的话)

事实上,当我将库发布到bintray时,所有的风格都会被上传(这很好),但我无法使用它们。使用的插件是官方的

上传的aar:

 androidsdk-0.0.4-fullRelease.aar
 androidsdk-0.0.4-fullDebug.aar
 androidsdk-0.0.4-lightRelease.aar
 androidsdk-0.0.4-lightDebug.aar
如您所述,
fullRelease
被命名为
分类器
,请参阅

我正在搜索解决方案,以选择要上传的口味。

我已经看过了bintray的例子(和),还有其他的例子,但我还是被卡住了

以下是我当前的脚本:

apply plugin: 'com.android.library'
apply plugin: 'com.github.dcendents.android-maven'
apply plugin: 'com.jfrog.bintray'

buildscript {
    repositories {
        jcenter()
    }
}

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.1"

    defaultConfig {
        minSdkVersion 9
        targetSdkVersion 23
        versionCode 64
        versionName "0.0.4"
    }

    publishNonDefault true

    productFlavors {
        full {
        }
        light {
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:23.1.1'
    compile 'com.android.support:recyclerview-v7:23.1.1'
    fullCompile 'com.squareup.picasso:picasso:2.5.0'
}

version = android.defaultConfig.versionName

uploadArchives {
    repositories.mavenDeployer {
        pom.project {

            packaging 'aar'

        }
    }
}

////////////////////////////////
// Bintray Upload configuration

Properties properties = new Properties()
properties.load(project.rootProject.file('local.properties').newDataInputStream())

bintray {
    user = properties.getProperty("bintray.user")
    key = properties.getProperty("bintray.apikey")

    configurations = ['archives']
    pkg {
        repo = "MyRepo" // repo name
        userOrg = 'hugo'
        name = "AndroidSDK" // Package name
        websiteUrl = siteUrl
        vcsUrl = gitUrl
        publish = true
    }
}
要导入我当前使用的库,请执行以下操作:

compile ('com.example.lib:sdk:0.0.8:fullRelease@aar') {
    transitive = true;
}

我没有尝试过,因此如果无法解决问题,我将删除答案

您应该为每种口味发布不同的工件(或构建变体,如果您愿意)。
通过这种方式,您将在jcenter x中拥有工件,每个工件都有一个pom文件

比如:

groupId
|--library-full
|----.pom
|----.aar
|--library-light
|----.pom
|----.aar
在顶级文件中,您可以定义

allprojects {
    repositories {
        jcenter()
    }

    project.ext {
        groupId="xxx" 
        libraryName = ""
        ......
    }
}
然后在库模块中:

productFlavors {
        full {
            project.ext.set("libraryName", "library-full");
        }
        light {
            project.ext.set("libraryName", "library-light");
        }
}

bintray {

    //...
    pkg {
        //...Do the same for other variables
        name = project.ext.libraryName
    }
}

最后,确保只发布发布版本构建类型(为什么还要发布调试版本?

听起来您不希望在文件名中使用分类器。看起来分类器与生成的库文件名相同。您是否尝试过给它们相同的文件名,但将它们输出到不同的目录? 例如,在android范围内:

libraryVariants.all { variant ->
    variant.outputs.each { output ->
        def outputFile = output.outputFile
        if (outputFile != null && outputFile.name.endsWith('.aar')) {
            def fileName = "same_name-${version}.aar"
            output.outputFile = new File(outputFile.parent+"/${archivesBaseName}", fileName)
        }
    }
}

如果有人仍然被这个问题困扰,这就是对我有效的方法-

假设您想发布您的flavour1的发布版本,请将其添加到您的
build.gradle

android {
    ...
    defaultPublishConfig "flavour1Release"
}
如果gradle文件中存在,请删除
PublishNodefault true

像这样将其添加到
bintray
块中

bintray {
    ...
    archivesBaseName = 'YOUR_ARTIFACT_ID'
    ...
}
然后,只需按照您的意愿运行
bintrayUpload
任务


每次您需要发布新的版本时,都必须更改
defaultPublishConfig

我也面临同样的挑战,以下是我能做的最好的:

使用
mavenPublications
和gradle
maven publish
插件以及bintray插件,您可以将任何变体发布到mavenLocal和bintray

以下是我要发布的所有项目库模块末尾应用的文件:

def pomConfig = {
    licenses {
        license {
            name 'The Apache Software License, Version 2.0'
            url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
        }
    }
    developers {
        developer {
            id 'louiscad'
            name 'Louis CAD'
            email 'louis.cognault@gmail.com'
        }
    }
    scm {
        connection 'https://github.com/LouisCAD/Splitties.git'
        developerConnection 'https://github.com/LouisCAD/Splitties.git'
        url siteUrl
    }
}

def publicationNames = []
publishing.publications {
    android.libraryVariants.all { variant ->
        if (variant.buildType.name == "debug") return // Prevents publishing debug library

        def flavored = !variant.flavorName.isEmpty()

        /**
         * Translates "_" in flavor names to "-" for artifactIds, because "-" in flavor name is an
         * illegal character, but is well used in artifactId names.
         */
        def variantArtifactId = flavored ? variant.flavorName.replace('_', '-') : project.name

        /**
         * If the javadoc destinationDir wasn't changed per flavor, the libraryVariants would
         * overwrite the javaDoc as all variants would write in the same directory
         * before the last javadoc jar would have been built, which would cause the last javadoc
         * jar to include classes from other flavors that it doesn't include.
         *
         * Yes, tricky.
         *
         * Note that "${buildDir}/docs/javadoc" is the default javadoc destinationDir.
         */
        def javaDocDestDir = file("${buildDir}/docs/javadoc ${flavored ? variantArtifactId : ""}")

        /**
         * Includes
         */
        def sourceDirs = variant.sourceSets.collect {
            it.javaDirectories // Also includes kotlin sources if any.
        }
        def javadoc = task("${variant.name}Javadoc", type: Javadoc) {
            description "Generates Javadoc for ${variant.name}."
            source = variant.javaCompile.source // Yes, javaCompile is deprecated,
            // but I didn't find any working alternative. Please, tweet @Louis_CAD if you find one.
            destinationDir = javaDocDestDir
            classpath += files(android.getBootClasspath().join(File.pathSeparator))
            classpath += files(configurations.compile)
            options.links("http://docs.oracle.com/javase/7/docs/api/");
            options.links("http://d.android.com/reference/");
            exclude '**/BuildConfig.java'
            exclude '**/R.java'
            failOnError false
        }
        def javadocJar = task("${variant.name}JavadocJar", type: Jar, dependsOn: javadoc) {
            description "Puts Javadoc for ${variant.name} in a jar."
            classifier = 'javadoc'
            from javadoc.destinationDir
        }
        def sourcesJar = task("${variant.name}SourcesJar", type: Jar) {
            description "Puts sources for ${variant.name} in a jar."
            from sourceDirs
            classifier = 'sources'
        }

        def publicationName = "splitties${variant.name.capitalize()}Library"
        publicationNames.add(publicationName)

        "$publicationName"(MavenPublication) {
            artifactId variantArtifactId
            group groupId
            version libraryVersion

            artifact variant.outputs[0].packageLibrary // This is the aar library
            artifact sourcesJar
            artifact javadocJar

            pom {
                packaging 'aar'
                withXml {
                    def root = asNode()
                    root.appendNode("name", 'Splitties')
                    root.appendNode("url", siteUrl)
                    root.children().last() + pomConfig
                    def depsNode = root["dependencies"][0] ?: root.appendNode("dependencies")

                    def addDep = {
                        if (it.group == null) return // Avoid empty dependency nodes
                        def dependencyNode = depsNode.appendNode('dependency')
                        dependencyNode.appendNode('groupId', it.group)
                        dependencyNode.appendNode('artifactId', it.name)
                        dependencyNode.appendNode('version', it.version)
                        if (it.hasProperty('optional') && it.optional) {
                            dependencyNode.appendNode('optional', 'true')
                        }
                    }

                    // Add deps that everyone has
                    configurations.compile.allDependencies.each addDep
                    // Add flavor specific deps
                    if (flavored) {
                        configurations["${variant.flavorName}Compile"].allDependencies.each addDep
                    }
                    // NOTE: This library doesn't use builtTypes specific dependencies, so no need to add them.
                }
            }
        }
    }
}

group = groupId
version = libraryVersion

afterEvaluate {
    bintray {
        user = bintray_user
        key = bintray_api_key
        publications = publicationNames

        override = true
        pkg {
            repo = 'splitties'
            name = project.name
            desc = libraryDesc
            websiteUrl = siteUrl
            issueTrackerUrl = 'https://github.com/LouisCAD/Splitties/issues'
            vcsUrl = gitUrl
            licenses = ['Apache-2.0']
            labels = ['aar', 'android']
            publicDownloadNumbers = true
            githubRepo = 'LouisCAD/Splitties'
        }
    }
}
为了使其工作,我需要定义
bintray\u用户
bintray\u api\u键
属性。我个人只在我的
~/.gradle/gradle.properties
文件中有它们,如下所示:

bintray_user=my_bintray_user_name
bintray_api_key=my_private_bintray_api_key
我还需要定义我在根项目文件的文件中使用的以下ext属性:

现在,我终于可以在我的android库模块中使用它了,在那里我有多个
productFlavors
。以下是可发布库模块文件的一个片段:

当您正确地完成了所有这些设置后,可以使用库的名称而不是(您可以使用它作为示例),通过尝试首先发布到mavenLocal来尝试发布您的风味库的版本。 要执行此操作,请运行以下命令:

myLibrary $ ../gradlew publishToMavenLocal
myLibrary $ ../gradlew bintrayUpload
然后,您可以尝试在应用程序的存储库中添加
mavenLocal
,并尝试将库作为依赖项添加(artifactId应该是风格名称,用“-”替换“uux”)并构建它。 您还可以使用文件资源管理器(在Mac in Finder中使用cmd+shift+G访问隐藏文件夹)检查目录
~/.m2
,并查找您的库

发布到bintray/jcenter时,只需运行以下命令:

myLibrary $ ../gradlew publishToMavenLocal
myLibrary $ ../gradlew bintrayUpload
重要提示:

在将库发布到mavenLocal、Bintray或其他maven存储库之前,您通常需要针对使用库的示例应用程序尝试库。这个示例应用程序(应该是同一项目中的另一个模块)只需要具有项目依赖关系,它应该如下所示:
compileproject(“:myLibrary”)
。但是,由于您的库有多个productFlavors,您需要测试所有这些产品。不幸的是,目前无法从示例应用程序的
build.gradle
文件中指定要使用的配置(除非在库的
build.gradle
文件中使用
publishNonDefault true
,该文件会中断maven和bintray发布),但可以指定默认配置(即buildVariant)在库的模块中,例如:
android
闭包中的
defaultPublishConfig“myLibraryDebug”
。您可以在android Studio中的“buildvariants”工具窗口中查看库的可用构建变量

如果您需要示例,请随意探索。有味道的模块名为
并发
,但我也使用我的脚本来创建无味道的库模块,并且我在项目中的所有库模块上对其进行了全面测试

如果您需要帮助设置,可以联系我。

设置:

buildTypes {
  debug {
  }
  release {
  }
}

publishNonDefault true
修复方法:

defaultPublishConfig 'release'

// Fix for defaultPublishConfig not working as expected
// ref: https://github.com/dcendents/android-maven-gradle-plugin/issues/11
libraryVariants.all { variant ->
  if( publishNonDefault && variant.name == defaultPublishConfig ) {
    def bundleTask = tasks["bundle${variant.name.capitalize()}"]
    artifacts {
      archives(bundleTask.archivePath) {
        classifier null //necessary to get rid of the suffix in the artifact
        builtBy bundleTask
        name name.replace('-' + variant.name, '')//necessary to get rid of the suffix from the folder name
      }
    }
  }
}
此修复程序仍将发布所有工件,但它将发布一个没有味道后缀的默认工件,这足以使所有工件都工作

仅上传默认工件的修复程序如下(如果bintray插件知道POM过滤器是什么):


您必须将每个味道更新为不同的工件。@GabrieleMariotti如何在bintray
配置中指定味道?我以前没有尝试过。但是您必须在flavor块中指定bintray配置的某些部分以分配工件名称。好的,通过此操作,我可以将不同的味道上载到d不同的bintray包。但是分类器仍然在这里,每个包都有所有的味道。抱歉,我不知道您使用分类器指的是什么。我正在检查jcenter中的pom文件,您应该有groupId/artifactId来标识库。是的,我有这个,但分类器也在这里,所以我有这两个
install {
  repositories.mavenInstaller {
    /*
    POM filters can be used to block artifacts from certain build variants.

    However, Bintray does not respect POM filters, therefore this only works for maven deploy plugin.
    Also, bintray crashes with named filters, since it always expects a /build/pom/pom-default.xml,
  which does not happen with named filters.
    */
    filter { artifact, file ->
      // this how the default classifier is identified in case the defaultPublishConfig fix is applied
      artifact.attributes.classifier == null
    }
  }
}