@kanglongba
2016-07-27T13:46:22.000000Z
字数 6507
阅读 2546
android library jCenter MavenCentral
我们在Android Studio中使用一些第三方库时(例如:gson),只需要在moduel的build.gradle中添加一条依赖就可以了:
dependencies {compile 'com.google.code.gson:gson:2.3.1'}
然后Android Studio就会去jCenter仓库或者Maven Central仓库,自动去寻找,并且下载这个第三方库。
而'com.google.code.gson:gson:2.3.1'就是第三方库在仓库中的地址。它的组成部分如下:
| com.google.code.gson | gson | 2.3.1 |
|---|---|---|
| groupId | artifactId | version |
所以,如果自己也有一些轮子想分享给大家使用,就需要把它传到jCenter或Maven Central中。下面就是上传的具体方法。
jCenter和Maven Central是两个标准的Maven仓库,其中jCenter是Google官方默认的中央仓库。它们分别由bintray和sonatype维护。
| 仓库名称 | 维护机构 | Android Studio中使用 |
|---|---|---|
| jCenter | bintray | jcenter() |
| maven | sonatype | mavenCentral() |
所以,如果想把library传到jCenter和Maven Central,必须要有bintray和sonatype的账号。
在project的build.gradle中添加依赖
dependencies {classpath 'com.github.dcendents:android-maven-gradle-plugin:1.4.1'}
在library的build.gradle中应用插件
apply plugin: 'com.github.dcendents.android-maven'
在project的build.gradle中添加依赖
dependencies {classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7'}
在library的build.gradle中的应用插件
apply plugin: 'com.jfrog.bintray'
project:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.1.2'
//Gradle Android Maven plugin
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.4.1'
//Gradle Bintray Plugin
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7.1'
}
}
library:
apply plugin: 'com.android.library'
//Gradle Android Maven plugin
apply plugin: 'com.github.dcendents.android-maven'
//Gradle Bintray Plugin
apply plugin: 'com.jfrog.bintray'
在第二步中,已经为library配置了Gradle Bintray Plugin插件,但是为了能够使用它,还需要对Gradle Bintray Plugin进行进一步的配置。
对Gradle Bintray Plugin的配置,在library的build.gradle中进行。
登陆 bintray
Edit Your Profile -> API Key
bintray.user = bintray_username
bintray.apikey = bintray_api_key
local.properties在工程创建时,就已经被默认加到.gitignore文件中了,不会被误传到仓库,所以通常都用它来保存账号的配置信息。
添加bintray账号
从project的local.properties文件读取
bintray {user = properties.getProperty("bintray.user")key = properties.getProperty("bintray.apikey")...}
配置要上传到Bintray中的package的信息
以下四个是package的必需项:
下面我的一个library的bintray配置信息:
bintray {...pkg {repo = 'maven'name = 'magicreddot'vcsUrl = 'https://github.com/kanglongba/MagicRedDot.git'licenses = ['Apache-2.0']userOrg = 'bintray_user' //可选项,如果作者属于一个组织,填写组织的名字;否则默认为‘BINTRAY_USER’}}
配置package的版本信息
上实例:
bintray {...pkg {...version {name = '1.0.0-Final‘desc = 'a powerful red dot widget for android'released = new Date()vcsTag = 'v1.0.0'attributes = ['gradle-plugin': 'com.use.less:com.use.less.gradle:gradle-useless-plugin']}}}
Gradle Bintray Plugin插件提供三种方式,分别是:
三种我都不懂,但是大家都用 Configurations,我也跟风这么写:
bintray {...configurations = ['archives']}
至此,对Gradle Bintray Plugin插件的配置就完成了。最后附上我的项目-MagicRedDot的bintray配置信息。
在project的local.properties中:
bintray.user = bintray_usernamebintray.apikey = bintray_api_kaybintray.gpg.password = gpg_passphase
在library的build.gradle中:
def siteUrl = 'https://github.com/kanglongba/MagicRedDot' // 项目的主页def gitUrl = 'https://github.com/kanglongba/MagicRedDot.git' // Git仓库的urlProperties 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 = "maven"name = "magicreddot" //发布到JCenter上的项目名字websiteUrl = siteUrlvcsUrl = gitUrllicenses = ["Apache-2.0"]publish = trueversion {desc = 'a powerful red dot widget for android'gpg {sign = true //Determines whether to GPG sign the files. The default is falsepassphrase = properties.getProperty("bintray.gpg.password")//Optional. The passphrase for GPG signing'}}}}
Gradle Bintray Plugin插件用来使Android Studio可以自动接入你在bintray中的指定仓库,Gradle Android Maven plugin的插件用来执行具体的打包、上传工作。它们合力将library发布到bintray仓库中。
对Gradle Android Maven plugin插件的配置全部都在library的build.gradle文件中进行,具体可以参考它的文档。
我参考了其他人的配置,觉得主要可分为两个:
对package的配置
配置groupId和version
group = 'com.bupt.edison.magicreddot'version = '1.0.0'
命名groupId时,一般由host和library name组成。com.bupt.edison就是我的host,magicreddot就是我的library name。通常情况下,artifactId也以library name命名。
artifactId也可以在这配置,在project的settings.gradle中添加:
rootProject.name = 'magicreddot'
但是,更常见的是在Gradle Bintray Plugin插件中配置。
对任务的配置
较长,直接贴代码
install {repositories.mavenInstaller {// This generates POM.xml with proper parameterspom {project {packaging 'aar'// Add your description herename 'Android Magic Dot'description 'a powerful red dot widget for android'url siteUrl// Set your licenselicenses {license {name 'The Apache Software License, Version 2.0'url 'http://www.apache.org/licenses/LICENSE-2.0.txt'}}developers {developer {id 'kanglongba' //填写bintray或者github的用户名name 'edison' //姓名,可以是中文email 'kanglongba@gmail.com'}}scm {connection gitUrldeveloperConnection gitUrlurl siteUrl}}}}}task sourcesJar(type: Jar) {from android.sourceSets.main.java.srcDirsclassifier = 'sources'}task javadoc(type: Javadoc) {source = android.sourceSets.main.java.srcDirsclasspath += project.files(android.getBootClasspath().join(File.pathSeparator))}task javadocJar(type: Jar, dependsOn: javadoc) {classifier = 'javadoc'from javadoc.destinationDir}artifacts {archives javadocJararchives sourcesJar}
上面已经把相关的配置都做好了,接下来就可以将library上传到bintray中的仓库了。
两种方式:
./gradlew install
./gradlew bintrayUpload
如果只是上传到自己的bintray仓库中,那么别人想使用的话,必须在project的build.gradle中添加你的仓库地址:
repositories {...maven {url 'https://dl.bintray.com/kanglongba/maven'}}
因此,为了更加开源,还需要把library同步到jCener中。jCenter是bintray维护的一个公共仓库。
操作很简单,在library的bintray主页直接点击Add to JCenter按钮,比如MagicRedDot:
MagicRedDot的bintray主页 -> Linked To -> Add to JCenter
接下来会弹出一个页面,什么也不用填,直接 send。
然后等待管理员审核,大概四个小时左右,library就被添加到了jCenter仓库中。这时可以访问jCenter的仓库地址,根据library的groupId和artifactId看看是否添加成功。
至此,就可以在项目中通过添加依赖的方式使用我们的库了:
dependencies {...compile 'com.bupt.edison.magicreddot:magicreddot:1.0.0'}
虽然jCenter是Android默认的中央仓库,但是Maven Central仍有很多使用者,因次最好也将library传到Maven Central仓库中。
由于已经将library上传到了jCenter中,所以可以直接从jCenter同步到Maven Central。幸运的是,同步操作非常简单,比直接上传到Maven Central简单很多。
同步操作可以分为以下四步,其中第二步和第三步只需要操作一次:
由于这部分步骤较多,请参考这篇文章的图文部分。