我面临着同样的挑战,以下是我能做到的最好的方法:
使用
mavenPublications
和Gradle
maven-publish
插件以及bintray插件,您可以将任何变体发布到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
def flavored = !variant.flavorName.isEmpty()
def variantArtifactId = flavored ? variant.flavorName.replace('_', '-') : project.name
def javaDocDestDir = file("${buildDir}/docs/javadoc ${flavored ? variantArtifactId : ""}")
def sourceDirs = variant.sourceSets.collect {
it.javaDirectories
}
def javadoc = task("${variant.name}Javadoc", type: Javadoc) {
description "Generates Javadoc for ${variant.name}."
source = variant.javaCompile.source
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
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
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')
}
}
configurations.compile.allDependencies.each addDep
if (flavored) {
configurations["${variant.flavorName}Compile"].allDependencies.each addDep
}
}
}
}
}
}
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
和
bintray_api_key
属性。我个人只是将它们放在我的
~/.gradle/gradle.properties
文件中,像这样:
bintray_user=my_bintray_user_name
bintray_api_key=my_private_bintray_api_key
我还需要在我的根项目的build.gradle
文件中定义我在publish.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"
id "com.github.dcendents.android-maven" version "1.5"
}
apply plugin: 'com.android.library'
apply plugin: 'maven-publish'
android {
...
defaultPublishConfig "myLibraryDebug"
defaultConfig {
...
versionName libraryVersion
...
}
...
productFlavors {
myLibrary
myLibrary_logged
myOtherLibraryFlavor
}
...
}
dependencies {
...
myLibrary_loggedCompile "com.jakewharton.timber:timber:${timberVersion}";
}
...
ext {
libraryDesc = "Delegates for kotlin on android that check UI thread"
}
apply from: '../publish.gradle'
当您正确设置了所有内容,将库的名称替换为
我的(您可以使用它作为示例),您可以尝试通过首先发布到mavenLocal来发布您的flavored库的版本。要这样做,请运行以下命令:
myLibrary $ ../gradlew publishToMavenLocal
您可以尝试在应用程序的存储库
(例如在此处)中添加
mavenLocal
,并尝试将库添加为依赖项(artifactId 应为口味名称,并将“_”替换为“-”),然后构建它。您还可以使用文件浏览器(在 Finder 中使用 Mac 上的 cmd+shift+G 访问隐藏文件夹)检查目录
~/.m2
,并查找您的库。
当发布到 bintray/jcenter 时,您只需要运行此命令:
myLibrary $ ../gradlew bintrayUpload
重要提示:
在将您的库发布到mavenLocal、Bintray或其他maven仓库之前,通常您会想要针对使用该库的示例应用程序进行测试。这个示例应用程序应该是同一项目中的另一个模块,只需要具有项目依赖项,应该看起来像这样:compile project(':myLibrary')
。然而,由于您的库具有多个productFlavors,您将希望测试所有这些flavor。不幸的是,目前无法在示例应用程序的build.gradle
文件中指定要使用哪个配置(除非您在库的build.gradle
文件中使用publishNonDefault true
,这会破坏maven和bintray的发布),但您可以在库的模块中指定默认配置(即buildVariant)如下:defaultPublishConfig "myLibraryDebug"
在android
闭包中。您可以在Android Studio的“Build Variants”工具窗口中查看库的可用构建变体。
如果您需要示例,请随意探索这里我的库“Splitties”。该模块名为concurrency
,但我也使用它来处理未经调味的库模块,并且我已经在项目中对所有库模块进行了彻底测试。
如果您需要帮助设置,请联系我。