2015-12-17 133 views
28

我已经为我的项目添加了一些flavors(或productFlavors)(如果需要)。将Android库(aar)发布到Bintray并提供所选的口味

事实是,当我发布图书馆到bintray,所有口味上传(这是伟大的),但我无法使用它们。使用的插件是官方的here

上传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被命名为classifier,看到doc chapter 23.4.1.3

我正在寻找一种解决方案来选择我想上传哪些口味。

我已经看了bintray例子(herehere)和this,也与其他的例子,但我仍然坚持。

这里是我当前的脚本:

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:[email protected]') { 
    transitive = true; 
} 
+0

你必须更新每个风味不同的神器。 –

+0

@GabrieleMariotti你怎么能在bintray'配置'中指定味道? –

+0

我没有尝试过。但是你必须指定flavor块中的某些部分的bintray配置来分配工件名称。 –

回答

8

我面临同样的挑战,这里是最好的,我可以让尚未:

使用mavenPublications和沿bintray插件gradle这个maven-publish插件,你可以发布任何变种mavenLocal和bintray。

这里的publish.gradle文件我申请我所有的项目库模块的最后,我要发布:

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 '[email protected]' 
     } 
    } 
    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_userbintray_api_key性能。我个人只是有他们在我的~/.gradle/gradle.properties文件是这样的:

bintray_user=my_bintray_user_name 
bintray_api_key=my_private_bintray_api_key 

我还需要定义我在publish.gradle文件中使用我的根项目的build.gradle文件中的以下扩展属性:

allprojects { 
    ... 
    ext { 
     ... 
     // Libraries 
     groupId = "xyz.louiscad.splitties" 
     libraryVersion = "1.2.1" 
     siteUrl = 'https://github.com/LouisCAD/Splitties' 
     gitUrl = 'https://github.com/LouisCAD/Splitties.git' 
    } 
} 

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

plugins { 
    id "com.jfrog.bintray" version "1.7.3" // Enables publishing to bintray 
    id "com.github.dcendents.android-maven" version "1.5" // Allows aar in mavenPublications 
} 

apply plugin: 'com.android.library' 
apply plugin: 'maven-publish' // Used for mavenPublications 

android { 
    ... 
    defaultPublishConfig "myLibraryDebug" // Allows using this library in another 
    // module in this project without publishing to mavenLocal or Bintray. 
    // Useful for debug purposes, or for your library's sample app. 
    defaultConfig { 
     ... 
     versionName libraryVersion 
     ... 
    } 
    ... 
    productFlavors { 
     myLibrary 
     myLibrary_logged // Here, the "_" will be replaced "-" in artifactId when publishing. 
     myOtherLibraryFlavor 
    } 
    ... 
} 

dependencies { 
    ... 
    // Timber, a log utility. 
    myLibrary_loggedCompile "com.jakewharton.timber:timber:${timberVersion}"; // Just an example 
} 
... 

ext { 
    libraryDesc = "Delegates for kotlin on android that check UI thread" 
} 

apply from: '../publish.gradle' // Makes this library publishable 

当你拥有所有这些设置不当,你的库的名称,而不是mine's(你可以作为一个例子使用),你可以尝试发布试图首先发布到mavenLocal的风格库的一个版本。 要做到这一点,运行这个命令:

myLibrary $ ../gradlew publishToMavenLocal 

然后,您可以尝试添加在你的应用程序的库(example here)mavenLocal,然后尝试在你的库作为依赖(artifactId的应该是味道的名字,用“_”代替“ - “)并构建它。 您也可以使用文件资源管理器(在Finder的Mac上使用cmd + shift + G访问隐藏文件夹)检查目录~/.m2并查找您的资料库。

当它的时间发布到bintray/jcenter,你只需要运行这个命令:您发布库mavenLocal,Bintray或其他Maven仓库之前

myLibrary $ ../gradlew bintrayUpload 

重要,您通常会想要使用该库的示例应用程序试用您的库。此示例应用程序应该是同一项目中的另一个模块,只需要具有项目依赖关系,该应用程序应该如下所示:compile project(':myLibrary')。但是,由于您的图书馆有多个productFlavors,因此您需要测试所有这些。不幸的是,目前不可能从样例应用程序的build.gradle文件中指定要使用的配置(除非您在库的build.gradle文件中使用publishNonDefault true,这会打破maven和bintray出版物),但您可以指定默认配置(即buildVariant)在你图书馆的模块中:defaultPublishConfig "myLibraryDebug"android关闭。您可以在Android Studio的“Build Variants”工具窗口中查看库的可用构建变体。

随意探索my library "Splitties" here如果你需要一个例子。有味的模块名为concurrency,但我也使用我的脚本处理无味的库模块,我在项目中的所有库模块上进行了全面测试。

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

+0

作为关于底部部分的注释时,才可以切换到'defaultPublishConfig'flavour1Release''''',您可以通过指定missingDimensionStrategy像missingDimensionStrategy'品牌','' XXX' missingDimensionStrategy'environment','ZZZ'' – MrTristan

3

我没有尝试,所以我会删除答案,如果它不解决不了问题。

您应该为每种风味发布不同的工件(如果您愿意,也可以构建变体)。
通过这种方式,您将拥有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 
    } 
} 

最后确保仅发布版本建立类型(为什么还要调试版本?)

+0

好吧,用这个我可以将不同的味道上传到不同的bintray软件包。但分类器仍然在这里,每个包装都有它的味道。 –

+0

对不起,但我不知道你说的是分类器。 我正在检查jcenter中的pom文件,并且应该有groupId/artifactId来标识库。 –

+0

是的,我有这个,但我有分类器也在这里,所以我在每个包上都有'androidsdk-0.0.4-lightDebug.aar'和'androidsdk-0.0.4-lightDebug.aar'。所以目前有两个问题。例如,分类器是'lightDebug'。 –

1

这听起来像你不希望在文件名中的分类器。它看起来像分类器与生成的库文件名相同。你有没有尝试给他​​们相同的文件名,但输出到不同的目录? 例如在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) 
     } 
    } 
} 
+0

,它允许我改变.aar文件名(来自'build/outputs/aar/androidsdk /',但不是由bintray插件使用的文件名,所以它没有帮助。 –

2

如果有人还是坚持了这个问题,这里是为我工作 -

比方说,你要发布的发布版本为您flavour1添加到您的build.gradle

android { 
    ... 
    defaultPublishConfig "flavour1Release" 
} 

删除publishNonDefault true如果它存在于您的gradle文件中。

bintray块中添加这种类似这样的

bintray { 
    ... 
    archivesBaseName = 'YOUR_ARTIFACT_ID' 
    ... 
} 

然后只需运行bintrayUpload任务,你会。

defaultPublishConfig必须在每次需要发布新口味时进行更改。

+0

删除'publishNonDefault true'将阻止模块在调试中用于其他模块,这是一个好主意,但并不完全解决问题 –

+0

是的,如果你想在调试中使用库模块,你将不得不使用'defaultPublishConfig“flavour1Debug”'。只有当你必须发布到bintray – k1slay

5

的设置:

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过滤器)的修复:

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 
    } 
    } 
} 
相关问题