我面临同样的挑战,这是我能做到的最好的:
使用mavenPublications
bintraymaven-publish
插件和 gradle 插件,您可以将任何变体发布到 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 '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_user
andbintray_api_key
属性。我个人只是将它们放在我的~/.gradle/gradle.properties
文件中,如下所示:
bintray_user=my_bintray_user_name
bintray_api_key=my_private_bintray_api_key
我还需要publish.gradle
在根项目的build.gradle
文件中定义我在文件中使用的以下 ext 属性:
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
当您正确完成所有这些设置时,使用您的库名称而不是我的名称(您可以将其用作示例),您可以尝试通过首先发布到 mavenLocal 来尝试发布您的风味库版本。为此,请运行以下命令:
myLibrary $ ../gradlew publishToMavenLocal
然后,您可以尝试添加mavenLocal
应用程序的存储库(此处为示例)并尝试将您的库添加为依赖项(artifactId 应该是风味名称,将“_”替换为“-”)并构建它。您还可以使用文件资源管理器(在 Mac 上的 Finder 中使用 cmd+shift+G 来访问隐藏文件夹)检查目录~/.m2
并查找您的库。
当需要发布到 bintray/jcenter 时,您只需运行以下命令:
myLibrary $ ../gradlew bintrayUpload
重要的:
在将库发布到 mavenLocal、Bintray 或其他 maven 存储库之前,您通常需要针对使用该库的示例应用程序尝试您的库。这个示例应用程序应该是同一个项目中的另一个模块,只需要具有项目依赖项,它应该如下所示compile project(':myLibrary')
:但是,由于您的库有多个 productFlavor,因此您需要测试所有这些。不幸的是,目前无法从示例应用程序的文件中指定要使用的配置build.gradle
(除非您publishNonDefault true
在库的build.gradle
文件中使用,这会破坏 maven 和 bintray 出版物),但您可以在库的文件中指定默认配置(即 buildVariant)这样的模块:defaultPublishConfig "myLibraryDebug"
在android
关闭。您可以在 Android Studio 的“构建变体”工具 Windows 中查看您的库的可用构建变体。
如果您需要示例,请随意探索我的图书馆“Splitties” 。风味模块名为concurrency
,但我也将我的脚本用于未风味的库模块,并且我在项目中的所有库模块上对其进行了全面测试。
如果您需要帮助为您设置它,您可以联系我。