制作自己的依赖库的第二种方法
上一篇文章中我们介绍了制作自己的依赖库的第一种方法,出于好奇心,今天尝试了下第二种方法,也踩了一些坑,所以在这边做个笔记记录一下。
所谓的第二种方法就是将库发布到jCenter,下面我们具体看下步骤!
将库发布到jCenter的步骤
准备要发布的library
这一步直接跳过,不清楚的请上网查询或者查看上一篇文章!
注册账号
jCenter属于bintray旗下的一个仓库,所以先要注册账号:https://bintray.com,这里说下第一个坑。
一开始我点击上图的START YOUR FREE TRIAL
按钮进行了注册,结果尼玛只能在组织里创建maven
仓库,导致我上传library
到jCenter
一直失败,后来网上查了一下,发现应该点击右侧的For an Open Source Account
,这回总算可以在自己的账号里创建maven
仓库了,参考的链接为http://blog.csdn.net/tmac2000/article/details/53261141。
创建maven仓库
注册成功并登录之后,点击页面上的Add New Repository
,创建maven
仓库:
创建package
maven
仓库创建成功之后,在maven
仓库中创建一个package
,注意package
的名字和要发布的library
的名字相同。
配置项目的build.gradle文件
在项目的build.gradle
中添加bintray-release
的classpath
:classpath 'com.novoda:bintray-release:0.3.4
,如下:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.3'
classpath 'com.novoda:bintray-release:0.3.4'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
配置library的build.gradle文件
apply plugin: 'com.android.library'
apply plugin: 'com.novoda.bintray-release'
android {
compileSdkVersion 26
buildToolsVersion "26.0.1"
defaultConfig {
minSdkVersion 15
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
lintOptions {
abortOnError false
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:26.+'
testCompile 'junit:junit:4.12'
}
//新添加
publish {
userOrg = 'glemontree' //在https://bintray.com上注册的用户名
groupId = 'com.glemontree' //jCenter上的路径
artifactId = 'PasswordView' //要上传的library名称
publishVersion = '1.0.0' //library的版本号
desc = 'A PasswordView library' //library的简单描述
website = 'https://github.com/Glemontree/PasswordView' //library的开源地址,例如在github上的地址
}
在AS的Terminal中执行命令
在Android Studio
中的Terminal
中执行如下命令:gradlew clean build bintrayUpload -PbintrayUser=BINTRAY_USERNAME -PbintrayKey=BINTRAY_KEY -PdryRun=false
,其中PbintrayUser
代表注册的用户名,PbintrayKey
表示API Key
,PdryRun
是一个配置参数,当为true
的时候,会运行所有的环节,但是不会上传,如下图:
当然API Key
你可以在下面的页面中找到:
直接点击图中最左侧的API Key
即可查看到。
不出意外的话等待几分钟即可上传成功。
当然这个过程中可能还会出现另外一个坑,如果你不在要上传的library
的build.gradle
文件的android
标签里添加如下代码:
lintOptions {
abortOnError false
}
那么你可能会遇到错误:Lint found errors in the project; aborting build.
。
上传成功
上传成功后就可以在maven
仓库中看到我们的library
,如下:
点击library
的名字后则可以查看详情:
此时我们还不能在项目中引用该库,要将其添加到jCenter
,点击上图右下角的Add to JCenter
按钮即可,等待数小时审核通过后会邮件通知你的,之后就可以在项目中引用了:
总结
本文参考自文章Android 发布开源库到 JitPack、jCenter,有兴趣的可以直接去Othershe的博客进行查看。